0

This is homework assignment for school, but I'm begging for someone to just correct my code. I've been working at this for two days and I think I have everything worked out except I can't get it to work as a 2D Array, so I set this up temporarily just to try to figure things out, but I'm digging myself deeper into a hole I think.

The assignment requires that two dice be rolled 36,000 times and then the results for each sum be displayed on the right, and the sum of the two dice on the left, like this in a 2D array:

12 850
11 1020
10 1200
...
2 900

I've got the right column displaying correctly, but the left column won't display the sums, it just displays "System.Int32[]" a bunch of times.

Here's the code:

Random rand = new Random();
const int ARRAY_SIZE = 13;
const double DICE_ROLLS = 36000;
int sum = 0;
int die1 = 0;
int die2 = 0;

int[] sums = new int[ARRAY_SIZE];

int[] dice = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

for (int i = 0; i < DICE_ROLLS; i++ )
{
    die1 = rand.Next(1, 7);
    die2 = rand.Next(1, 7);

    sum = die1 + die2;
    sums[sum] += 1;
}

for (int i = 2; i < sums.Length; i++)
{
    Console.WriteLine("{0,2}      {1,8}", dice, sums[i]);
}
9
  • 1
    Given that you just want the left column to have the sum index corresponding to the right column's actual sum, why bother with the array at all? Why not just write i? If you want to use the dice array, you need to index it, but to do that effectively, you'll have to initialize it so that each entry in the array is simply the value of the index for that entry, so it seems kind of silly to bother with the array at all. Commented Mar 11, 2015 at 4:01
  • I apologize, I updated the post, I got so flustered with the assignment, I read over it, a 2D array is REQUIRED unfortunately. Sorry for the confusion. Commented Mar 11, 2015 at 4:02
  • How does die throw 2 give you a sum of 900, when each die only has 6 sides? Commented Mar 11, 2015 at 4:02
  • Also, why is your array size 13? Commented Mar 11, 2015 at 4:03
  • 2
    Actually, your code really isn't very "screwed up". It's quite close to something that works. The main problem here is that you have an assignment where there clearly are some constraints preventing you from implementing any arbitrary solution, but we don't know those constraints. It's difficult to provide specific advice without knowing exactly what your teacher wants. Nevertheless, I have answered with what I hope is specific enough information to get you back on track, without outright doing your homework for you. :) Commented Mar 11, 2015 at 4:14

2 Answers 2

3

In the spirit of the homework assignment, rather than fixing the code outright, I will try to explain the parts you need to fix, and let you do the actual fixing.

The primary issue in your code is that you are trying to print dice as the value for the left column of the output, rather than individual elements of dice (e.g. dice[i]).

Note, however, that you can't just use dice[i], because your dice array has fewer elements in it than the sums array. If you just replaced dice with dice[i] in your WriteLine() statement, you'd get an index-out-of-bounds exception.

IMHO, the best way to address this is to initialize sums with 11 elements instead of 13, and then when tracking the sums (i.e. in your first loop), subtract 2 from the actual sum value to get the index for the sums array:

sums[sum - 2] += 1;

Then in your second loop, you can safely just use i to index both arrays.

I hope that helps. Please feel free to ask for any clarifications and good luck with your assignment.

Sign up to request clarification or add additional context in comments.

Comments

1

Since you'll be using a 2d array you'll want to do something like this:

var rand = new Random();
const double diceRolls = 36000;

var sums = new[,] {{2, 0}, {3, 0}, {4, 0}, {5, 0}, {6, 0}, {7, 0}, {8, 0}, {9, 0}, {10, 0}, {11, 0}, {12, 0}};

for (var i = 0; i < diceRolls; i++)
{
  var die1 = rand.Next(1, 7);
  var die2 = rand.Next(1, 7);

  var sum = die1 + die2;
  sums[sum - 2, 1] += 1;
}

for (var i = 0; i < sums.GetLength(0); i++)
{
  Console.WriteLine("{0,2}      {1,8}", sums[i, 0], sums[i, 1]);
}

Note 1: sum - 2. Since the array length is only 11 you need to subtract 2 from the dice value. (0-10 instead of 2-12).

Note 2: sums.GetLength(0). If you use sums.Length you'll get 22 since there actually are 22 elements in the array. You need to get the length for rank 0

Note 3: Since you're dealing with 2d arrays you'll have the sum of the dice roll in sum[i, 0] and the total count in sum[i, 1].

1 Comment

Thanks, this is exactly what I needed! From comparing the codes I see exactly where I went wrong. So you gave me the answer, but I'm also learning from it! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.