Thread: simple matrix

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    106

    simple matrix

    Hey so I need to print out a matrix using malloc function. I have done that part and this is my code
    Code:
    #include<stdio.h>
    #include <stdlib.h>
    
    int main (void)
    {
    int row_size1,col_size1,row_size2,col_size2;
    int **A;
    int i = 0, j = 0;
    
    //allocate space for 1 matrix
    A = malloc(1 * sizeof (int *));
    
    printf("Enter the number of rows and columns:");
    scanf("%d %d",&row_size1,&col_size1);
    
    A= malloc(row_size1*sizeof(int*));
    for(i=0;i<row_size1;i++)
        {
          A[i]=(int*)malloc(col_size1*sizeof(int));
        }
    
    printf("Enter the elements of the first matrix write 99 when done:");
    for(i=0;i<row_size1;i++)
      {
        for(j=0;j<col_size1;j++)
          {
              scanf("%d",&A[i][j]);
          if (A[i][j] == 99) // 'x' is character variable I declared to \
     break;
              }
            }
    
    printf("The required matrix is\n");
    for(i=0;i<row_size1;i++)
       {
         for(j=0;j<col_size1;j++)
                printf("%d ",A[i][j]);
                printf("\n");
            }
    free(A);
    }

    So the catch is I don't want to ask the users seperately the number of rows and columns of a matrix. I want them to tell me at the same time they are telling me the matrix.
    for example:
    Code:
    printf("What is your matrix")
    
    (user) 2 2 1 2 3 4
    so I want the code to record the first 2 values (2,2) as the number of rows and columns and then print out the rest of the matrix.
    The only thing confusing me about this is how I am supposed to allocate space for a matrix I don't know the dimensions of

    Thanks
    Last edited by zafy; 11-13-2012 at 05:21 PM.

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Read the row and value from the user before allocating memory for the matrix. Just don't tell the user what you're doing.

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    There are a lot of memory leaks in your program.

    First allocation of "A" gets disregarded and not freed -> It's not needed.

    Don't cast malloc. Also, each element you allocate here needs to be freed individually.
    Code:
    A[i]=(int*)malloc(col_size1*sizeof(int));
    Fact - Beethoven wrote his first symphony in C

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    Is this what you meant?
    Its still not compiling
    Code:
    #include<stdio.h>
    #include <stdlib.h>
    
    
    int main (void)
    {
      int row_size1,col_size1,row_size2,col_size2;
      int **A;
      int i = 0, j = 0;
    
    
      //allocate space for 1 matrix
      A = malloc(1 * sizeof (int *));
    
    
         /* printf("Enter the number of rows and columns:");
      scanf("%d %d",&row_size1,&col_size1);
    
    
      A= malloc(row_size1*sizeof(int*));
      for(i=0;i<row_size1;i++)
        {
          A[i]=(int*)malloc(col_size1*sizeof(int));
        }
      */
      printf("Enter the elements of the first matrix write 99 when done:");
      row_size1= A[0][0];
      col_size1= A[0][1];
    
    
      A= malloc(row_size1*sizeof(int*));
      for(i=0;i<row_size1;i++)
        {
          A[i]=(int*)malloc(col_size1*sizeof(int));
    
    
        }
    
    
    
    
      for(i=0;i<row_size1;i++)
        {
          for(j=0;j<col_size1;j++)
            {
              scanf("%d",&A[i][j]);
              if (A[i][j] == 99) // 'x' is character variable I declared to \
     break;
            }
        }
    
    
    printf("The required matrix is\n");
    for(i=0;i<row_size1;i++)
        {
          for(j=0;j<col_size1;j++)
            printf("%d ",A[i][j]);
          printf("\n");
        }
     free(col_size1);
        }
    Also could you please explain what you meant by not casting malloc

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    No.

    You have kept the first allocation and commented out the second. As soon as the next malloc is called, the address of "A" is overwritten with another address -> The first address is lost and therefore can not be freed.

    Uncomment what you have commented out and then comment the First ​malloc.

    You are still casting malloc with (int *) - FAQ: Casting malloc?
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    Wait.. so the problem I have is if I uncomment what I have commented how will the program know what row_size1 and col_size1 are?
    This is what I think you mean
    Code:
    #include<stdio.h>
    #include <stdlib.h>
    
    
    int main (void)
    {
      int row_size1,col_size1,row_size2,col_size2;
      int **A;
      int i = 0, j = 0;
    
    
    
    
      printf("Enter the elements of the first matrix write 99 when done:");
      row_size1= A[0][0];
      col_size1= A[0][1];
    
    
      A= malloc(row_size1*sizeof(int*));
      for(i=0;i<row_size1;i++)
        {
          A[i]=malloc(col_size1*sizeof(int));
    
    
        }
      //allocate space for 1 matrix
       A = malloc(1 * sizeof (int *));
    
    for(i=0;i<row_size1;i++)
        {
          for(j=0;j<col_size1;j++)
            {
              scanf("%d",&A[i][j]);
              if (A[i][j] == 99) // 'x' is character variable I declared to \
                break;
            }
        }
    
    
    printf("The required matrix is\n");
    for(i=0;i<row_size1;i++)
     {
          for(j=0;j<col_size1;j++)
            printf("%d ",A[i][j]);
          printf("\n");
        }
     free(col_size1);
    }
    p.s please bear with me I'm a bit slow but I really want to learn this stuff

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Uncomment what you have commented out and then comment the Firstmalloc.
    This is what I meant:
    Code:
      //allocate space for 1 matrix
      //A = malloc(1 * sizeof (int *));
    
      printf("Enter the number of rows and columns:");
      scanf("%d %d",&row_size1,&col_size1);
    
      A= malloc(row_size1*sizeof(int*));
    
      for(i=0;i<row_size1;i++)
      {
        A[i]= malloc(col_size1*sizeof(int));
      }
    And to free the allocated memory
    Code:
      for(i=0;i<row_size1;i++)
      {
        free(A[i]);
      }
    
      free(A);
    Fact - Beethoven wrote his first symphony in C

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    I dont want to seperately ask the user for the number of rows and columns. I want the program to run like this
    Code:
    printf("Enter the elements of the first matrix write 99 when done:");
    (user) 2 2 1 2 3 4 
    
    then I want 2 and 2 to be the number of rows and columns and 1 2 3 4 to be the 2 by 2 matrix printed
    

  9. #9
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by zafy View Post
    I dont want to seperately ask the user for the number of rows and columns.
    Then don't ask the user. Just read the row and column using scanf. You don't have to tell the user that you're reading the row and column before you allocate memory and run through the loop for reading each value in the matrix.

  10. #10
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I was pointing out a memory leak in the code you had provided - If you want to not do it that way any more, that's ok.
    Fact - Beethoven wrote his first symphony in C

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    Isnt that what I'm doing when I am writing this in my code
    row_size1= A[0][0]; //first element is row
    col_size1= A[0][1];

  12. #12
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    No. Read two integers into separate variables. The matrix can't be allocated until you know how big to make it, so you can't use A[0][0] and A[0][1] until you get the size from the user.

  13. #13
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    I was pointing out a memory leak in the code you had provided - If you want to not do it that way any more, that's ok.
    Thank you for the memory leak. I kind of mentioned in the beginning that this was the trick in the program I wasn't able to do. Sorry I dodnt mean to confuse you

  14. #14
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    I dont get how to read 2 integers from 6 integers that the user has provided without using A[0][1].
    Could you demonstrate it please

  15. #15
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    It looks like you already know how to read two integers from the user:
    Code:
    scanf("%d %d",&row_size1,&col_size1);
    (I took this line from your post from an hour ago)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help in Matrix Addition & finding Inverse of a Matrix
    By ssatyan.129 in forum C Programming
    Replies: 6
    Last Post: 05-15-2009, 02:48 PM
  2. help with simple programs. Matrix multiplications
    By ovadoggvo in forum C Programming
    Replies: 1
    Last Post: 05-18-2005, 11:46 PM
  3. Matrix Multiplication with simple syntax! plz help.
    By codebox in forum C Programming
    Replies: 6
    Last Post: 11-05-2004, 09:03 AM
  4. Simple Matrix Operations?
    By devin in forum C++ Programming
    Replies: 4
    Last Post: 12-15-2003, 12:10 AM
  5. Matrix: Reloaded + Enter The Matrix (the game)
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 04-18-2003, 12:35 AM