Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How will I draw the quadratic equation? 1

Status
Not open for further replies.

Sugarmello

IS-IT--Management
Jan 3, 2011
20
0
0
CY
//I need to draw the parabola U of the equation y=x^2//
//So I have the following which is not complete. What is missing to be correct?//
#include<iostream>
#include<cmath>

using namespace std;
int main() {

dpuble y;
double x;
int max;
int min;
void printStars( int x);
int x = 0; while(x < 10) {
double y = sqrt((double)x); x++; return 0; }

for(int i=0; i<20; i++)

{

for(int j=0; j<20; j++)

{

cout << y[j];

}

cout << endl;

}


//Is the following needed?//
//if(y>ymax)

{

ymax=y;

}

else if(y<ymin)

{

ymin=y;

//}

void printStars( int x)
{

int i;

if (x == 0)
return;
for(i = 0; i <= x; i++)
cout << " * ";
cout << "\n";

 
Can you explain (to youreself;) what and where do you want "to draw"?

It seems you are writing console (not GUI) program.
What's your x-axis? What's your y-axis?

Why y = sqrt(x)? It's funny but sqrt means "square ROOT of" so you have y^2 = x (not y = x^2)...

Try to draw possible solution "by hand" then think about the program which can produce the same picture...
 
Do you mean a console graph, something like
Code:
   |      **
   |     *  *
   |    *    *
   |   *      *
   |   *      *
   |  *        *
   |  *        *
   +--*--------*-------
 
Yes xwb like the graph you have shown but the opposite.
The U shape. Also the rules have changed and the graph should be drawn with constants.
const int length=79
const int height=24
const int minx= -10
const int maxx=10
const int miny=-10
const int maxy=100

So how is this done now?
with arrays or just for loops?
 
Easiest way is to declare a 2D char array (char graph[height + 1][length + 1].

1) Fill graph with spaces (memset (graph, ' ', sizeof(graph))
2) Write a routine to dump the array to file/screen
3) Try drawing the axes and then dump it
4) Now you need to work out the scaling.

float xscale = length / (maxx - minx)
float yscale = height / (maxy - miny)

5) Work out the points for the graph.
6) For each x, y point,

xpoint = (x - minx) * xscale
ypoint = (y - miny) * yscale
graph[ypoint][xpoint] = '*';

7) Now dump the graph.
 
xpoint and ypoint are integers so you need to cast the expression.
 
Having this code what is missing or is being wrong in order to draw the graph correctly?

#include<iostream>
using namespace std;
int main()
{
const int screen_length=79;
const int screen_height=24;
const int minx=-10;
const int maxx=10;
const int miny=-10;
const int maxy=100;
double xx=20/static_cast<float>(screen_length);
double yy=static_cast<float>(screen_height)/110;
int xpoint,ypoint;
for(int i=1;i<=24;i++)
{
if(i==23)
{
for(int k=1;k<=80;k++)
cout<<"-";
}
xpoint=(i-minx)*xx;
for(int c=1;c<=80;c++)
{
if(c==40)
cout<<"|";
else
cout<<" ";
ypoint=(c-miny)*yy;
if(ypoint==xpoint*xpoint)
cout<<"*";
}
cout<<endl;
yy+=yy;
xx+=xx;
}
cout<<xx<<endl<<yy;
cin>>xx;
return 0;
}
 
The problem with drawing the graph directly is you can't go back. It is better to draw it in an array and then dump the array.

Declare the array as graph[screen_height + 1][screen_width + 2]
Set the whole graph to spaces - basically a blank sheet of paper.
Now terminate all the lines at screen_length + 1. This will give you an array of char[].

Replace
Code:
            for(int k=1;k<=80;k++)
                cout<<"-";
with
Code:
   for (int x = 0; x <= screen_length; ++x)
      graph[23][x] = '-';
 
Thank you but there is there a possible way without the array?
 
Not something I have ever thought about. It is possible at a price: lots of recomputations. It isn't worth saving a bit of memory for that many recomputations.

For a quadratic, you could always draw it sideways: i.e. y horizontally and x vertically. You wouldn't believe how many people turn their heads to read such graphs. The problem is if you then want to draw x^2 + y^2 = n, it gets difficult.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top