Need a vertical bar graph program working for user input

I am making a vertical bar graph program, and I managed to get one working, only it uses hardcoded numbers. I need it to take user input, and tried to get it to work with user input, but it goes into an infinite loop. Can someone help me out with getting it to work with user input?
Code below:

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 ( ) {
    int num;
    int array[40];
    cout << "Enter number of numbers here: ";
    cin >> num;
    for(int i = 0; i < num; i++) {
            cin >> array[i];
            }
    int largest = array[0];
    for(int cond = 0; cond <= 5; cond++){
            if(array[cond] > largest) {
             largest = array[cond];
            }
            for(int rows = largest; rows >= 1; rows--){
            for(int columns = 0; columns <= 5; columns++){
            if(array[columns] >= rows) {
             cout << "* ";
             }
            else {
                  cout << "  ";
                  }
            }
            cout << endl;
            }
            }
            for(int columns = 0; columns <= 5; columns++) {
             cout << "--";
             }
            cout << endl;
            for(int columns = 1; columns <= 6; columns++){
             cout << columns << " ";
             }
            cout << endl;
            }
It is not an infinite loop by any means. Your nested for-loops makes it run for a realllllyyyyyyyyyyyyyyyyyyyyyyyyyyy long time.

"largest" currently stores the smallest number, and not the largest one. But say that function worked fine, and the highest number someone entered was 40.

 
for(int cond = 0; cond <= 5; cond++)

This will run 5 times.

for(int rows = largest; rows >= 1; rows--)

This will run 40 or so times.

for(int columns = 0; columns <= 5; columns++)
And this will run 5 times.

So the for-loop for(int cond = 0; cond <= 5; cond++) Will run all of that 5 times. which is like what, 200 rounds? Your code still needs some work.

PS. The info I just gave is not 100% correct, but its more than close enough.
Last edited on
Okay, so how do I change it so that largest is correctly storing the largest?
Put it in its own for-loop. Before this one for(int cond = 0; cond <= 5; cond++)

So it would look like this -
1
2
3
4
5
6
7
8
9
10
11
  for(int i = 0; i < num; i++) {
            cin >> array[i];
            }


for (int i = 0; i < num; i++)
	{
		if (array[i] > largest) {
			largest = array[i];
		}
	}


Still goes on forever, and asks for numbers too many times.
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
47
48
49
#include <iostream>
using namespace std;
int main ( ) {
    int num;
    int array[40];
    cout << "Enter number of numbers here: ";
    cin >> num;
    for(int i = 0; i < num; i++) {
            cin >> array[i];
            }
    int largest = array[0];
     for(int i = 0; i < num; i++) {
            cin >> array[i];
            }


for (int i = 0; i < num; i++)
	{
		if (array[i] > largest) {
			largest = array[i];
		}
	}
    for(int cond = 0; cond <= 5; cond++){
            if(array[cond] > largest) {
             largest = array[cond];
            }
            for(int rows = largest; rows >= 1; rows--){
            for(int columns = 0; columns <= 5; columns++){
            if(array[columns] >= rows) {
             cout << "* ";
             }
            else {
                  cout << "  ";
                  }
            }
            cout << endl;
            }
            }
            for(int columns = 0; columns <= 5; columns++) {
             cout << "--";
             }
            cout << endl;
            for(int columns = 1; columns <= 6; columns++){
             cout << columns << " ";
             }
            cout << endl;
            }
    
    
Yeh. The code I gave you just puts the largest number into "largest".

I really dont know what you want your program to do, but something is definitely wrong with all these nested for-loops. If you in a good way explain what you want your program to do, I'll try and help you.
I need my program to do this:
1
2
3
4
5
6
Number of values to graph: 3
1 2 3

        #
     #  #
  #  #  #
 1  2  3

Well, the formatting messes it up a bit, but basically I want that with dashes under the # signs and the numbers lined up.
Last edited on
Bump........
I got it working a bit more, it now prints out the entered numbers correctly but there are no # symbols.
I also got the largest actually working properly.
Can someone help me get the # symbols to print correctly?
Code:
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
47
48
#include <iostream>
using namespace std;
int main ( ) {
    int num;
    int array[40];
    cout << "Enter number of numbers here: ";
    cin >> num;
    for(int i = 0; i < num; i++) {
            cin >> array[i];
            }
    int largest = array[0];
   //  for(int i = 0; i < num; i++) {
     //       cin >> array[i];
       //     }


for (int i = 0; i < num; i++)
	{
		if (array[i] > largest) {
			largest = array[i];
		}
	}
   // for(int cond = 0; cond <= 5; cond++){
         //   if(array[cond] > largest) {
       //      largest = array[cond];
     //       }
            for(int rows = largest; rows <= 1; rows++){
            for(int columns = 0; columns <= 5; columns++){
            if(array[columns] >= rows) {
             cout << "# ";
             }
            else {
                  cout << "  ";
                  }
            }
            cout << endl;
            }
            
            for(int columns = 0; columns <= 5; columns++) {
             cout << "--";
             }
            cout << endl;
            for(int columns = 0; columns < num; columns++){
             cout << array[columns] << " ";
          //   cout << "The largest (should be): " << largest;
             }
            cout << endl;
            }
