Histogram Problem

How to display a histogram of a generated matrix

if the program asks the user to enter any character (symbol) E.g. #,*,&....
and then displays this charecter according to the elements of matrix arr[][]

for example if the elements of matrix arr[2][2] are : 3 6 2 1
the histogram will be in this form:

3 ***
6 ******
2 **
1 *

please could you help me

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
58
59
60
61
62
63
64
 #include<iostream>
#include<iomanip>
using namespace std;
void histogram( );

int main()
{
    int i, j,rows,cols;
    int arr[5][5];
    cout<< "enter rows and cols : ";
    cin>> rows>> cols;

	
    for(i=0;i<rows;i++)
    {
        for(j=0;j<cols;j++)
        {
            cout<< "enter element "<<j+1<<": ";
            cin>> arr[i][j];
        }
    }

    cout<< "\n Matrix :"<< endl;
     for(i=0;i<rows;i++)
    {
        for(j=0;j<cols;j++)
        {
            cout<<setw(4) <<arr[i][j];
        }
        cout<< endl<< endl;
    }

	 void histogram();
        
 
   return 0;
}


void histogram() // Calculate histogram  
{
	int i;
	char character;
	cout<<" Please enter the display character: ";
	cin>>character;
	
	
}

void display(int arr[][5],int rows,int cols) // Display the histogram
{
	
    int i,j;
	char character;
	
	do
	{
		for(i=0; i<rows; i++)
		for(j=0;j<cols; j++)
		cout<<arr[i][j]<<setw(4)<<character;
		
	}while( i<rows&&j<cols);
	
}
Last edited on
please could you help me

What help do you need?

Line 33: That's a function prototype, not a function call. remove the void

Lines 40-48: These lines prompt for the character to display, but the character is lost when the function exists.

Line 54: character is never initialized.

Line 60: You don't want setw(4) here. You also want to display character for the number of times specified by the contents on arr[i][j].

BTW, it's a little unusual to use a two dimensional array for a histogram. Usually a histogram is one dimensional array.



Last edited on
Hi,

Firstly change the histogram function like this

1
2
3
4
5
6
7
8
9
char histogram() // dont forget to change the prototype to char 
{
	
	char character;
	cout<<" Please enter the display character: ";
	cin>>character;
	return character;
	
}//although you could have easily done it without function :D 


then add this to your main()

1
2
3
4
5
6
7
8
9
10
11
12
cout<<endl<<endl;
	 char ch;
	 ch=histogram();
for(i=0;i<rows;i++)
    {
    for(j=0;j<cols;j++)
        {
        cout<<arr[i][j]<<" ";
             for(int l=0;l<arr[i][j];l++)//this is the code you were looking for
             cout<<ch;//I am just iterating form 0 to number and printing char
        cout<<endl<<endl;
        }


I checked it and here's the possible output


enter rows and cols : 2 2
enter element 1: 3
enter element 2: 6
enter element 1: 2
enter element 2: 1

 Matrix :
   3   6

   2   1


 Please enter the display character: *
3 ***

6 ******

2 **

1 *

 
Exit code: 0 (normal program termination)

Hope it helps
thank you very much
Topic archived. No new replies allowed.