what I need to do is actually make it vertical, but in order for me to even start on that i need to find the max number and total numbers of my array, since I'
m new to C++ i have no idea where to start. any thoughts?
#include <iostream>
#include <iomanip>
usingnamespace std;
int main ()
{
constint max = 100;
int array[max]; // creates as many ints as necessary
int values;
for (int i = 0; i < max; i++)
{
cin >> array[i];
// until the array is full
for (int j = 0; j < array[i]; j++) // loop to print the stars for each array
{ // value
cout << setw(3) << "*"; // print the star
}
cout<<endl;
}
return 0;
}
You can track the maximum value as the user enters values.
1 2 3 4 5
#include <algorithm>
int max_value = 0;
...
// after cin >> array[i]
max_value = std::max(max_value, array[i]);
You will want a way for the user to break out of the loop (like maybe by entering a negative value) so they don't have to enter in 100 numbers every time.
I assume that the columns have to be aligned on the bottom of the graph.
Try to solve something simpler. Assume that the input is in the range 0-5. Then try to produce vertical output with height 5 (the maximum input allowed).
To simplify even further - use intermediate 2d array (say named graph) with dimensions [5][max] that will store something like the image of the graph before you output it. You will fill it with spaces and stars. You will remove it from your final version, but for now you can make your life easier that way.
What you have to do is work from the top and scan your way down.
- For line 5, you seek all entries in array[] that have the value 5 in them.
If array[x] has 5, then you write star in graph[4][x].
If array[x] has something < 5, then you write space in graph[4][x].
- Then you move to line 4 (counting upwards) of the graph. Now, you seek all entries in array[] that have value 4 or bigger.
If array[x] has something >= 4, you write star in graph[3][x].
If array[x] has something < 4, you write space in graph[3][x].
You go down to line 1 and graph[0][x] like this. Then output the graph array to the screen.
Then convert your code to directly output to the screen. This is not very hard, since you did the processing in the direction in which the console prints its output.
When you come to that, you must find the maximum value in array[]. I described the solution when you already know that the maximum value is not bigger then 5. So, your code was supposed to start scanning from 5 and work its way down. If the maximum in array[] is n, then you have to start from n, and scan your way down to 1.