1

I've got a function that accepts a dynamic multidimensional array (which is initialized to 0) as a parameter, and I'm trying to modify certain values within the array in my function.

The function that accepts the array as a parameter is supposed to simulate the roll of two dice and output the frequency distribution to the array I made that's initialized to zero.

The code for it is as follows:

#include <iostream>
#include <cstdlib>

using namespace std;

int** rollDie(int numRolls, unsigned short seed, int** &rollarray)
{
srand(seed);
int side1, side2;
while (numRolls > 0)
{
side1 = 1 + rand() % 6;
side2 = 1 + rand() % 6;

rollarray[side1][side2]++;

numRolls--;
}
return rollarray;

}

int** initializeArray(void)
{
    int i, j;
    int** m = new int*[6];

    for (i = 0; i < 6; i++)
        m[i] = new int[6];

    for (i = 0; i < 6; i++)
        for (j = 0; j < 6; j++)
            m[i][j] = 0;

    return m;
}

int main()
{
int numRolls;
unsigned short seed;
int ** a = initializeArray();


cout << "rolls?\n";
cin >> numRolls;

cout << "seed?\n";
cin >> seed;
int ** b = rollDie(numRolls, seed, a);

int i,j;
for (i = 0; i < 6; i++) {
    for (j = 0; j < 6; j++) {
        cout << b[i][j];
    }
    cout << "\n";
    }

}
19
  • where do you initialise a? Commented Jul 19, 2013 at 1:10
  • a is initialized in a function that makes a 6x6 array of zeroes. I tested that in main and was able to print out the array properly. Commented Jul 19, 2013 at 1:12
  • What is "doesn't run properly", how is properly? Commented Jul 19, 2013 at 1:17
  • 1
    Sure, you're not passing it as a reference, you should use int** &rollarray instead, or make a copy and return the modified copy, or int &rollarray[][], for better reading Commented Jul 19, 2013 at 1:21
  • 1
    @bc8787 - you declared a and the initializeArray perfectly. as far as I can see the bug is only in the 1+rand()%6 line - just remove the 1+. Oh, and BTW - you don't need b at all! rollDie doesn't need to return anything - it changes the data pointed to by a, just print a instead of b after the rollDie\ Commented Jul 19, 2013 at 1:56

2 Answers 2

1

Code works for me with just a few issues (I had to guess how you defined a. Next time add that too):

In the printing you should print a space after every number (minor)

In the random, you choose index as 1+rand()%6, so from 1 to 6, but when you print you take indexes from 0 to 5! So your first row and first column will be 0.

Other than that it seems to work.

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

Comments

0

Only when one goes and does something else does the answer come to mind. I suspect you declared a as:

int a[6][6];

which is an array of 36 integers. In your function, though, you're declaring rollarray to be a pointer to an array of pointers to integers. All you need to do is change the function signature to:

int* rollDie(int numRolls, unsigned short seed, int* rollarray)

As cluracan said, you also want to use array indices in the range 0 to 5.

This is a good case for either the judicious use of print statements or stepping through with a debugger to see what's really going on.

2 Comments

He edited his question to show he declared a as an array of arrays
Which, of course, leads to the question "Why??? Why make it so much more complicated than it needs to be?" :-)

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.