-
create a graph of y=x^2
well i have this scholl project on c++
i manage to create the x and y axis but my only problem is the graph it self
in this specific project not allowed to use any graph libraries or anything equivalent only 4 chars space,*,-,|
here is what i i did
void main()
{
int i = 1;
int x = 0;
int y = 0;
int k = 0;
char a;
const screen_length = 79;
const screen_height = 25;
const minx = 10;
const maxx = -10;
const miny = -5;
const maxy = 100;
int xstep = maxx - minx/79-1;
//int xstep= screen_height/screen_length;
while (i<screen_height)
{
//x = minx;
while(k<screen_length)
{
if (y == x * x)
cout<<'*';
else
if (k == screen_length/2)
cout<<'|';
else
if (i == 24)
cout<<'_';
else
cout<<' ';
x++;
k++;
y++;
}
cout<<endl;
k = 0;
i++;
y--;
}
cin>>a;
}
-
For variety, here's an alternate algorhythm that has a shot at producing an acceptable result.
1) let x axis be vertical axis and y axis be horisontal
2) x axis located on far left of screen
3) y axis cuts left to right across the middle of the screen
4) label x at top and y at far right
5) label the 2 axis values with even numbers only starting with 2
6) x axis depicted by | and y by - (or _);
7) any point x y on the graph is identified by a *
8) depict screen as 2D char array of dimension 25 by 80 allowing max x to be 8 or -8 and max y to be 64
9) initialize array to all spaces
10) use a number of loops to assign all non-calculated chars (everything but the *) to all the respective elements
11) assign all points of the graph to * using a loop or two
12) display array (that is the "graph") using yet another loop.
-
Yes, as the previous poster points out, your y gets big quick, so it displays better on the screen if the y is horizontal. Here is some code to display it in several pages. You could also write the graph to a file.
Code:
#include <iostream.h>
#include <conio.h>
int main()
{
int i = 1;
int x = 0;
int y = 100;
int k = 0;
char a;
const screen_length = 79;
const screen_height = 25;
//const screen_height = 24;
const minx = 10;
const maxx = -10;
const miny = -5;
const maxy = 100;
int xstep = maxx - minx/79-1;
//int xstep= screen_height/screen_length;
while (i<=maxy)
{
//x = minx;
while(k<screen_length)
{
if (y == x * x)
cout<<'*';
else if (k == 0)
cout<<'|';
//else if (i == 24)
else if (i % screen_height == 0)
cout<<'_';
else
cout<<' ';
x++;
k++;
}
if (i % screen_height == 0)
getch();
cout<<endl;
k = 0;
i++;
x = 0;
y--;
}
getch();
}
-
thx guys
ill try them out