Im stuck Need help C Write a complete C program that will si


I\'m stuck. Need help.
C++
Write a complete C++ program that will simulate the throwing of 2 dice: The program will ask the user to choose a number in the range of 2-12 (possible values for 2 dice). The program will then simulate rolling the 2 dice until the user\'s number is found and keep track of how many throws it took to get that number. In order to get a more accurate number of throws, the experiment will be repeated a number of times. So, the user must also enter how many times they want to run the experiment. Specifically, Ask the user for the number that they would like to roll. Use a loop to make sure that the user\'s response is in the 2-12 range. Give an error message if an invalid number is entered, and then repeat the input until a valid value is entered. Ask the user how many times they would like to run the roll counting experiment. Use a loop to make sure that the user\'s response is at least 1. Give an error message if an invalid number is entered, and then repeat the input until a valid value is entered. To run the experiment: get 2 random dice rolls and add their values together in a loop, count how many rolls it takes to get the user\'s target value Repeat the count rolling experiment, keeping track of the total number of rolls, until the number of repetitions value has been reached. Write out the number of rolls for each time the experiment is run, and the average number of rolls that it took to find the user\'s number. (see the examples below for a suggested format). Comments: Block at beginning of file must contain: name, lab section, program number. sample runs: Enter a number between 2 and 12: 8 How many tries do you want? 5 found 8 on roll 1 found 8 on roll 12 found 8 on roll 7 found 8 on roll 15 found 8 on roll 1 In 5 tries, found 8 in an average of 7.2 rolls Enter a number between 2 and 12: 3 How many tries do you want? 9 found 3 on roll 8 found 3 on roll 9 found 3 on roll 14 found 3 on roll 64 found 3 on roll 3 found 3 on roll 16 found 3 on roll 10 found 3 on roll 8 found 3 on roll 21 In 9 tries, found 3 in an average of 17.0 rolls

Solution

DiceRoll.cpp

#include <iostream>
#include <cstdlib>

using namespace std;

int main(){
  
   int num, tries;
   int dice1, dice2, sum, total_rolls=0;
  
   //Input number   
   cout<<\"Enter a number between 2 and 12: \";
   cin>>num;
  
   //Loop till user inputs a correct number
   while(num < 2 || num > 12){
       cout<<\"Invalid number. Enter a number between 2 and 12: \";
       cin>>num;
   }
  
   //Input number of tries
   cout<<\"How many tries do you want? \";
   cin>>tries;
  
   //Loop for number of tries
   for(int i=0;i<tries;i++){
       int rolls = 0;
       sum = 0;
       //Loop till sum of two rolls is not equal to num
       while(sum!=num){
           //Generate random number between 1 and 6 for both the dices
           dice1 = rand()%6 + 1;
           dice2 = rand()%6 + 1;
           //Increment rolls after each successful roll
           rolls++;
           //Sum the result of two rolls
           sum = dice1+dice2;
       }
           //Add rolls to keep track of total rolls made
       total_rolls+=rolls;
       //Printing rolls for try i   
       cout<<\"found \"<<num<<\" on roll \"<<rolls<<\"\ \";
      
   }
  
  
   //Calculating average roll taken to find num
   float avg_roll= (float)total_rolls/tries;
   //Printing result
   cout<<\"In \"<<tries<<\" tries, found \"<<num<<\" in an average of \"<<avg_roll<<\" rolls\";
   return 0;
}

 I\'m stuck. Need help. C++ Write a complete C++ program that will simulate the throwing of 2 dice: The program will ask the user to choose a number in the rang
 I\'m stuck. Need help. C++ Write a complete C++ program that will simulate the throwing of 2 dice: The program will ask the user to choose a number in the rang

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site