Hi, I have this assignment I am working on.
"Simulate the rolling of five 6-sided dice, storing each roll number in an array. Then:
Loop through the array to report this information:
The result of each roll
The total of all rolls
The number of dice that rolled above a 4."
so far I wrote..
const int size=5;
int rollingdice[size];
int totalofrolls=0;
srand (time(0));
for ( int i=0;i<size;i++)
{
rollingdice=1+rand()%6;
cout<<"The result of "<<i+1<< " roll is "<<rollingdice[i]<<endl;
//[i] I am not sure if I should change variable i in the body of the loop. The program runs and the output seems to be correct, but I read in the textbook that changing initializing variable in the body of the loop is somehow bad.
totalofrolls=totalofrolls+rollingdice[i];
}
cout<<"The total of all rolls is "<<totalofrolls<<"."<<endl;
Thank you for your answer. if I do not put <i+1> part than the i will start with 0 and go up to 4 (which is good because i use it for my array), but I also want to count number of rolls (how many times I rolled the dice, which is 5 and it should start with 1 and go up to 5)..
int rollingdice[size];
int b=1;
int total=0;
string yesanswer;
srand(time(0));
for (int i=0,b=1;i<size;i++, b++)
{
rollingdice[i]=1+rand()%6;
cout<<"Rolling the dice "<<b<< " time: "<<rollingdice[i]<<endl;
total=total+rollingdice[i];
}
cout<<"The total of all rolls is "<<total<<"."<<endl;
}
this is better I use another variable in the initialization..
I still do not know how to find out if the random num stored in array are more than 4
if statements are a great thing to do, this is a function that will do what you want, please do not copy it but the idea for what you want is the same.
1 2 3 4 5 6 7 8 9
int abovefour(int dice[], int length){// enters your array and the length of the array
int four = 0;// this is the total number of rolls above 4
for( int i = 0; i != length; i++){// runs the if statement for EACH number in array
if ( dice[i] >= 4){// says if it is >= for then it adds to the counter
++four; // same as "four = four + 1" or "four+=1" this is just my writing preference
}
}
return four; // returns the total counted number to the main function, if you do not know about functions ignore this and just focus on the for loop above
}