Need Help with School Coding Question

Hi all. I've been working on this problem for a week but just can't crack it. I'm very new to c++ and taking a class on it, and this is one of the questions. I'm not that strong at plotting graphs yet, and I received some feedback that I should put the outputs in array first, but I suuuck at arrays. =/



The output should be a parabolic line with x values from 0-1 and y values from 0-0.1 in the downward direction. (but not negative values)

The attached code I have is whack. My x values are increasing to 1 at a weird increment, then decreasing back to 0, and my plots are linear, not parabolic.

Any help would be greatly appreciated. If you simply post the workable code for the problem, please, if you could, explain the plotting and the array if you use one.

Thank you.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
   cout<<fixed<<setprecision(2)<<endl;

   int i = 0;
   double P, u;
   double k = 0.25;

   cout<<"P    ";

   for(u=0; u<=1;u+=0.05)
   {
       P = (k*u*(1-u))/(k+u);

       cout<<P<<" ";
   }

   for(u=0.00; u<=0.25;u+=0.05)
   {
       i++;

       cout<<"\n"<<u;
       for(int j = 0; j<i;j++)
       {
           cout<<"  ";
       }
       cout<<".\n";
   }

return 0;
}
]
Last edited on
Maybe you should start by stating your equation correctly.

Your code doesn't correspond to your stated equation, and neither of them are parabolic (quadratic) in u. (So it's a silly question).

Your coded equation is at least that given in Chegg.


Dump your output here to file and plot it in gnuplot. C++ doesn't do native plots: try it in Python and use matplotlib if you want that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
   const double k = 0.25;
   const int N = 20;
   const double du = 0.05;
   for ( int i = 0; i <= N; i++ )
   {
      double u = i * du;
      double P = k * u * ( 1 - u ) / ( k + u );
      cout << fixed << setprecision(2) << setw(10) << u << setprecision(4) << setw(10) << P << '\n';
   }
}


      0.00    0.0000
      0.05    0.0396
      0.10    0.0643
      0.15    0.0797
      0.20    0.0889
      0.25    0.0938
      0.30    0.0955
      0.35    0.0948
      0.40    0.0923
      0.45    0.0884
      0.50    0.0833
      0.55    0.0773
      0.60    0.0706
      0.65    0.0632
      0.70    0.0553
      0.75    0.0469
      0.80    0.0381
      0.85    0.0290
      0.90    0.0196
      0.95    0.0099
      1.00    0.0000



Make sure that u goes from 0 to 1 (in your second loop it only goes from 0 to 0.25).
Last edited on
Plugging the equation into my graphing calculator, with k=0.25, I get a parabola output.
That is not a parabola. A parabola has a line of symmetry.

Plot the data from my code. You can use either Excel or gnuplot.
Thanks for the help. I can get the data plots no problem. Bu the output specifically needs to be the plotted graph. We've used for statements in the past to plot. So the graph needs to be in c++. That's the main thing I'm having trouble with.
So the graph needs to be in c++.


Standard C++ doesn't have graphics.
If you want to draw from c++ you need a plotting library. Which plotting library do you have available/want to use?

Personally, I would dump the data to file and plot it with gnuplot:
http://www.gnuplot.info/
We don't use a plotting library. We've been using for statements and arrays.

Sorry I've still very new to this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;

int main()
{
   const double k = 0.25;
   const int N = 20;
   const double du = 0.05;
   vector<string> canvas( 21, string( 1+N, ' ' ) );
   for ( int i = 0; i <= N; i++ )
   {
      double u = i * du;
      double P = k * u * ( 1 - u ) / ( k + u );
//    cout << fixed << setprecision(2) << setw(10) << u << setprecision(4) << setw(10) << P << '\n';
      canvas[(int)(200*P+0.5)][i] = '*';
   }
  
   for ( int i = 0; i <= 20; i++ ) cout << canvas[i] << '\n';
}


*                   *
                     
                   * 
                     
                  *  
                     
                 *   
                     
 *              *    
               *     
                     
              *      
                     
  *          *       
            *        
           *         
   *                 
          *          
    *   **           
     ***             
Topic archived. No new replies allowed.