# Thread: array asking for constant value (I am giving constant values)

1. ## array asking for constant value (I am giving constant values)

My task is to create a matrix of random numbers of nxn size. However, there is an error I can´t solve, it is telling me that the expression needs to have a constant value (on the variable "matrix" in the for loop). What am I doing wrong?
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main()
{
srand(NULL);

int rows;
int columns;

printf("define the size of the matrix\n number of rows\n");
scanf_s("%d", &rows);
printf("number of columns\n");
scanf_s("%d", &columns);
const int x=rows;
const int y=columns;

for (int i = 1; i < x; ++i)
{
for (int j = 1; j <y; ++j)
{
int matrix[x][y] = rand() % 21 + (-10);
printf("%d     ", matrix[i][j]);
}
printf("\n");
}
}``` 2. You have quite a few errors.
The srand call should be:
Code:
`srand(time(NULL));`
x and y seem useless. You already have rows and columns so just use those.

You need to define the matrix before the loop.
Code:
`int matrix[rows][columns];    // assuming C99`
Your loops need to start at 0 not 1 since array indices start at 0.

You need to index the matrix with i and j.

Instead of + (-10) why not just - 10 ? 3. Thanks for your answer. I have corrected the mistakes I made. However, the declaration of the matrix before the loop is asking me for constant values again, how could I solve this problem, because whenever I declare rows and columns variables with const int, it requires me an initializer.
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main()
{
srand(time(NULL));

const int rows;
int columns;

printf("define the size of the matrix\n number of rows\n");
scanf_s("%d", &rows);
printf("number of columns\n");
scanf_s("%d", &columns);
int matrix[rows][columns];

for (int i = 0; i < rows; ++i)
{
for (int j = 0; j <columns; ++j)
{
matrix[i][j] = rand() % 21 -10;
printf("%d     ", matrix[i][j]);
}
printf("\n");
}
}``` 4. In original C, and subsequently standardised in ANSI-C89/ISO-C90, arrays need a constant size.

This means literally things like
int array;

Things like this are not const enough for C89 (const is a rather light interpretation of const-ness, compared to say C++)
const int size = 10;
int array[size];

Variable-length array - Wikipedia, the free encyclopedia
C99 on the other hand allows such constructs.

Your compiler may have a switch which tells it which version of the language to compile against.

But if you absolutely need C89 compatibility, or you have no choice, then you need something like
Code:
```#include <stdlib.h>

int main ( ) {
int rows, cols;  // initialise how you want

// allocate space when  you know what rows,cols you need.
int **matrix = malloc( rows * sizeof(*matrix) );
for ( r = 0 ; r < rows ; r++ ) matrix[r] = malloc( cols * sizeof(*matrix[r]));

// use matrix[r][c] notation here

// All done, free the memory
for ( r = 0 ; r < rows ; r++ ) free(matrix[r]);
free(matrix);
}``` Popular pages Recent additions #include, constant, int, matrix, scanf_s%d 