mega5800
mega5800

Reputation: 87

define 2D array dimensions by random numbers in C

I want to create a 2d array using random dimensions.

Here's my random number maker method:

int GetRandomNumber(int low, int high)
{
    srand((unsigned)time(NULL));
    int res = rand() % ((high + 1) - low) + low;

    return res;
}

and here's the main code for now:

void main()
{
    int dime = GetRandomNumber(3, 5), a = GetRandomNumber(3, 5), b = GetRandomNumber(3, 5);

    int matA[a][dime];
    int matB[dime][b];

    system("PAUSE");
}

The problem is I get an expression must have a constant value and cannot allocate an array of constant size 0 errors.

My question is how to make a 2d array using random numbers as dimensions.

Upvotes: 1

Views: 598

Answers (3)

Nur Hossen
Nur Hossen

Reputation: 31

#include <stdio.h>
#include <stdlib.h> 
#include<time.h> 
void main()
{
    srand(time(0));
    int dime = GetRandomNumber(3, 5), a = GetRandomNumber(3, 5), b = GetRandomNumber(3, 5),c = GetRandomNumber(3, 5);

    int matA[a][dime];
    int matB[dime][b];
    printf("%d %d %d %d",dime,a,b,c);    
    //system("PAUSE");
}

int GetRandomNumber(int low, int high)
{
    int res = rand() % ((high + 1) - low) + low;

    return res;
}

Upvotes: 0

John Bode
John Bode

Reputation: 123596

As of C99, you can define an array's size at runtime using variable length arrays:

int x = some_value();
int y = some_other_value();

int arr[x][y];

Since their size isn't determined until runtime, there are some restrictions on their use - they can't have static storage duration (i.e., they can't declared at file scope or with the static keyword), and you can't use an initializer in the declaration. You also want to be careful if x or y are large.

If you don't have a C99 or later compiler available, then this won't work. You'll have to use dynamic memory allocation instead, and you'll have to allocate it as a "jagged" array:

int x = some_value();
int y = some_other_value();

int **arr = malloc( sizeof *arr * x );
if ( arr )
{
  for ( size_t i = 0; i < x; i++ )
  {
    arr[i] = malloc( sizeof *arr[i] * y );
  }
}

When you're done, you'd deallocate as

for (size_t i = 0; i < x; i++ )
  free( arr[i] );
free( arr );

This will work like any other 2D array when it comes to accessing elements:

arr[i][j] = some_value();

but be aware that the rows will not be contiguous - there will be gaps from the end of one row to the beginning of the next row. If that matters, then the last option is to declare a 1D array and manually compute the index:

int *arr = malloc( sizeof *arr * x * y );
...
arr[i * x + j] some_value();

When you're done, you'd free it as

free( arr );

Upvotes: 4

Austin Stephens
Austin Stephens

Reputation: 401

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int getRandom(int digits, int start) {
    return rand() % digits + start;
}

int main() {
    /* Seed rand() with current time; only want to do
     * this once */
    srand(time(0));

    /* Set row and col sizes equal to random numbers on
     * the interval [1, 4]; 4 is the number of
     * digits we want and 1 is where we start */
    int row = getRandom(4, 1);
    int col = getRandom(4, 1);

    /* Declare a 2d array of row and col size */
    int arr[row][col];

    /* Traverse through the 2d array */
    for(int i = 0; i < row; ++i) {
        for(int j = 0; j < col; ++j) {
            /* Set arr[i][j] to a random number in the
             * interval [-10, 10]; there are 21 digits
             * from -10 to 10 */
            arr[i][j] = getRandom(21, -10);
            /* Print the contents of the array */
            printf("arr[%d][%d] = %d\n", i, j, arr[i][j]);
        }
    }

    return 0;
}

Upvotes: 0

Related Questions