Vertical bar graph

Write a program that accepts a list of up to 40 non-negative integers and displays a vertical bar graph of those values.

Number of values to graph: 5
8 4 6 3 7

#
# #
# # #
# # #
# # # #
# # # # #
# # # # #
# # # # #
---------------
8 4 6 3 7
This example graph isnt posting right its supposed to be 4 # signs above the 4 and so on.

Have everything set for my arrays and cutting array off at the right point but my loop to create the # sign graph seems to be messed up as its ony printing one line of #s and spaces.

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
#include <iostream>
#include <iomanip>
using namespace std;

int main(){
	
	int num;
	int input;
	int size = 0;
	int array[40];
	
	cout << "Number of values to graph:";
	cin >> num;

	
		for(int i = 0; i < 40; i++){
			cin >> input;
			 if(size == (num - 1))
        {
            break;
        }
			array[i] = input;
			size++;
		}
    
    int high = array[0];
    
    for(int c = 0; c < size; c++){
    	if(array[c] > high)
    	high = array[c];
	}
    
    for(int row = high; row >= 1; row = row - 1){
    	for(int col = 0; col <= num; col++){
    		if(array[col] >= row)
    		cout << "#";
    		else
    		cout << " ";
    		
		}
	}
	
}
	
Last edited on
closed account (SECMoG1T)
count down from the largest value.


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
56
57
#include <iostream>
#include <iomanip>

int main()
{

   const int MAX_SIZE = 40;
   int num_of_elements{};
   int input_var{};
   int array[MAX_SIZE];

    std::cout << "Number of values to graph [1 - 40]: ";
    std::cin >> num_of_elements;

    while(num_of_elements < 1 || num_of_elements > MAX_SIZE)
    {
        std::cout<<"retry: num of values to be graphed should be within range[1 - 40]: ";
        std::cin>>num_of_elements;
     }

     int highest_value = 0;


    for(int i = 0; i < num_of_elements; i++)
    {
        std::cout<<"Enter the "<<i+1<<" value: ";
        std::cin >> input_var;

        if(input_var > highest_value)
           highest_value = input_var;

        array[i] = input_var;
    }

    std::cout<<"\n\n";

    for(int countDown = highest_value; countDown > 0; countDown--)
    {
        for(int valIndex = 0; valIndex < num_of_elements; valIndex++)
        {
            if(countDown <= array[valIndex])
                std::cout<<"#";
            else
                std::cout<<" ";

            std::cout<<"  ";
        }

        std::cout<<"\n";
    }

    std::cout<<"----------------------------------\n";

    for(int i = 0; i< num_of_elements; i++)
        std::cout<<array[i]<<"  ";
}
Last edited on
Topic archived. No new replies allowed.