Question

I just need the code in c++ by using array and random number generation Write a...

I just need the code in c++ by using array and random number generation

  1. Write a function called rollDice.

    1. This function emulates rolling two 6-sided dice

      1. Choose two random numbers between 1 and 6

      2. They should be random enough (pseudo-radom), not the same random number every time the function is run
    2. Add the numbers together and return the sum

      1. Note: it is not sufficient to merely randomly choose a number between 2 and 12, can you see why?

  2. Write a main function that rolls two dice (calls rollDice) 1000 times and uses an array to keep track of the number of times each result (2-12) occurred. Main then prints out the results

    Hint: create an array with space for 11 integers (representing results 2-12). Initialize them all to 0, then add 1 to the proper location in the array for each dice roll.
  3. Main should print out the values in the array separated by spaces all on a single line (use a for loop to accomplish this).

  4. Submit your assignment as a single CPP file containing both the main and the function.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
using namespace std;

int rollDice(){
  
   int dice1,dice2;
   dice1=rand()%6+1;
   dice2=rand()%6+1;
   return dice1+dice2;
}

int main()
{
   srand (time(NULL));
   int sum[]={0,0,0,0,0,0,0,0,0,0,0};
   for(int i=0;i<1000;i++)
   {
       sum[(rollDice())-2]+=1;
   }
   for(int i=0;i<11;i++)
   {
       cout<<sum[i]<<" ";
   }
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
I just need the code in c++ by using array and random number generation Write a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How to write python code that is a dice rolling function that generates random numbers. The...

    How to write python code that is a dice rolling function that generates random numbers. The dice rolling function takes two arguments: the first argument is the number of sides on the dice and the second argument is the number of dice. The function returns the sum of the random dice rolls. For example, if I call roll dice(6,2) it might return 7, which is the sum of randomly chosen numbers 4 and 3. It somewhere along the lines of:...

  • hey, struggling with this assignment, wondering if if I could get some help. Here is the...

    hey, struggling with this assignment, wondering if if I could get some help. Here is the project details The following lists a Dice class that simulates rolling a die with a different number of sides. The default is a standard die with six sides. The rollTwoDice function simulates rolling two dice objects and returns the sum of their values. The srand function requires including cstdlib. my class ------------------------------------ class Dice { public: Dice(); DIce(int numSides); virtual int rollDice()const; protected: int...

  • in C please, Write the code to simulate rolling three dice and find the sum of...

    in C please, Write the code to simulate rolling three dice and find the sum of the top three faces. Repeat rolling the three dice 10,000 times. Use an array called frequency to have the count of each sum. Pass this array to a function that calculates the maximum number that appears in this array. Print this maximum in main.

  • Write a program that calls the random number generator two times to simulate rolling a pair...

    Write a program that calls the random number generator two times to simulate rolling a pair of dice. It should store the two numbers into variables. It will then determine if the dice roll is a winner (doubles OR add up to 7). Remember to add two includes and also to seed the random number generator to the clock. This will roll two dice and add them together. Doubles, or a total of 7 - YOU ARE A WINNER!!! First...

  • ***Please complete the code in C*** Write a program testArray.c to initialize an array by getting...

    ***Please complete the code in C*** Write a program testArray.c to initialize an array by getting user's input. Then it prints out the minimum, maximum and average of this array. The framework of testArrav.c has been given like below Sample output: Enter 6 numbers: 11 12 4 90 1-1 Min:-1 Max:90 Average:19.50 #include<stdio.h> // Write the declaration of function processArray int main) I int arrI6]i int min-0,max-0 double avg=0; * Write the statements to get user's input and initialize the...

  • Write a program in C that creates an array of 100 random numbers from 0-99. The...

    Write a program in C that creates an array of 100 random numbers from 0-99. The program sums the random numbers and prints the sum.  It then writes the numbers to a new file using open, close and write. Then looks in the current directory for files that match the pattern “numbers.XXXX”. For each file, open the file and read the file. You can assume that the file will contain 100 integers. Sum the integers. Print the filename and the sum...

  • Write code that declares an array of integer values that will represent the first five even...

    Write code that declares an array of integer values that will represent the first five even numbers: 2, 4, 6, 8, 10. Add code to initialize each element of the array with the even number values shown above. Add code to calculate the product of the array elements, and store the result in a variable named product. You must access the array elements to accomplish this. Do not write product = 2 * 4 * 6 * 8 * 10;...

  • I need a c++ code please. 32. Program. Write a program that creates an integer constant...

    I need a c++ code please. 32. Program. Write a program that creates an integer constant called SIZE and initialize to the size of the array you will be creating. Use this constant throughout your functions you will implement for the next parts and your main program. Also, in your main program create an integer array called numbers and initialize it with the following values (Please see demo program below): 68, 100, 43, 58, 76, 72, 46, 55, 92, 94,...

  • Random Number Generation and Static Methods Write a program that simulates the flipping of a coin...

    Random Number Generation and Static Methods Write a program that simulates the flipping of a coin n-times to see how often it comes up heads or tails. Ask the user to type in the number of flips (n) and print out the number of times the coin lands on head and tail. Use the method ‘random()’ from the Math class to generate the random numbers, and assume that a number less than 0.5 represents a tail, and a number bigger...

  • 1. Write a C code that do the following: Generate array of random integer numbers of...

    1. Write a C code that do the following: Generate array of random integer numbers of size 25 Fill the array with random integers ranging from [1-100] Find the maximum number of the array and its location Find the minimum number of the array and its location Search for a number in the array Print the array Flip the array Sort this array Quit the program when the user choose to exit

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT