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

  • SEARCH
  • Home
  • 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 8042797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:42:23+00:00 2026-06-05T04:42:23+00:00

I am doing some c++ practice and trying to write a program to count

  • 0

I am doing some c++ practice and trying to write a program to count the amount of times a dice combination is rolled after 10000 attempts. I have used a 2D array to store every possible dice combination, and I perform 10000 rand()%6+1 and increments the value in the memory allocation it randoms.

This is my attempt.

cout << "\nDice roll analyser" << endl;

const int first = 6;
const int second = 6;
int nRolls[first][second];
int count = 0;

while (count < 10000){
    nRolls[rand()%6+1][rand()%6+1]+=1;
    count++;
}

for (int i=0;i<first;i++){
    for (int j=0;j<second;j++){
        cout << nRolls[i][j] << " ";
    }
}

This is the output that I get;

0 0 0 0 0 0 0 269 303 265 270 264 228 289 272 294 290 269 262 294 303 277 265 294 288 266 313 274 301 245 317 276 292 284 264 260

What I am trying to achieve is the amount of times each combination is rolled e.g. how many times 1, 6 is rolled etc.

  • 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-05T04:42:24+00:00Added an answer on June 5, 2026 at 4:42 am

    You never update your count.

    For something where you want to run a code segment n times, where right now n = 10000, this is the general way you wanna do it.

    for (int i = 0; i < 10000; ++i)
    {
        //loop code.
    }
    

    additionally, myVariable+=1 can always be simplified to either ++myVariable or myVariable++ (if you aren’t use the value of myVariable right when you are assigning it, it is better to use the first one. More info on pre/post increment can be found here: http://gd.tuwien.ac.at/languages/c/programming-bbrown/c_015.htm

    so instead of nRolls[rand()%6+1][rand()%6+1]+=1;
    you can instead do

    ++(nRolls[rand()%6+1][rand()%6+1]);
    

    Additionally, arrays are zero-indexed, meaning when you do rand()%6+1 you are restricting the values from 1 to 6 and leaving out the 0 position of an array, which is the first one, so consider instead just using

    ++(nRolls[rand()%6][rand()%6]);
    

    then, to find out how often you roll a (i,j), where i and j are between 1 and 6,

    cout << "(" << i << "," << j << "):" << nRolls[i-1][j-1] << endl;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing some programming practice right now by trying to sort a 2D array
im doing some practice sql problems one of the questions is: Prepare a shipping
While doing some random experimentation with a factorial program in C, Python and Scheme.
After doing some research on the subject, I've been experimenting a lot with patterns
For practice, I'm trying to do some stuff in Python. I've decided to make
I am doing some refactoring of my app and am trying to use best
I was doing some hands-on with the Unix sed command. I was trying out
I've been trying to learn embedded software development for some time (been doing software
I am just doing some practice from one of my books, and I was
I'm doing a practice project in PHP (making a forum), and trying do decide

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.