Thread: Creating variable-size arrays

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    2

    Creating variable-size arrays

    I'm writing a program in C to multiply two user-inputted matrices and am having a little bit of trouble initializing the input arrays...

    Basically, the program starts with the user inputting the dimensions of each matrix (matrix1 and matrix2) and then these values are stored as integers in the variables rowsize1, columnsize1, rowsize2, columnsize2.

    I now want create these two matrices... However I cannot do this with matrix1[rowsize1][columnsize2] and matrix2[rowsize2][columnsize2] as this is illegal in C!

    I read that variable-size arrays can be created in C by using the "malloc" function but I'm not entirely sure on how to implement it in my case...

    For the first matrix (matrix1) would I have to do something like this:

    int **matrix1;
    matrix1 = (int**)malloc(columnsize1*sizeof(int*));

    for(a = 0; a < rowsize1; ++a)
    matrix1[a] = (int*)malloc(sizeof(int));


    I'm not really sure though...

    Any help would be much appreciated!!

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    do not cast malloc
    you dimentions are incorrect
    Code:
    int **matrix1;
    matrix1 = malloc(rowsize1*sizeof(*matrix1));
    if(matrix1)
    {
       int a;
       for(a = 0; a < rowsize1; ++a)
       {
          matrix1[a] = malloc(columnsize1*sizeof (*matrix1[a] ));
          if(matrix1[a]  == NULL)
          {
             /* malloc failed - free what was allocated and return error */
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    2
    Thanks a lot for that! It works perfectly now!
    Here is my complete code....

    Just to clarify, this method of creating a variable-size array is a new feature in the C99 standard, right? Therefore, there is no way to compile this on Microsoft Visual C (I receive numerous errors when I try to)?

    Thanks again!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    
    int a, row, column, rowsize1, rowsize2, columnsize1, columnsize2, i, j, counter1, counter2;
    
    /* Prompt user for matrix dimensions */
    
    printf("Enter row (m) size of Matrix 1: \n");
    scanf("%d", &rowsize1);
    printf("Enter column (n) size of Matrix 1: \n");
    scanf("%d", &columnsize1);
    
    printf("\n");
    
    printf("Enter row (m) size of Matrix 2: \n");
    scanf("%d", &rowsize2);
    printf("Enter column (n) size of Matrix 2: \n");
    scanf("%d", &columnsize2);
    
    /* Test matrix dimensions */
    
    while((rowsize2!=columnsize1))
        {
    	   printf("\nMatrix dimensions must agree! Please re-enter values!\n\n");
    	   
    	   printf("Enter row (m) size of Matrix 1: \n");
           scanf("%d", &rowsize1);
           printf("Enter column (n) size of Matrix 1: \n");
           scanf("%d", &columnsize1);
    
           printf("\n");
      
           printf("Enter row (m) size of Matrix 2: \n");
           scanf("%d", &rowsize2);
           printf("Enter column (n) size of Matrix 2: \n");
           scanf("%d", &columnsize2);
        }
    	
    /* Initialize dimensions of input matrices */
      
    int **matrix1;
    matrix1 = malloc(rowsize1*sizeof(*matrix1));
    if(matrix1)
    {
       int a;
       for(a = 0; a < rowsize1; ++a)
       {
          matrix1[a] = malloc(columnsize1*sizeof (*matrix1[a] ));
          if(matrix1[a]  == NULL)
          {
             /* malloc failed - free what was allocated and return error */	   	    
             free(matrix1);
    		 printf("Memory allocation failed!\n");
          }
       }
    }
    		
    int **matrix2;
    matrix2 = malloc(rowsize2*sizeof(*matrix2));
    if(matrix2)
    {
       int a;
       for(a = 0; a < rowsize2; ++a)
       {
          matrix2[a] = malloc(columnsize2*sizeof (*matrix2[a] ));
          if(matrix2[a]  == NULL)
          {
             /* malloc failed - free what was allocated and return error */
    		 free(matrix2);
    		 printf("Memory allocation failed!\n");
          }
       }
    }
    		 
    /* Prompt user for matrix values */
    
    printf("\n");
    
    for (row=0;row<rowsize1;++row)
       for (column=0;column<columnsize1;++column)
          {
          printf("Enter value for position (%d, %d) in Matrix 1: ", row+1, column+1);
    	  scanf("%d", &matrix1[row][column]);
          }
    	  
    printf("\n");
    
    for (row=0;row<rowsize2;++row)
       for (column=0;column<columnsize2;++column)
          {
          printf("Enter value for position (%d, %d) in Matrix 2: ", row+1, column+1);
    	  scanf("%d", &matrix2[row][column]);
          }
    	  
    /* Initialize dimensions of output matrix */
    		
    int **output;
    output = malloc(rowsize1*sizeof(*output));
    if(matrix1)
    {
       int a;
       for(a = 0; a < rowsize1; ++a)
       {
          output[a] = malloc(columnsize2*sizeof (*output[a] ));
          if(output[a]  == NULL)
          {
             /* malloc failed - free what was allocated and return error */
    		 free(output);
    		 printf("Memory allocation failed!\n");
          }
       }
    }
    
    /* Fill output matrix with zeroes */
    
    for(counter1=0;counter1<rowsize1;++counter1)
       for(counter2=0; counter2<columnsize2; ++counter2)
          output[counter1][counter2]=0;
    	  
    /* Multiply matrices together */ 
    
    for (row=0; row<rowsize1; ++row)
       for (column=0; column<columnsize2; ++column)
      
    	     for(i=1;i<=rowsize2;++i)
    		    output[row][column] = output[row][column] + (matrix1[row][i-1])*(matrix2[i-1][column]);
     
    /* Output matrix */
    
    printf("\nAnswer: \n");
    
    for (counter1=0; counter1<rowsize1; ++counter1)
       {
       printf("\n");
       for (counter2=0; counter2<columnsize2; ++counter2)
          printf("%i ", output[counter1][counter2]);
       }
       
    printf("\n");
       
    return EXIT_SUCCESS;
    		
    }

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Since you are using Visual Studio, you can let it fix your indentation (select all, alt+f8), and you can also look into safe functions such as scanf_s. And you're right. Visual Studio doesn't support C99, unfortunately.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating and freeing dynamic arrays
    By circuitbreaker in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2008, 11:18 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Replies: 11
    Last Post: 03-25-2003, 05:13 PM
  4. Identifying size of structure arrays
    By gowtham in forum C Programming
    Replies: 2
    Last Post: 02-10-2003, 05:02 PM
  5. Variable size arrays
    By crag2804 in forum C Programming
    Replies: 4
    Last Post: 09-08-2002, 06:00 PM