Hi all! I need a little help with a problem. I've been instructed to write a dice rolling program that uses rand to roll dice one and uses rand again to roll dice two. I'm then to record the values of the two faces in a two-dimensional array. The dice are rolled 36,000 times and the output is supposed to look like this:

Die2 Die 1
1 2 3 4 5 6
1 1010 1046 1004 983 1022 1074
2 985 1054 993 980 1002 1055
3 1023 1002 998 939 1067 990
4 972 1017 966 1034 982 989
5 954 1007 977 1052 949 997
6 1008 962 1013 948 977 969

I know the spacing's not right but I think you get the point. I have most of the code written but I can't figure out how to print the answers (if I've done it right, that is) in this fashion. Any help would be appreciated!! Here's the code:

``````#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;

int main()
{
int die1;
int die2;
int faces[5][5];

for (int i = 0; i < 5; i++)
faces[i][i] = 0;

for (int i = 0; i < 36000; i++)
{
srand(i * (unsigned int)time(0));

die1 = 1+rand() % 6;
die2 = 1+rand() % 6;

}
faces[5][5] = faces[die1][die2];

cout<< "\n   1	2	 3	 4	5	 6";
cout<<"\n 1";
cout<<"\n 2";
cout<<"\n 3";
cout<<"\n 4";
cout<<"\n 5";
cout<<"\n 6";

for(int i = 0; i < 5; i++)
cout<< setw(6)<< faces[i][i];
cout<<endl;

return 0;

}``````

Thanks!

3
Contributors
2
Replies
4
Views
8 Years
Discussion Span
Last Post by vmanes

setw( ) is your friend for getting columns lined up nicely.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.