Histogram Problems

Im a first year C++ programer using unix and have no idea how to do this question. I think ill either have to use a 2D array or a 1D array with multiple do for and while loops. Im mainly strugling with plotting the histogram in vertical colums this is the question

Implement a C++ program that reads a sequence of positive integer numbers terminated by a negative number (sentinel) and displays a histogram of all numbers. A histogram shows the total number of occurrences for each number in a sequence. For example a sequence of numbers can be delimetered by whitespace or newline bytes.
1 2 2 1 4 5 4 2 3 5 6 7 6 4 1 2 -1

has the following histogram.
*
* * *
* * * * *
* * * * * * *
-------------
1 2 3 4 5 6 7
You program should display a histogm


thanks guys x x
You may find that people on here are fairly unhelpful if you basically just ask them to write a program for you, especially if it sounds like it's a homework exercise ;)

You are much more likely to get constructive help if you actually post the code you have been working on and ask specific questions about the parts you have trouble with. :D

Regards
-Xander314

PS: And if you put code on here, remember to enclose it in [code][/code] tags so you get syntax highlighting:
[code]int main()[/code] -> int main()
Last edited on
Run a loop from 1 to number of row required. for each loop iteration, print stars (*), equivalent to the value of loop index.

Rest @Xander314 has explained. :)
Last edited on
sorry first time poster lol :P

this is my code so far

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 number[10] = {0};	/* define a 1D array */
int i = 0;	/* set i to zero so they can be used in the loop*/
int num;
int biggest = 0;


	cout << "Enter a sequence of one or more positive integer numbers terminating";
	cout << " with a (-1) to indicate the end of your sequence. ";
	cin >> num;
do
{
	for (i=0; i<10; i++)
	{
		cin >> number[num - 1];
		number[num - 1]++;
	
		
	
	biggest = number[i];
	

		for (i = 0; i < 10; i++)
		{
			for (int y = biggest; y>= 0; y--)
			{
				if (number[i] >= y)
					cout << "*";
				else 
					cout << " ";
			}
		}
	}
}while (num != -1);		/* -1 is the sentinel */
	
	





return 0;
}
Last edited on
also please add code tags to do this just press the <> buttons under format and put your code inbetween the two tags.
You input a value into num which is subsequently never changed. Thus your do ... while loop will last for forever, unless the user enters -1 at the start in which case it will run once.

You had this:
1
2
3
4
5
for (i=0; i<10; i++)
	{
		cin >> number[num - 1];
		number[num - 1]++;
	

You are reading every input into the same slot in the array as num does not change. Do you mean to increment the value num? Even so, I don't really see why this variable is necessary: you have the loop counter i.

Regards
-Xander314
also my advice for actually creating the graph. since its impossible to go back a line after using endl or \n instead start a for loop with the variable i having the value of the largest number (decrement i until its equal to zero). then go through each number entered and if its greater than or equal too i print an * for it, otherwise dont.
Im fairly new at programming too but ive had a crack at the problem. I think im close probally something to do with the storing of my largest variable, maybe someone could fix it for me. Hope this helps.


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

int main()
{
int highest=0;
int num[10];
int frequency[10]={0};
int largest=0;
int x;

cout << "Enter a sequence of one or more positive integers terminating by -1"<<endl;



    for(int i=0; i<10; i++)
        {cin>>num[i];}


   for(int i = 0 ; i < 10 ; i++)
{
        x=num[i];
       frequency[x]++;
       
{

   if(frequency[x+1]>frequency[x])
    frequency[x+1]=largest;
    else
    frequency[x]=largest;
    }


    for (int i = largest; i> 0; i--)
{
for (int j = 0; j < 100; j++)
{
if (frequency[j] >= i)
cout << "*";
else
cout << " ";
}
}

}


return 0;

}
finding the largest element of an array is easy. looks like this:
1
2
3
4
5
6
7
int nums[5] = { 2, 5, 8, 50, 19 };
int largest = nums[0];
for (int i = 1; i < 5; i++)
{
	if (largest < nums[i])
		largest = nums[i];
}


your code doesnt allow for the user to enter -1 to terminate, it just gets 10 numbers. i dont understand at all what your second for loop is doing, it would be very easy for it to go out of bounds of the array if the user enters something other than 10. also you dont need nested for loops or anything.
Last edited on
boom shakalaka
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>
using std::cout;
using std::endl;
using std::setw;
using std::cin;

int main(void)
{
	const int max = 10;
	int list[max] = { 0 };
	
	cout << "Enter up to 10 numbers.  To stop enter a negative number. " << endl;

	cin >> list[0];
	for (int i = 1; i < 10 && list[i - 1] != -1; i++)
	{
		cin >> list[i];	
	}			

	int largest = list[0];
	for (int i = 1; i < max; i++)
	{
		if (largest < list[i])
			largest = list[i];
	}

	for (int i = largest, numCount = 0; i > 0; i--, numCount++)
	{
		cout << endl << "|";
		for (int y = 0; y < 10; y++)
		{
			if (list[y] >= i)
				cout << setw(3) << "*";
		}
	}
	
	cout << endl;
	for (int i = 0; i < 10; i++)
	{
		cout << "----";
	}

	cout << endl;
	return 0;
}
I have the same issue, I can output my "*" horizontally but not vertically like I am trying to do, the post above isn't what's needed for my purpose

1
2
3
4
5
6
7
8
while ( column < largest )
	{
		for ( int z = 0; z < largest; z++ )
		
		{if (counter[column] >= largest- z)
		cout << "*"; else cout << " ";}
	column++;
	}


Can anyone suggest how to turn these stars vertically rather then horizontally?
the code i just posted prints them vertically, read it.
Topic archived. No new replies allowed.