473,385 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Write a C++ program to roll 3 dice, show the values of these dice, and show the total

Please I need help with this.
Write a C++ program to roll 3 dice, show the values of these dice, and show the total value of these dice.?

I actually did this but I am very sure it is wrong.

Expand|Select|Wrap|Line Numbers
  1.  #include <iostream>
  2.    #include <cstdlib>
  3.    #include <time.h>
  4.  
  5.   using namespace std;
  6.  
  7.  int rollDice();
  8.  
  9.  int main(int argc, char* argv)
  10.   {
  11.       int numRolls;
  12.        int arrRolls[6];
  13.        int dieRoll;
  14.  
  15.        srand((unsigned)time(0));
  16.  
  17.       cout << "How many times would you like to roll the dice?" << endl;
  18.        cin >> numRolls;
  19.  
  20.      for (int k = 0; k < 6; k++)
  21.       {
  22.            arrRolls[k] = 0;
  23.        }
  24.  
  25.        for (int i = 1; i <= numRolls; i++)
  26.       {
  27.           dieRoll = rollDice();
  28.          cout << "Die Roll " << i << ": " << dieRoll << endl;
  29.           arrRolls[dieRoll - 1]++;
  30.       }
  31.  
  32.      for (int j = 0; j < 6; j++)
  33.      {
  34.            cout << "The number " << j + 1 << " was rolled " << arrRolls[j] << " times." << endl;
  35.        }
  36.  }
  37.  
  38.    int rollDice()
  39.    {
  40.        int result;
  41.  
  42.        result = (rand() % 6) + 1;
  43.  
  44.        return result;
  45.    }
Feb 27 '10 #1
4 6504
whodgson
542 512MB
hmmm...think that the three dice should be rolled simultaneously.
1. So you want to generate three random integers with values between 1 and 6 inclusive.
2. Output required is the value of each die and the sum of the three dice.
Can you do 1.? (not sure I can) but 2. should then be pretty simple.
BTW what happened when you compiled and ran your program ... any warnings or errors?
Feb 27 '10 #2
Banfa
9,065 Expert Mod 8TB
Line 42

result = (rand() % 6) + 1;

Is a very poor way to obtain a random result between 1 - 6 because the low bits of the standard library random number generator are often not very random but this relies solely on the low bits.

Read this
Feb 27 '10 #3
@whodgson
This was the ouput when I compiled it. That doesn't look like what we were told to do. Help me with the second part, I would try to figure out the first part.

How many times would you like to roll the dice?
3
Die Roll 1: 1
Die Roll 2: 2
Die Roll 3: 1
The number 1 was rolled 2 times.
The number 2 was rolled 1 times.
The number 3 was rolled 0 times.
The number 4 was rolled 0 times.
The number 5 was rolled 0 times.
The number 6 was rolled 0 times.
Press any key to continue . . .
Feb 27 '10 #4
@Banfa
Okay!Thank you. I have read it. I will try to do it over again and see how it comes out.
Feb 27 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Maroon | last post by:
Hi, I want to write a standard java user defined function, like this example:- select db_fun("roll"), roll from student; **db_fun() will work for all tables of the all databse here db_fun is...
18
by: jacob navia | last post by:
In C, we have read-only memory (const), read/write memory (normal data), and write only memory. Let's look at the third one in more detail. Write only memory is a piece of RAM that can only...
6
by: tigrfire | last post by:
I've been working on a program to try and play a game of Craps, based on a version I found elsewhere - I didn't code the original, but I added a few things such as a balance and wager system. I'm...
11
by: Tomi Lindberg | last post by:
Hi, I'm trying to find a way to calculate a distribution of outcomes with any combination of dice. I have the basics done, but I'm a bit unsure how to continue. My main concern is how to make...
26
by: mwt | last post by:
Hello. Today I wrote my first program in C. It adds up the elements in an array. I am just beginning to learn this language. Any tips or pointers about better ways to write/structure/format/etc....
4
by: dpp4669 | last post by:
I need help with writing a program to use randmax and the standard deviation when u roll the dice 10000 times I know you will need the standard dev. and the average where do i put the randmax and do...
8
by: coolindienc | last post by:
Now I know this one should have no errors. BUT there is something about it that when I try to run the program it does not run. Any suggestions????? Andy import random class rolldice: ...
2
by: myburt | last post by:
Hi im writing a very "simple" program in C# for a console app.. I'm supposed to make a program that asks a user how many dice to roll, rolls a six sided die that many times, and returns the total...
38
by: d0ugg | last post by:
I'm writing a program that will have to roll the dice A and dice B one million times, and calculate the percentage of times that the dies will be equal. I'm having trouble to figure out how the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.