Need advice on how to produce a bar graph

I need some advice on how to produce a bar graph where one * is equal to one unit. I have tried what it says to do in my book but it does not compile. Typed in correctly it does not compile. I was wondering if someone could point me in the right direction. Any help would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

#include<iostream>
#include<iomanip>
#include<cmath>
 
using namespace std;
 
int main()
{

     double avgRain[] = {1.81, 1.04, 0.27, 7.25, 7.79, 2.88, 9.71, 5.04, 3.59, 8.80, 3.67, 2.07};
     double actRain[] = {9.57, 4.42, 4.78, 3.14, 8.72, 3.24, 6.01, 6.31, 9.76, 6.10, 8.37, 6.29};
     string months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};


     int total, total2;

    cout << "Month" << setw(18) << " Average" << setw(11) << " Actual" << setw(16) << " Total Below" << setw(13) << " Total Above" << endl;
    cout << setw(23) << "Rainfall" << setw(12) << "Rainfall" << setw(13) << " Average" << setw(12) << " Average" << endl << endl;
     for (int i = 0; i < 12; i++)
     {
          total = avgRain[i] - actRain[i];
          total2 = actRain[i] - avgRain[i];
          //cout << total;

          if( total < 0 )
          {
               total = 0;
               //cout << total;
          }

          if ( total2 < 0)
          {
               total2 = 0;
               //cout << total2;
          }
          
          cout << setw(11) << left << months[i] << setw(11) << right << avgRain[i] << setw(11)
               << actRain[i] << setw(11) << total << setw(15) << total2 << endl;

     }
     return 0;
}


Last edited on
Where is the graph part?
For an horizontal bar:
std::cout<<'*'; this will print one asterisk. Repeat it n times.

For vertical bars:
calculate the height of every bar, compute the maximum.
prints an asterisk if the value is greater or equal than the row.
stop when row is 0

Sorry, I modified it to at least show some of the bar graph. I need it to use '*' as a representation of the amounts listed. But, the problem is there is no way to show a 1.8 using asterisks, I need to round either up or down to produce the whole amount. I also used the "std::cout<<'*';" and all it did was multiply the results.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

#include<iostream>
#include<iomanip>
#include<cmath>
 
using namespace std;



int main()
{

     double avgRain[] = {1.81, 1.04, 0.27, 7.25, 7.79, 2.88, 9.71, 5.04, 3.59, 8.80, 3.67, 2.07};
     double actRain[] = {9.57, 4.42, 4.78, 3.14, 8.72, 3.24, 6.01, 6.31, 9.76, 6.10, 8.37, 6.29};
     string months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};




     
     
     for ( int i = 0; i < 12; i++)
     {

    
          double j = avgRain[i]/1.0;
          cout.setf(ios::showpoint);
          cout.setf(ios::fixed);
          cout.precision(1);
          cout << setw(15) << left << months[i] << j << " " << endl;

     }
     return 0;
}
Topic archived. No new replies allowed.