Stuck on C++ Project

First...this is a basic C++ class for Electrical Engineers. The project states, generate a set of random numbers, seperate them into categories (i.e. 1-20 and 21-40), calculate the mean on the values and plot the results in a histogram. Everything is done except the histogram. My professor keeps saying you need to force the computer to output symbols, instead of actual numbers. The histogram should look like this:

0-20: ******
21-40: ***
41-60: ******
61-80: *********
81-100: **

Here is the program, just need help converting my numbers to symbols. Thanks.

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;


int main()
{
int test=0;
int data=0;
int count=0;
int sum=0;
int mean=0;
int rn120=0;
int rn2140=0;
int rn4160=0;
int rn6180=0;
int rn81100=0;
srand(time(0));



do
{
cout<<"Please enter the number of test values (1-1000):\t";
cin>>test;
}
while (test>1000);



do
{

data=rand()%100+1;

if(data>=1 && data<=20)
rn120++;
else if (data>=21 && data<=40)
rn2140++;
else if (data>=41 && data<=60)
rn4160++;
else if (data>=61 && data<=80)
rn6180++;
else if (data>=81 && data<=100)
rn81100++;

cout<<data<<"\t";

sum+=data;
count++;

}
while (count<test);

mean=sum/test;

cout<<"\n\n\n";
cout<<"The sum of the test values is: \t"<<sum<<"\n";
cout<<"The mean of the test values is:\t"<<mean<<"\n";


cout<<" 0-20:"<<rn120<<"\n";
cout<<" 21-40:"<<rn2140<<"\n";
cout<<" 41-60:"<<rn4160<<"\n";
cout<<" 61-80:"<<rn6180<<"\n";
cout<<"81-100:"<<rn81100<<"\n";




system("pause");
return 0;
}
First of all when you post code you need place your c++ code between the code tags. To do that clink on the # symbol. This makes the code readable.
Regarding the format question, I think you need to print ** symbols instead of the number
for example if rn120=5, you need to show five *s
1
2
3
4
5
cout << "0-20:"
for(int i=0;i<rn120;i++)
      cout <<"*";
cout<<endl;
Couple things.

1. Format your histogram labels so that the base of the histogram lines up.

2. Use an array (or better, a vector) for your histogram bins.

2. You'll probably need to scale your histogram. If you take a thousand samples, The size of a 1:1 scale histogram is going to be way too big to display on the console. For example, with a thousand samples being placed into 5 bins, assuming roughly even distribution, you'll have 200 samples per bin, or histogram bars ~200 asterisks long. If you scale the histogram by a factor of 10, then the histogram bars will be ~20 asterisks long.

Of course, if your sample size is only 100 samples, that's roughly 20 samples per bing, and with a scale of 10, that's only 2 asterisks per bin.

So, you need a sliding scale, based on the histogram size. After taking all the samples, and filling all the bins, find the largest bin size. Once you have that largest bin, calculate a scale factor that will give you the optimally sized histogram. The easiest way to do this is to divide the largest bin by your optimal histogram size (say, max bar size is limit to 30). Round that value UP to the largest whole number, and that's your scale factor.

Here's some sample code that generates Craps die rolls for reference:
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
vector<int> histogram_bins(11, 0);
int num_samples, rand_num, largest_bin, scale_size, counter;
string actvals = "Count ";
string dienum = "Roll";

//Prompt for sample size
do{
	cout << "Input number of die rolls to generate (100-10,000): ";
	cin >> num_samples;

	if(num_samples < 100 || num_samples > 10000){
		cout << "Incorrect Number of samples";
		num_samples = 0;
	}
}while(0 == num_samples);

//Generate random craps rolls and stash in appropriate bins
for(int x = 0; x < num_samples; x++){
	//roll the dice
	rand_num = (rand() % 6 + 1) + (rand() % 6 + 1);
	histogram_bins[rand_num-2]++;
}

//Find the largest_bin
largest_bin = 0;
for(unsigned int x=1; x < histogram_bins.size(); x++)
	if(histogram_bins[x] > histogram_bins[largest_bin])
		largest_bin = x;

//Determine scale factor
//We want the largest bar to be no more than 30 asterisks
//integer division truncates, so we add 1 to effectively round up
scale_size = histogram_bins[largest_bin]/30 + 1;

cout << "Scale = " << scale_size << endl << endl;

//Now we create our histogram
cout << actvals << dienum << "     Histogram" << endl;
cout.setf(ios::left);
for(unsigned int x = 0; x < histogram_bins.size(); x++){
	cout.width(actvals.size());
	cout << histogram_bins[x];
	cout.width(dienum.size());
	cout << x+2 << ":";
	counter = 0;
	while(counter++*scale_size < histogram_bins[x])
		cout << "*";
	cout << endl;
}
First...this is a basic C++ class for Electrical Engineers. The project states, generate a set of random numbers, seperate them into categories (i.e. 1-20 and 21-40), calculate the mean on the values and plot the results in a histogram. Everything is done except the histogram. My professor keeps saying you need to force the computer to output symbols, instead of actual numbers. The histogram should look like this:

0-20: ******
21-40: ***
41-60: ******
61-80: *********
81-100: **

So presumably you have five arrays such as a0_20[] with (in this case ) 6 elements holding a random number with values between 0 - 20. If this is the case then you would use a for loop to cycle through the elements and print a star and at the end a new line..
e.g.
1
2
3
 for(int i=0;i<a0_20[].sizeof();++i)
        cout<<'*'<<" ";
cout<<endl;

Repeat for the other ranges.
Am not sure why the mean of each range is required or why your professor is being so liberal as to allow you to print the histogram horizontally......or have I missed something.
Topic archived. No new replies allowed.