Hi,
I'm a mathematics major, so the concepts behind this make sense, but I have the hardest problem putting it into computer language.
So I am supposed to write a function that produces this histogram:
Obviously since I created this by typing, it's a little off, but that's the idea behind the program.
I have the following function (in progress, does not output the numbers on either the x or the y-axis):
[CODE=cpp]void graph(int a[], int lo, int hi, int ymax)
{
int x, y;
for (y=19; y>=-1; y--)
{
for (x=-2; x<=25; x++)
if (y==0 && x>=0 && x%3==0)
cout << "+";
else if (y==0 && x>=0 && (x%2==0 || x%1==0))
cout << "-";
else if (x==0 && y>=0 && y%2==0)
cout << "|";
else if (x==0 && y>=0 && y%1==0)
cout << "+";
else
cout << " ";
}
}[/CODE]
Why do I get some horribly spread out mess?!
I feel like I should be using x and y to represent the xy-axis, so when x==0 I'm referencing the origin and then making y be variable and vice versa, but the screen comes up jumbled.
I'm a mathematics major, so the concepts behind this make sense, but I have the hardest problem putting it into computer language.
So I am supposed to write a function that produces this histogram:
Code:
|
18+
|
16+
|
14+
|
12+
|
10+
|
8+
|
6+
|
4+
|
2+
|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
0 1 2 3 4 5 6 7 8 9 10 1112 1314 15 161718 19 2021 22 2324 25
I have the following function (in progress, does not output the numbers on either the x or the y-axis):
[CODE=cpp]void graph(int a[], int lo, int hi, int ymax)
{
int x, y;
for (y=19; y>=-1; y--)
{
for (x=-2; x<=25; x++)
if (y==0 && x>=0 && x%3==0)
cout << "+";
else if (y==0 && x>=0 && (x%2==0 || x%1==0))
cout << "-";
else if (x==0 && y>=0 && y%2==0)
cout << "|";
else if (x==0 && y>=0 && y%1==0)
cout << "+";
else
cout << " ";
}
}[/CODE]
Why do I get some horribly spread out mess?!
I feel like I should be using x and y to represent the xy-axis, so when x==0 I'm referencing the origin and then making y be variable and vice versa, but the screen comes up jumbled.
Comment