Search code examples
c++pointersrandommultidimensional-arraypointer-to-array

How to populate 2d array with random numbers using pointers


When I execute this code, I don't get a matrix of completely random numbers but instead get many repeating numbers. Any idea how I can rainGen function to produce a truly random 2d matrix? Any help you can provide would be really really appreciated!!

#include <iostream>
#include <cstdlib>
using namespace std;

const int MATRIX_ROW = 5;
const int MATRIX_COL = 5;

int** rainGen(int **theMatrix_ptr_ptr, int rows);

int main()
{
    int matrix[MATRIX_ROW][MATRIX_COL];
    int *matrix_ptr[MATRIX_ROW];
int **matrix_ptr_ptr = &matrix_ptr[0];
int rows = 3;

for (int count = 0; count < MATRIX_ROW; count++)
    matrix_ptr[count] = &matrix[count][0];

rainGen(matrix_ptr_ptr, rows);

return 0;
}

int** rainGen(int **theMatrix_ptr_ptr, int rows)
{
    srand(1023);

    for (int i = 0; i < rows; i++)
        for (int j = 0; j < MATRIX_COL; j++)
            *((*theMatrix_ptr_ptr + i) + j) = rand() % 100;

return theMatrix_ptr_ptr;
}

Solution

  • I added some debugging code. The int matrix[MATRIX_ROW][MATRIX_COL]; statement allocates memory for matrix but it doesn't initialize the element values. The values will be whatever bit pattern was left from the previous memory use. I initialized the matrix element values to an unexpected value of -1. At the end, I printed the matrix.

    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    const int MATRIX_ROW = 5;
    const int MATRIX_COL = 5;
    
    int** rainGen(int **theMatrix_ptr_ptr, int rows);
    
    int main()
    {
         int matrix[MATRIX_ROW][MATRIX_COL];
         // initialize matrix elements to -1.
         for (int i = 0; i < MATRIX_ROW; ++i)
              for (int j = 0; j < MATRIX_COL; ++j)
                   matrix[i][j] = -1;
         int *matrix_ptr[MATRIX_ROW];
         int **matrix_ptr_ptr = &matrix_ptr[0];
         int rows = 3; // error
         for (int i = 0; i < MATRIX_ROW; i++)
              matrix_ptr[i] = &matrix[i][0];
         rainGen(matrix_ptr_ptr, rows);
         // print matrix elements.
         for (int i = 0; i < MATRIX_ROW; ++i) {
              for (int j = 0; j < MATRIX_COL; ++j)
                   cout << " [" << i << "," << j << "] " << matrix[i][j];
              cout << endl;
         }
         return 0;
    }
    
    int** rainGen(int **theMatrix_ptr_ptr, int rows)
    {
         srand(1023);
         for (int i = 0; i < rows; i++)
              for (int j = 0; j < MATRIX_COL; j++)
                   *((*theMatrix_ptr_ptr + i) + j) = rand() % 100; //error
         return theMatrix_ptr_ptr;
    }
    

    Output:

    [0,0] 8 [0,1] 53 [0,2] 30 [0,3] 51 [0,4] 40 
    [1,0] 88 [1,1] 60 [1,2] -1 [1,3] -1 [1,4] -1 
    [2,0] -1 [2,1] -1 [2,2] -1 [2,3] -1 [2,4] -1 
    [3,0] -1 [3,1] -1 [3,2] -1 [3,3] -1 [3,4] -1 
    [4,0] -1 [4,1] -1 [4,2] -1 [4,3] -1 [4,4] -1 
    

    As you can see from the matrix elements with a value of -1, you are not giving all the elements of the matrix a random value.

    As a helpful hint, I marked two statements of particular interest with an // error comment.

    Once you have fixed your problems, the output should look something like this:

    [0,0] 8 [0,1] 14 [0,2] 19 [0,3] 36 [0,4] 99 
    [1,0] 53 [1,1] 45 [1,2] 20 [1,3] 64 [1,4] 3 
    [2,0] 30 [2,1] 51 [2,2] 40 [2,3] 88 [2,4] 60 
    [3,0] 22 [3,1] 36 [3,2] 42 [3,3] 1 [3,4] 49 
    [4,0] 96 [4,1] 24 [4,2] 48 [4,3] 92 [4,4] 46