My assignment was to do the following:
Write a program to simulate rolling a die 100 times and to display each outcome and a histogram of the outcome. Keep a count of the number of times each of the numbers 1,2,3,4,5,6 came up and call on a function printStars to display that number of stars followed by the count as follows:
Sample output:
Rolling die 100 times:
5 1 6 4 4 1 5 1 5 2 4 1 6 5 6 5 3 2 3 2 1 3 5 2 2 5 3 4 4 5 2 5 2 4 4 3 1 2 6 1
2 2 3 3 2 3 4 5 4 1 5 3 6 5 4 4 1 5 4 4 1 4 1 6 5 2 2 3 6 4 2 5 4 5 5 3 1 1 3 2
6 5 1 6 4 4 1 1 3 6 5 3 3 4 3 3 3 6 4 4
1: * * * * * * * * * * * * * * * * 15
2: * * * * * * * * * * * * * * * 14
3: * * * * * * * * * * * * * * * * * * 17
4: * * * * * * * * * * * * * * * * * * * * * 20
5: * * * * * * * * * * * * * * * * * * * 18
6: * * * * * * * * * * * 10
Press any key to continue . . .
I have written so far half the program, where the die is rolled 100 times, but Im having trouble making the histogram function without using arrays since my professor hasn't taught it yet. This is the code I have thus 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 50 51 52 53 54 55 56 57 58 59 60 61
|
// Program to simulate rolling a die 10 times
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
unsigned seed=time(0);
int main()
{
srand(seed);
int roll,die;
cout<<"Rolling a die 100 times:"<<endl;
for(roll=1;roll<=100;roll++)
{ die=1+rand()%6;
cout<<die<<" ";
}
cout<<endl;
}
void printStars (int a,b,c,d,e,f,i)
while (cin>>i)
{
if (i==1)
a++;
else if (i==2)
b++;
else if (i==3)
c++;
else if (i==4)
d++;
else if (i==5)
e++;
else if (i==6)
f++;
else
if (i == -1 )
break;
}
cout << setw (7) << "1: ";
for (int i=0;i"*";
cout<< endl;
cout << setw (5) << "2: ";
for (int i=0;i"*";
cout<< endl;
cout << setw (5) << "3: ";
for (int i=0;i"*";
cout<< endl;
cout << setw (5) << "4: ";
for (int i=0;i"*";
cout<< endl;
cout << setw (5) << "5: ";
for (int i=0;i"*";
cout<< endl;
cout << setw (6) << "6: ";
for (int i=0;i"*";
cout<< endl;
return 0;
}
|