homework help please!

Hi everyone.. I need some help on a homework assignment. I have been reading on it and trying to create the program for the past 5 hours or so and now im just getting frustrated.. I'm not looking for you to do it for me.. just a push in the right direction. :)

Here is what we are supposed to do:
Create a bar chart using * and spaces from a set of integers through standard input.

The user will input a set of integers up to 100 greater than 0. When the user enters 0 the program will stop reading standard input and print the chart.

For example if the user enters 1 2 3 4 3 2 1 10 0 it would print:
1
2
3
4
5
6
7
8
9
10
       *
       *
       *
       *
       *
       *
   *   *
  ***  *
 ***** *
********

Here is my (many times edited) code so far.. Not sure if it is headed in the right direction?

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
  4 #include<iostream>
  5 using namespace std;
  6 
  7 int main()
  8 {
  9   const int MAX = 100; 
 10   int values[MAX]; // 
 11 
 12  
 13
 14   int i;
 15   int x;
 16   int y;
 17 
 18  for(i=0; i<MAX; i++)
 19   {
 20     cin >> values[i];
 21   }
 22 
 23 
 24   for(x=0; x<100; x++)
 25   {
 26    for(y=0; y<MAX; y++)
 27    {
 28      if(values[y]>=100-x)
 29      {
 30        cout << "*";
 31      }
 32      else
 33      {
 34        cout << " ";
 35      }
 36    }
 37    cout << endl;
 38   }


Anyways, any help would be great. Thanks!
Last edited on
check the input while getting the value if 0 break the loop.
Hope this works

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
#include<iostream>
 using namespace std;
 
int main()
{
  const int MAX = 10 ;
  int values[MAX]; 
  int i;
   int x;
  int y;
  int count=0;

 for(i=0; i<MAX; i++)
  {
    cin >> values[i];
    if (values[i] == 0)
    break;
    count++;
  }

   for(x=0; x<MAX;x++)
   {
  for(y=0; y<count; y++)
   {
     if(values[y]>=MAX-x)
      {
     cout << "*";
      }
     else
   {
       cout << " ";
      }
    }
    cout << endl;
   }
}
Last edited on
Thank you so much! It worked :)
Topic archived. No new replies allowed.