Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 04-19-2003
Dr_Spencer
write a program that simulates the rolling of two dice. The program should use rand to roll the first die and should use rand again to roll the second die. The sum of the two values should then be calculated. [note: each die can show an integer value from 1 to 6, so the sum of the two values will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 being the least frequent sums.]. The program should roll two dice 36,000 times. Use a single-subscripted array to tally the numbers of times each possible sum appears. Print the result in the tabular format. Also, determine if the total are reasonable (i.e., there are six ways to roll 7, so approximately one sixth of all the rolls should be 7).
• 04-19-2003
Jamsan
• 04-19-2003
alpha
code?

• 04-19-2003
Dr_Spencer
the main thing is I didnt understand the question and also I m having trouble with C++ array . Please do help me to write this program. I dont need the complete program but if u show me the door I can pass through that. Please.
• 04-19-2003
Jamsan
shows filling an array, random numbers, and accessing different portions of an array

Code:

```#include <iostream.h> #include <time.h> #include <stdlib.h> int main (void) {         srand(time(NULL));         int array[10] = { 0 };         for (int i = 0; i < 10; i++)                 array[i] = rand() % 6 + 1;         cout << "The Array\n";         for (i = 0; i < 10; i++)                 cout << "    " << array[i] << endl;         cout << endl;         return 0; }```
• 04-19-2003
Stoned_Coder
seed rand. num generator ( erm... srand() ring any bells)
make an array with enough elements for each possible dice score. This is the counter.
for loop 36000 times ( remember 36000 is bigger than 32767:D )
{
roll two dice ( use rand()%something+somethingelseyouworkout)
add 1 to the corresponding array element
}
print array
exit

now go code it
• 04-19-2003
The Dog
You'll need a function to roll a dice. If you think about it, all that it must do is return an integer from 1-6. (simple enough)

Now you have a function that will be used for both die.

Declare an array with a capacity of 12 elements (HINT: an int only goes up to 32767 ( on older compilers, but nevertheless )

Now all you need to do is roll the die inside a loop, and increment the value for the correct element in the array. ( think a little bit harder )
• 04-19-2003
Stoned_Coder
dont need 12 elements only 11. you cant roll a 1 and wasting an element is well just wasteful:p
• 04-19-2003
The Dog
>> dont need 12 elements only 11. you cant roll a 1 and wasting an element is well just wasteful

True. I think that it would be better with 13 ( Yes 13, it's what I meant to say before ). With 13 elements, you'll be able to use the sum of two die in as the index easily.
• 04-19-2003
Stoned_Coder
is that worth it to do away with a subtraction my 3 year old daughter could do?
• 04-19-2003
Dr_Spencer
I m dumb in array
I am really dumb in array chapter.............however I study or try I cant work..... :( This array will lower my grade. I am unable to work I have no idea what I am suppose to do. :( ;(
• 04-19-2003
Stoned_Coder
well post your effort and we will help fix it. but then again i dont suppose for one minute you have made an effort. I spelt it out almost in words of 1 syllable, jamsan posted some code for you to see how random nums and arrays work. whats the problem?
laziness?
• 04-19-2003
Stoned_Coder
look ill be slightly lenient.....

assume unsigned int counter[11]={0}

for(36000 times)
{
++counter[ (rand()%11+2)-2];
}

rand%11 is rand num in range 0-10
rand%11+2 then is range 2-12

the -2 makes the dice tally with the array element.

i.e. num of 2's is in counter[0]
num of 3's is in counter[1]
etc.
• 04-19-2003
Dr_Spencer
:(
:( ok i will try to get it .......... but still this is going to break my brain..........i wused to do other work I dont know why I am sucking in array that badly.
• 04-19-2003
Stoned_Coder
that was only shown as an example. You WILL want TWO variables for random numbers to ensure correct distribution as in 2 and 12 coming up much less frequently than 6 or 7
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last