Array (Histogram)

closed account (1CohAqkS)
You are asked to write a program (in functions) that will read in twelve integers (all between 0 and 40) which are monthly figures of some imaginary place. You will have to read these figures into an array of integers.
Example: for the following sample rainfall figures:
12 30 10 5 17 15 22 12 11 16 0 21
The histogram drawn will be:
************
******************************
**********
*****
*****************
***************
**********************
************
***********
****************
*********************
Which is made up of a series of *’s.
closed account (1CohAqkS)
im not gud with arrays plz help me.......
http://www.cplusplus.com/forum/general/42786/


This guy has used array of number's. See how you will take help from that code.
closed account (1CohAqkS)
i have done tht but dosnt work...........plz help........
Last edited on
Post your code.
closed account (1CohAqkS)
this is my code so far.....its a bit different as the question says abt the output...


#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 */
system ("pause");

return 0;
}
closed account (1CohAqkS)
help me do this plz.....as the question says plzzzzzzzz..please
Last edited on
You don't have beg for the help. People here are always more than willing to help.

here you go:

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
#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;


std::cout << "Enter how many row's" << endl;
std::cin >> num;

for(i = 0; i < num && i < 10; i++)
{
	std::cin >> number[i];
}


for(i = 0; i < num && i < 10; i++)
{
	for(int j = 0; j < number[i]; j++)
		std::cout << "*";
	std::cout << std::endl;
}


system ("pause");

return 0;
}


A problem on this which you can try is, how you will optimize the for loops. Can you do this work in just one for loop?
Last edited on
Topic archived. No new replies allowed.