Need help with input a number of random numbers to generate and to output a chart showing the random numbers in a histogram.

Write a program to the following specifications to input a number of random numbers to generate and to output a chart showing the random numbers in a histogram, scaled to fit the screen.
• Prompt for and input a number of random numbers to generate.
• Each random number should be between 0 and 20, inclusive, generated by starting with a value of 9 or 10 (with a 50% chance of each) and, for 9 iterations, checking a probablity of 50% to determine whether to add one or subtract one.
• When the array is loaded, the program should keep track of the number with the maximum number of occurrences.
• The chart should be printed as a histogram, with up to 75 asterisks, scaled by dividing the number represented by the row of asterisks by the maximum number of occurrences for any number.
• The program must be well-documented.
• The program must use functions appropriately.
• Example run:
How many numbers? 3000
0*
1**
2*******
3*****
4**********************
5********************
6**************************************************
7******************************************************
8***********************************************************************
9************************************************************************
10*****************************************************************
11***************************************************************************
12******************************************************
13**************************************************
14*********************
15*******************
16*****
17*****
18*
19*
maximum is 383 and each * is 5
• Example run:
How many numbers? 1000000
0*
1*
2******
3******
4**********************
5**********************
6**************************************************
7**************************************************
8***************************************************************************
9***************************************************************************
10***************************************************************************
11***************************************************************************
12**************************************************
13**************************************************
14**********************
15**********************
16******
17******
18*
19*
maximum is 123646 and each * is 1648




Here is what I have so far:

#include <iostream>
using namespace std;

int main(){
int numbers; //variable for numbers to generate
int random[20]={0} ; //array of random numbers
int cell=0;
cout << "How many numbers?" ; //prompt for numbers
cin >> numbers; //input numbers
srand(time(0));
for(int i=0; i<=20; i++){
for(int i=0; i<9; i++){
random[cell]=rand()%2;

}
cell++;
}
int index=0;
for (index=0; index<20; index++)
cout << index << " :: " << random[index] << endl;
}


I need help with printing out * beside the numbers. like in sample run.
Thank you for you help.
Looks to me like a bell curve. I guess you could cheat and use c++11's normal_distribution, but otherwise I dunno.

EDIT: Try this, it looks fine to 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
int niz[20]={0}, n;

void populate()
{
     int start;
     for (int i=0, j; i<n; i++)
     {
         start=9+(rand()%2);
         for (j=0; j<9; j++)
         {
             rand()%2?start++:start--;
             }
         niz[start]++;
         }
     }

int main()
{
    cin>>n;
    srand(time(0));
    populate();
    int maks=*max_element(niz, niz+20);
    int m=max(maks/75, 1);
    for (int i=0; i<20; i++)
    {
        cout<<i<<string(niz[i]/m, '*')<<endl;
        }
    cout<<"maximum is "<<maks<<" and each * is "<<m<<endl;
    system("pause");
}
Last edited on
works fine. thank you ..i messed up on algorithm.
Topic archived. No new replies allowed.