Funct with rand + main funct with loop+histogram

Please help
* Student ID : Your student ID #.20129121 , Due date : April 15, 2015, Create function that produces a random number..Create a main function with a loop and shows histograms

Section 1 Create a function “int roll()” that returns a value from 1 to 5.

Section 2 Main Function. Set the random number seed with 1 followed by last 4 digits of your student ID.

Section 3 Call "roll" function 200 times while keeping count of each number. (You are required to use an array)

Section 4 Show the results, and display a histogram.

Section 5 Call "roll" function 8 times, and add those numbers

Section 6 Repeat "Section 5" 1,000,000 times while keeping count of each number. (You are required to use an array)

Section 7 Show the results, and display a histogram. This time each "*" represents 2000./*

output example:
1: 30: ******************************
2: 40: ****************************************
3: 45: *********************************************
4: 36: ************************************
5: 49: *************************************************
 
8: 1: *
9: 15: *
 10: 106: *
 11: 294: *
 12: 828: *
 13: 2015: **
 14: 4112: ***
 15: 8142: *****
 16: 13944: *******
 17: 22440: ************
 18: 33426: *****************
 19: 46892: ************************
 20: 61240: *******************************
 21: 75027: **************************************
 22: 87266: ********************************************
 23: 95331: ************************************************
 24: 97483: *************************************************
 25: 95145: ************************************************
 26: 86909: ********************************************
 27: 75659: **************************************
 28: 61257: ********************************
 29: 46917: *************************
 30: 33497: ******************
 31: 22583: *************
 32: 14090: *******
 33: 8041: *****
 34: 4102: ***
 35: 2007: **
 36: 817: *
 37: 311: *
 38: 83: *
 39: 17: *
 40: 3: *
Last edited on
What part do you have problem with?

the entire thing. i can't get it to sort like that on the side. im in the C++ class online and the teacher is terrible. he wont answer emails and or have office hours. heres what i have thus far

/*
Student ID : Your student ID #.20129121
Last four digts 9121
*/
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<fstream>
#include<string>
using namespace std;

//function prototype
int roll();
int main ()
{

unsigned int id=19121;
//create a random seed of last 4 digits of student id followed by 1
srand(id);

//array to hold five random numbers
int arrCount[6]={0};

//call roll function 200 times
//and count corresponding value in array
for(int index=0;index<200;index++)
arrCount[roll()]++;


//print histogram number of times each value occures in the arrCount
for(int index1=1;index1<=5;index1++)
{
for(int index2=0;index2<arrCount[index1];index2++)
cout<<"*";

cout<<"."<<endl;
}


//calling roll function 8 times and adding those values in totalSum
int totalSum=0;
for(int index1=0;index1<8;index1++)
totalSum=+roll();

cout<<"Total sum of adding roll for 8 times "<<totalSum<<endl;

//create an array of size 5
int arrCount2[6]={0};

//call roll function for 1000000 times
for(int index=0;index<1000000;index++)
//call roll function and increment the corresponding value in array
arrCount2[roll()]++;


//print histogram number of times each value occures in the arrCount2
for(int index1=1;index1<=5;index1++)
{
//Get quotient of each count value at index1 by divididing by 2000
int count=arrCount2[index1]/2000;

//print the starts fro
for(int index2=0;index2<count;index2++)
cout<<"*";

cout<<"."<<endl;
}


//pause the program output on console
system("PAUSE");
return 0;
}//bottom of main

//Returns a random number
int roll()
{
srand(time(NULL));
return rand()%5+1;

}

the output comes out funky. i can't paste it in here for some reason
Please, use the code tags. See http://www.cplusplus.com/articles/jEywvCM9/

Re-read "Section 6".
that did not really help me. is there anything else you could tell me?
Section 5: array of 200 ints, each int represents a single roll()

Section 6: array of 1000000 ints, each int represents the sum of 8 roll()s
Perhaps we read differently
Section 6 Repeat "Call roll function 8 times, and add those numbers" 1,000,000 times while keeping count of each number. (You are required to use an array)

