Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,208,980 members, 8,004,507 topics. Date: Saturday, 16 November 2024 at 06:00 PM

Graphing In C++?? - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Graphing In C++?? (4499 Views)

Creating UWP Application In C# Or C++ (xaml) / Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD / [problem] Write A Program In C++ That Finds The Hcf Of 2 Numbers Without Using A Recursive Function (2) (3) (4)

(1) (Reply) (Go Down)

Graphing In C++?? by OluwaKANYE: 4:56pm On Nov 30, 2008
The goal of this programming assignment is to implement a program in C++  that displays a table of values for simple formulas of the form f(x) = A*sin(B*x) + C*sin(D*x) for different values of A,B,C,D. After the table of values is printed your program should then plot a curve on the screen. You must use cout/cin (not printf/scanf) and C++ style parameter passing in this program. For example, if the user inputs A, B, C, D values to ask you to plot 5*sin(1.5*x) + 3*sin(2.5*x) from 0, 3, your program should output a table something like this:

  x  | y
-----------
0.00 | 0.00
0.20 | 2.92
0.40 | 5.35
0.60 | 6.91
0.80 | 7.39
1.00 | 6.78
1.20 | 5.29
1.40 | 3.26
1.60 | 1.11
1.80 | -0.80
2.00 | -2.17
2.20 | -2.91
2.40 | -3.05
2.60 | -2.79
2.80 | -2.39

Then, it should output a plot something like this:

0.00 |                     *
0.20 |                             *
0.40 |                                   *
0.60 |                                       *
0.80 |                                        *
1.00 |                                      *
1.20 |                                   *
1.40 |                              *
1.60 |                        *
1.80 |                    *
2.00 |                *
2.20 |              *
2.40 |              *
2.60 |               *
2.80 |                *
     +----------------------------------------+
    -8.00                                   8.00

Notice that this plot shows the x values on the left and the horizontal position of the * is given by the corresponding f(x) value. To implement this, you will need to use iteration. The outer loop will control how many lines get printed. An inner loop (or a function call) will print the number of spaces needed in the current line.

Your program should prompt the user for values of A,B,C,D and the range of x values to plot. Because different parameters will result in different range of f(x) values to plot, you will have to perform a conversion of some sort so the display fits within a fixed width window.
3. Design:
You have several basic steps, each of which should be done in a separate function:
# Get user input  done
# Calculate X, Y values and store them  done
# Print a table of X, Y values      done
# Plot the X, Y values        I'm stuck sad cry

here's the code for what i've done so far

#include<stdio.h>
#include<math.h>
#include<iostream>
#include<iomanip>
using namespace std;

const int NumSteps=15;


void Fillx(const float Stepsize, const float xmin, float XVals[])
{
   for(int i=0;i<NumSteps;i++)
      XVals[i]=xmin + Stepsize*i;
}
void Filly(float XVals[], float YVals[])
{
   float A,B,C,D;

     cout << "Enter the value for A: ";
     cin >> A;
     cout << "Enter the value for B: ";
     cin >> B;
     cout << "Enter the value for C: ";
     cin >> C;
     cout << "Enter the value for D: ";
     cin >> D;
     for (int i=0; i < NumSteps; i++)
     YVals[i]=A*sin(B*XVals[i]) + C*sin(D*XVals[i]);
     cout << endl;
}
int main()
{
  float xmin,xmax;
  float Range, Stepsize;
  float XVals[NumSteps];
  float YVals[NumSteps];
  cout << "Graph plotter Program by"      << endl;
  cout << "###################" << endl;
  cout << "##Name                  ##" << endl;
  cout << "### ID NO  ###" << endl;
  cout << "###################" << endl;
  cout << endl;
  cout << "Enter Min value of x: ";
  cin >> xmin;
  cout << "Enter Max value of x: ";
  cin >> xmax;
  Range=xmax-xmin;
  Stepsize=Range/NumSteps;
  cout <<endl;
  Fillx(Stepsize, xmin, XVals);
  Filly(XVals, YVals);
  cout <<"  X "<<" | "<<" Y " << endl;
  cout <<"------------"<<endl;
  for (int i=0; i < NumSteps; i++)
  printf("%4.2f | %4.2f\n",XVals[i],YVals[i] );
  return 0;

}


I really need help on how to include a function to plot the graph before midnight today embarassed embarassed any help would be highly appreciated embarassed
Re: Graphing In C++?? by Kobojunkie: 6:40am On Dec 01, 2008
// SHORT WAY TO PRINT GRAPH
//y = A*sin(B*x) + C*sin(D*x)
//Since you have a range for min and max Y(f(min X), f(maxX))
// Get the min and max values stored somewhere
// 1) PRINT the Value of X
//2) calculate f(x) for x Value, Print (f(min X)+ f(x)) SPACES
//3) PRINT asterix at (y,x) point ( just printing * after the spaces should suffice)
//4) print end of line and start over for each X
//5) Print the Y axes and points outputting f(MIn X) and each ends as you have in your graph

Ps. Sorry, my compiler not working well tonight as I need to reload for C++ . will check in to help if you still need help
Re: Graphing In C++?? by OluwaKANYE: 6:49am On Dec 01, 2008
@Kobojunkie

//2) calculate f(x) for x Value, Print (f(min X)+ f(x)) SPACES
  //3) PRINT asterix at (y,x) point ( just printing * after the spaces should suffice)
  //4) print end of line and start over for each X
  //5) Print the Y axes and points outputting f(MIn X) and each ends as you have in your graph

Thanks, i really appreciate it but i still can't get it right. I could print out the arrays for x value and the corresponding f(x)=y for it, i also got the range(xmax-xmin),
please can you explain a little bit more from 2). Thanks so much smiley
Re: Graphing In C++?? by Kobojunkie: 5:42pm On Dec 01, 2008
Note that I make the following assumptions here

a) Based on user's entry, you calculate the Max Y value, and you have that done
b) Min Y is simply negative of Max Y, you also mark that as done
c) You already have your 15 X values from Min(x) to Max(x)
d) You start printing from min Y point on graph i.e -8 in the case of your post
e) Zero point for most case will be ((min(Y) - (min(Y))
eg, say minY is 2 then, 2-2 = 0,
in case minY = -8, then (-cool - (-cool = 0


To Output the Graph, Use a FOR LOOP to start from the min(x) value in your array, within your loop do the following
Step one: Print the X Value and follow it with the '|' character as shown in the graph
Step two: Using iteration to calculate ((min(Y) - (min(Y)) + Y), print a space character for each move
Step Three: Print an * right after that.
Repeat Steps one through Three for each X

Try that and see,  still have not gotten around to writting it
Re: Graphing In C++?? by OluwaKANYE: 6:17am On Dec 02, 2008
@Kobojunkie  I finally figured out how to do it after spending the whole night battling with it. It worked just the way you explained it THANKS smiley  I'm really grateful for your assistance smiley
Re: Graphing In C++?? by quadrillio(m): 4:12pm On Dec 02, 2008
@kobojunkie

Thanks, Cos I also learn something from this.

How u doing?

(1) (Reply)

Build An Android Instant Messaging App Using Sinch And Parse In 2 Hours / Building Website & Mobile Apps / Oracle 10g On Solaris Os

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 24
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.