Need to make a random size (x = rows, y = columns) matrix which is filled with random numbers (only 0 or 1). So, if I don't know the size of the matrix, I use dynamic array. So far I have made this far, but obviously it isn't quite correct... Now I can get only one dimension array with random natural numbers.

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    int **A
    int x, y;
    x = y = (rand() % 1);
    srand (time(0));
    A = new int*[x];
    for ( int i = 0 ; i < x ; i++ );
       A = new int*[y];
       for ( int j = 0 ; j < y ; j++ );

       cout << **A << endl;
   delete [] A;

return 0;
}

Is it even possible to make this matrix (random rows, random collumns, filled with random numbers)? If so, than I would appreciate any help with the code.. Or the size of the matrix must to be initiated before filling it?

P.S. First time with random number generation for me

Edited 7 Years Ago by soffie: n/a

If you want random numbers for each x and y you would need to separate their random number generation. Otherwise you would be randomizing the same value for both the row value and column value. Unless that is what you want!

For initializing each element in the 2d array I've always used nested for loops such as..

for (int i = 0; i < x; i++)
{
      // This loop is for the row
     for (int p = 0; p < y; p++)
     {
              // Here you would randomize each element for the array.
              random = rand() % 10;
              randomNums[i][p] = random;
     }
}

I think pointers only make this problem more complicated. I wouldn't use them unless you have to.

Here is some good information on two dimensional arrays. Scroll down towards the bottom.

This page contains a tutorial on random numbers.

First off you want to call srand() before you use rand() otherwise you will always get the same value for the first random.

Also you can take a look at a snippet I made for allocating memory in a 2 dimensional array. It uses user input and it expands on every input but you only need it once. here.

Or you can just random the 2 numbers (x, y) and after that random you can declare the multidimensional array and then make your for() loop and fill it with random 1s and 0s.

Your random is assigning x and y both the same value (1 or 0). This could result in the array being [0][0]. For making random number you can use this formula num = (rand() % max+1-min) + min; for the size of your array. Then use rand() % 2; for your assignment for each value.

If this doesn't help just ask and I'll write you a quick example for what you need.

Need to make a random size (x = rows, y = columns) matrix which is filled with random numbers (only 0 or 1). So, if I don't know the size of the matrix, I use dynamic array. So far I have made this far, but obviously it isn't quite correct... Now I can get only one dimension array with random natural numbers.

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    int **A
    int x, y;
    x = y = (rand() % 1);
    srand (time(0));
    A = new int*[x];
    for ( int i = 0 ; i < x ; i++ );
       A = new int*[y];
       for ( int j = 0 ; j < y ; j++ );

       cout << **A << endl;
   delete [] A;

return 0;
}

Is it even possible to make this matrix (random rows, random collumns, filled with random numbers)? If so, than I would appreciate any help with the code.. Or the size of the matrix must to be initiated before filling it?

P.S. First time with random number generation for me

Lines 14 and 16 - get rid of the semicolons. With the semicolons there, there is no loop.

Line 15 - it would be an error with the semicolons above, but after you take them out, notice that you don't use your loop control variable i in this line. You need to use it as an array index.

Line 18 - Once you actually declare the array and fill it in correctlyu, **A is going to point to A[0][0]. Again, you don't use your loop control variable j as an array index and you need to.

Thanks for the tips, I have solved my task, here is the new code.

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

int main()
{
  int x, y;
  int **A;
    srand((unsigned)time(0));
    x = (rand() % 10) + 4;
    cout << "Rows: "<< x << endl;

    y = (rand() % 10) + 4;
    cout << "Columns: "<< y << endl;
    cout << endl;

    A = new int*[x];
    for (int i = 0; i < x; i++)
      A[i] = new int[y];

    for(int i = 0; i < x; i++)
      for(int j = 0; j < y; j++)
        A[i][j] = rand() % 2;
          
    for(int i = 0 ; i < x ; i++)
    {
      for(int j = 0; j < y; j++)
        cout << A[i][j] << "  " ;
      cout << endl;
    }

    for (int i = 0; i < x; i++)
      delete [] A[i];
    delete [] A;

  system("PAUSE");
  return 0;
}
This question has already been answered. Start a new discussion instead.