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

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    26

    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. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    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. #3
    Registered User
    Join Date
    Jan 2015
    Posts
    26
    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. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,169
    In original C, and subsequently standardised in ANSI-C89/ISO-C90, arrays need a constant size.

    This means literally things like
    int array[10];

    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);
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Constant values
    By Satya in forum C Programming
    Replies: 5
    Last Post: 06-25-2015, 10:49 PM
  2. Why make a constant a constant again?
    By Overworked_PhD in forum C Programming
    Replies: 3
    Last Post: 11-03-2007, 06:57 PM
  3. Constant pointer to constant value
    By tretton in forum C Programming
    Replies: 10
    Last Post: 12-23-2005, 01:45 PM
  4. constant string array
    By v6sa in forum C++ Programming
    Replies: 2
    Last Post: 05-11-2005, 04:39 AM

Tags for this Thread