Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7726681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:16:34+00:00 2026-06-01T05:16:34+00:00

I have an assignment that simulates a dice game. As part of the program,

  • 0

I have an assignment that simulates a dice game. As part of the program, the user enters the number of dice to roll and the number of times to roll them. If the user rolls 4 dice, the program should sum the 4 values, store the result in an array, then redo the program the number times defined by the user. The main code and the function prototypes were defined by our tutor and cannot be amended. We have to write the function.

In Step 3 of the main, there are two for loops. The inner for loop calls the function in question. A 2D array rollSums[][] is assigned to the result of the function. This array is to be used in another function. I can’t figure out how to populate the 2D array correctly from the function. The code and my attempt at the function is below:

#include <iostream>
#include <iomanip>
#include <cstdlib> // needed for functions srand() and rand()
#include <ctime> // needed for function time()
#include <cmath> // needed for sqrt()

using namespace std;

const int MAXNUMTOROLL=10;
const int MAXROLLS=100;


int rollDice(int diceVals[], int numToRoll);

int main()
{
  int sum;
  int rollSums[MAXNUMTOROLL][MAXROLLS];
  int diceVals[MAXROLLS];
  double mean[MAXNUMTOROLL], std[MAXNUMTOROLL];
  int numToRoll, numRolls;

  srand(time(NULL));

  // STEP 1: Ask user to input the maximum number of dice to use:

  cout << "Please enter the maximum number of dice to use:" << endl;
  do 
    {
      cin >> numToRoll;
    } while (numToRoll < 0 || numToRoll > MAXNUMTOROLL); 

  cout << "Please enter the number of rolls:" << endl;

  // STEP 2: Ask user to input the number of rolls to carry out:
  do
    {
      cin >> numRolls;
    } while (numRolls < 0 || numRolls > MAXROLLS);

  // STEP 3: For k=1 to numToRoll, simulated numRolls rolls of the dice
  // and store the sum of the numbers rolled in the array rollSums[][]

  for (int k=1;k<=numToRoll;k++)
    {
      for (int i=0;i<numRolls;i++)
    {
      rollSums[k-1][i] = rollDice(diceVals, k);
    }
    }

  return 0;
}

int rollDice(int diceVals[], int numToRoll) //function simulating throwing of dice
{
 int sum=0;
 int i=0;

 for(i=0;i<numToRoll;i++)
 {
  diceVals[i]=1+rand()%6;
  sum=sum+diceVals[i];
 }
 return sum;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T05:16:36+00:00Added an answer on June 1, 2026 at 5:16 am

    adohertyd, see my comments in the code sample:

    #include <iostream>
    #include <iomanip>
    #include <cstdlib> // needed for functions srand() and rand()
    #include <ctime> // needed for function time()
    #include <cmath> // needed for sqrt()
    
    using namespace std;
    
    const int MAXNUMTOROLL=10;
    const int MAXROLLS=100;
    const bool show_debug = true;
    
    int rollDice(int diceVals[], int numToRoll);
    
    int main()
    {
        int roll_Sums[MAXNUMTOROLL];
        int diceVals[MAXROLLS];
        //double mean[MAXNUMTOROLL], std[MAXNUMTOROLL];
        int numDice, numThrows;
    
        //Initialize random number generator with the current time
        srand(time(NULL));
    
        // STEP 1: Ask user to input the maximum number of dice to use:
        cout << "Please enter the maximum number of dice to use:" << endl;
    
        // STEP 2: Validate number of dice input
        do 
        {
            cin >> numDice;
        } while (numDice < 0 || numDice > MAXNUMTOROLL); 
    
        //STEP 3: Ask user to input the number of times to throw each dice
        cout << "Please enter the number of rolls:" << endl;
    
        // STEP 4: Validate number of throws input
        do
        {
            cin >> numThrows;
        } while (numThrows < 0 || numThrows > MAXROLLS);
    
        cout << "\n\nThrowing Dice Now...\n\n";
    
        // STEP 5: Roll the dice
        //The loop deals with each dice
        for (int diceCount = 0; diceCount < numDice; diceCount++)
        {
            //The function call deals with all the throws per dice
            //Note: roll_Sums array didn't need to be two dimensional, 
            //      also, rollDice gets passed diceVals[] by value and the number of throws to execute
            roll_Sums[diceCount] = rollDice(diceVals, numThrows);
    
            //Debug output
            if(show_debug)
            {
                //Since roll_Sums is zero based, add one to the visible index so the user doesn't get confused :P
                cout << "Roll Sum for dice #" << diceCount + 1 << ": " << roll_Sums[diceCount] << endl << endl;
            }      
        }
    
        return 0;
    }
    
    //rollDice() returns the sum of all the dice rolls it performed 
    int rollDice(int diceVals[], int numToRoll)
    {
        int sum=0;
    
        for(int i=0;i<numToRoll;i++)
        {
            //Get your random dice rolls
            diceVals[i]=1+rand()%6;
    
            //Debug output
            if(show_debug)
            {
                cout << "Dice Roll # " << i+1 << ": " << diceVals[i] << endl; 
            }
    
            //Accumulate your value, e.g. "sum"
            sum += diceVals[i];
        }
    
        return sum;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have quite a large XSL document for an assignment that does a number
I currently have a school assignment that involves both PHP and asp.net. Now the
I'm working on an assignment that is telling me to assume that I have
For an assignment in college, we have to make a script in Perl that
I have an assignment in a language-independent class, and part of it is using
I am working on a short java assignment that I have been set. The
I have an assignment in which I need to create a function that tells
I have an assignment that requires us to implement a doubly linked list class.
I have an assignment that asks me to retrieve delimited text from form values.
So I have an assignment that is due later tonight and I'm stuck on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.