bump
Your problem is that all of this -

1
2
3
4
5
6
7
8
9
10
11
for(int rows = largest; rows <= 1; rows++){
            for(int columns = 0; columns <= 5; columns++){
            if(array[columns] >= rows) {
             cout << "# ";
             }
            else {
                  cout << "  ";
                  }
            }
            cout << endl;
            }


Is being skipped. Why? Look here - for(int rows = largest; rows <= 1; rows++)

Think for a moment. What are you telling this for-loop. Say I enter 3 numbers. 4,5 and 6.

Now row = 6 because thats the largest. Then you tell the program to run the for-loop as long as 6 is less or equal to 1? Uhmm. 6 is never less or equal to 1.

Perhaps you meant - for (int rows = largest; rows <= largest; rows++) ?

Look through your code. Learn how to debug. Either look up some videos on youtube or read about it. It is very useful. Do some changes here and there and if you get any more problems feel free to post again :)
Last edited on
I replaced that line with the line you thought I meant, and it still doesn't work. Can someone help me more?
bump, really need help...
bump 2
I have updated my code a bit, but I still can't figure out why it doesn't work.
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
47
48
49
50
51
52
53
#include <iostream>
using namespace std;
int main ( ) {
    int num;
    int array[40];
    cout << "Enter number of numbers here: ";
    cin >> num;
    for(int i = 0; i < num; i++) {
            cin >> array[i];
            }
    int largest = array[0];
   //  for(int i = 0; i < num; i++) {
     //       cin >> array[i];
       //     }


    for (int i = 0; i < num; i++) {
        
		if (array[i] > largest) {
			largest = array[i];
		}
		
	}
   // for(int cond = 0; cond <= 5; cond++){
         //   if(array[cond] > largest) {
       //      largest = array[cond];
     //       }
     for (int rows = largest; rows <= largest; rows++) {
         
     for(int columns = 0; columns < num; columns++){
             
        if(array[columns] >= rows) {
        cout << "# ";
        }
        
        else {
        cout << "  ";
        }
        }
        cout << endl;
        }
            
        for(int columns = 0; columns <= 5; columns++) {
        cout << "--";
        }
        cout << endl;
        for(int columns = 0; columns < num; columns++){
                    
        cout << array[columns] << " ";
          //   cout << "The largest (should be): " << largest;
        }
        cout << endl;
        }

Can someone help me figure this out, spacing and all (I would post what I need it to print here but it glitches up, check http://cboard.cprogramming.com/cplusplus-programming/166052-vertical-bar-graph-not-working.html for what I need
I made progress:
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
47
48
49
50
51
52
53
54
55
#include <iostream>
using namespace std;
int main ( ) {
    int num;
    int array[40];
    cout << "Enter number of numbers here: ";
    cin >> num;
    for(int i = 0; i < num; i++) {
            cin >> array[i];
            }
    int largest = array[0];
   //  for(int i = 0; i < num; i++) {
     //       cin >> array[i];
       //     }




    for (int i = 0; i < num; i++) {
        
		if (array[i] > largest) {
			largest = array[i];
		}
		
	}
   // for(int cond = 0; cond <= 5; cond++){
         //   if(array[cond] > largest) {
       //      largest = array[cond];
     //       }
     for (int rows = largest; rows <= largest; rows++) {
         
     for(int columns = 0; columns < num; columns++){
             
      //  if(columns] >= rows) {
        cout << "# ";
        //}
        
    //    else {
   //     cout << "  ";
      //  }
        }
        cout << endl;
        }
            
        for(int columns = 0; columns <= 5; columns++) {
        cout << "--";
        }
        cout << endl;
        for(int columns = 0; columns < num; columns++){
                    
        cout << array[columns] << " ";
          //   cout << "The largest (should be): " << largest;
        }
        cout << endl;
        }

It now prints one # for each number, so the only problem left is figuring out how to get it to print (number) #s!
Topic archived. No new replies allowed.