Section 7 Show the results, and display a histogram. This time each "*" represents 2000./*


You also make an elementary mistake on line, whose number I don't know due to the lack of use of the code tags. The issue relates to the purpose of the "seeding" of the random number generator.
so far I got this

# include <iostream>
#include <stdlib.h>
using namespace std;

//////////////Section 1///////////////
int roll();

void main()
{
////////////Section 2/////////////
cout << "Please enter the last four digits of your Student ID" << endl;
int ID = 0;
cin >> ID;

srand(10000 * ID);

int Array[7];
for (int i = 0; i < 7; i++)
{
Array[i] = 0; //Cleaning array
}

//////////////Section 3////////////////
for (int i = 0; i < 200; i++)
{
int Random = roll();
Array[Random - 1]++;
}

//////////////Section 4////////////////
for (int i = 0; i < 7; i++)
{
cout << i + 1 << " : " << Array[i] << " : ";
for (int j = 0; j < Array[i]; j++)
{
cout << "*";
}
cout << endl;
}

//////////////Section 5////////////////
int Random = 0;
for (int i = 0; i < 6; i++)
{
Random = Random + roll();
}

//////////////Section 6////////////////
int BigArray[42];
for (int i = 0; i < 42; i++)
{
BigArray[i] = 0; //Cleaning array
}
int BigRandom = 0;
for (int i = 0; i < 1000000; i++)
{
for (int j = 0; j < 6; i++)
{
BigRandom = BigRandom + roll();
}
BigArray[BigRandom - 1]++;
}

//////////////Section 7////////////////
for (int i = 5; i < 41; i++)
{
cout << i + 1 << " : " << BigArray[i] << " : ";
for (int j = 0; j < BigArray[i] / 2000; j++)
{
cout << "*";
}
cout << endl;
}
}

int roll()
{
int Random = rand() % 7 + 1;
return Random;
}


but i missed to many things
I do not Know where
Please edit your post and make sure your code is [code]between code tags [/code] so that it has syntax highlighting and line numbers, as well as proper indentation.
http://www.cplusplus.com/articles/jEywvCM9/
ok i completely changed it up and got the output the way i want it but i still have a small problem. the first set of numbers on the example output (1: - 5:) are supposed to count the number of 1s -5s for 200 rolls with a tally (*). however when i set i<6 so it returns values 1 to 5 the count only adds up to like 150 or so. if i set it to i<8 it will add up to 200. Line 28 is what im talking about. please help its due tonight.

#include<iostream>

#include<iomanip>

using namespace std;

int roll()
{
return (rand() % 7) + 1;
}


void main()
{
int ary[9] = { 0 };

int arx[43] = { 0 };
int id = 19121;
srand(id);

int i, j;

for (i = 1; i<=200; i++)
{
ary[roll()]++;
}

for (i = 1; i < 8; i++)
{
cout << setw(1) << i << ":" << setw(6) << ary[i] << ':';

for (j = 0; j<ary[i]; j++)
{
cout << "*";
}
cout << endl;
}

for (i = 0; i<1000000; i++)
{
arx[roll() + roll() + roll() + roll() + roll() + roll()]++;
}
for (i = 8; i<41; i++)
{
int j;
cout << setw(2) << i << ":" << setw(8) << arx[i] << ":";

for (j = 0; j<arx[i]; j += 2000)
{
cout << "*";
}
cout << endl;

}

here is a link to the output with i<8: https://onedrive.live.com/redir?resid=3274BC86E4F10732!1681&authkey=!AKpIVw8FicEV8E0&v=3&ithint=photo%2cpng
here is a link to the output with i<6: https://onedrive.live.com/redir?resid=3274BC86E4F10732!1682&authkey=!AILbv30Yg1KScxo&v=3&ithint=photo%2cpng
Last edited on
Please explain this:
1
2
3
4
int roll()
{
  return (rand() % 7) + 1;
}
Topic archived. No new replies allowed.