help agaiiinnnn urgenttt

plzzz help this assignment is due on sunday!! i spent a lot of time doing it!
help plzz
*Let us roll dice
Write a program that simulates the rolling of two dice. The program should use the function rand() to roll the first die and use rand() again to roll the second die. The sum of the two values should then be claculated (possible values are 2, 3, …, 12). Your program should roll the two dice 20000 times. Use an array named roll (initialised to 0’s) to tally (to record the number of occurences) each possible sum value. Calculate the relative frequency of each sum value as follows: devide the sum value by 20000, multiply by 100 and floor the result. Save the results in an array named frequency. Print the array frequency as a histogram of stars.

Sample run:


1
2
3
4
5
6
7
8
9
10
11
  #include <iostream>

using namespace std;

int main ()
{
    int results[12] = {0,0,0,0,0,0,0,0,0,0,0,0};
   

    return 0;
}


thats what i did :') help plz :')
Montecarlo simulation:

The program should use the function rand() to roll the first die and use rand() again to roll the second die.


1
2
die1 := rand() % 6 + 1
die2 := rand() % 6 + 1


The sum of the two values should then be claculated (possible values are 2, 3, …, 12).


sum := die1 + die2

Your program should roll the two dice 20000 times


1
2
3
4
FOR 0..20000:

    die1 := rand() % 6 + 1
    die2 := rand() % 6 + 1


Use an array named roll (initialised to 0’s) to tally (to record the number of occurences) each possible sum value


roll[sum-1]++

Calculate the relative frequency of each sum value


freq := (roll[value] / 20000) * 100
Last edited on
how can i print it as a histogram ??


**
****
*****
******
*******
******
*****
****
***
**
Interesting, I had a similar assignment last year in a Java course, which involved tracking 6000 rolls of two dice, and counting the frequency of the sum and combination of each roll. Just as in Smac89's example, I used die1 and die2 to get random numbers of 1-6, and summed them to get the roll totals. Similarly, I also used an array to track the frequency of each roll sum and combination. I'm unsure about the histogram part, but getting the frequency of each roll total and storing it in the array should be a good first step.
i still dont have any idea how to do this :')
If you can do your histogram horizontally as you've shown, it should be pretty straightforward:

1
2
3
4
5
6
7
8
9
10
11
12
//start with your array of resultFrequencies
//your previous code will have populated it at some point
//do the following to print a histogram

for (int row = 0; row < SIZE_OF_RESULT_ARRAY; row++)
{
    for (int col = 0; col < resultFrequencies[row]; col++)
    {
        //print an asterisk
    }
    //print a newline
}
ughhhh im bad at this !! its not working :(
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
# include <iostream>
# include <cstdlib>

using namespace std;

int main()
{
    int die1;
    int die2;
    int rand();
    int sum = 0;
    int freq = 0;
    int value = 0;
    int roll[12] = {0,0,0,0,0,0,0,0,0,0,0,0};

    die1 = rand() % 6 + 1;
    die2 = rand() % 6 + 1;
    sum = die1 + die2;

    for (int i=0; i< 20000; i++)
    {
        die1 = rand() % 6 + 1;
        die2 = rand() % 6 + 1;

        value = roll[sum-1]++;
    }
    freq = (roll[value] / 20000) * 100;

    for (int row = 0; row < roll[sum-1]; row++)
{
    for (int col = 0; col < freq[row]; col++)
    {
        cout << " * ";
    }
    cout << "\n";
}

    return 0;
}
You have to try to understand what the requirements to this are. The instructions are quite straightforward.

http://www.investopedia.com/terms/m/montecarlosimulation.asp

Using the montecarlo method, simulate rolling 2 dies and calculate the percentage of the sums of each roll. Display the percentages in the form of a bar graph where each bar is a line of stars

- Run a loop 20000 times
- Each loop will simulate rolling 2 dice
- Increment the index at the sum of the dice rolls using an array called rolls
- After the 20000 runs, go through the rolls array and calculate the percentage of each sum by dividing the number in that index by 20000 and multiplying by 100
Last edited on
Actually, it looks like you were very close, layanM. Your formula is backwards, resulting in the counts for each star's output being zero. Other than that, you pretty much had it, although you forgot the random number seed, which ensures your numbers really are random.
Check this out:
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
# include <iostream>
#include <windows.h>
#include <time.h>
using namespace std;
int main()
{
    int die1=0,die2=0,sum=0,roll=20000;
    int freq[12]={0};//frequency totals

    srand(time(NULL));//set random number seed

    //20000 rolls
    while (roll>0)
    {
    //simulate die roll
    die1=rand()%6+1;
    die2=rand()%6+1;
    sum=die1+die2;//sum roll
    freq[sum-1]++;//increment count
    roll--;
    }

    //output frequency as a histogram
    for (int i=1;i<12;i++)
    {
        //output stars to represent percentages 
        for (int s=(freq[i]*100/20000);s>0;s--)
            cout<<"*";
        cout<<endl;
    }
     return 0;
}

**
*****
********
**********
*************
****************
*************
***********
********
*****
**

IF you divide by 20000 first, and then multiply by 100 to get your percentage, you will get no output because these are integers and all decimal values round to 0 before being multiplied by 100. Even in this case, the percentages are rounded because there's only about 94 stars. If you change the code to output roll totals, you will see there is a difference in rolls each time, but the histogram will nearly always look the same, with the most common rolls in the middle because there are more combinations equaling those totals.

At any rate, it looks like some minor variable name changes, and the addition of a second array that holds these percentage values that should be added to the last loop would meet the requirements. I've already got the key differences figured out, but this should get you moving in the right direction. If you're tearing your hair out unable to figure out what to change to meet all requirements, I'll show you what I came up with later. (BTW, there was nothing wrong with most of your code, I just wrote it in my own style.)
Last edited on
thnk u soo muchh !!
Topic archived. No new replies allowed.