Thread: simple matrix

  1. #16
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    Please tell me this is what you meant
    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 *));
    
    
    
    
      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));
        }
      A = malloc(1 * 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");
        }
     for(i=0;i<row_size1;i++)
        {
          free(A[i]);
        }
    
    
      free(A);
    }

  2. #17
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    Personally I dont get how you can scanf something before even printing it and.. even if I use the same scanf after printf it only scans 2 values it means it disregards the other values
    or maybe thats what you meant

  3. #18
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    I tried the same program again moving the printf statement to above the scanf but I get a segmentation fault

  4. #19
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    956
    Your program will do exactly what you write it to do. If you want to tell the user to input a matrix before you read the first values (the size) you have to tell the user before you read the values. The point I was trying to make is that you don't have to print separate prompts to input the size and the values in the matrix.

    Post your latest code.

  5. #20
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    When I try to run your code I get warnings and errors.

    This line
    Code:
    if (A[i][j] == 99) // 'x' is character variable I declared to \
                break;
    '\' Causes the comment to run onto the next line, so the break is never used.

    To get around this, write your comment as
    Code:
    if (A[i][j] == 99) // 'x' is character variable I declared to '\'
                break;
    Code:
    
      A= malloc(row_size1*sizeof(int*));
      for(i=0;i<row_size1;i++)
        {
          // casting malloc is back
          A[i]=(int*)malloc(col_size1*sizeof(int));
        }
      
      // memory location of original A is lost.
      A = malloc(1 * sizeof (int *));
    Fact - Beethoven wrote his first symphony in C

  6. #21
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    Please tell me I am closer to the answer
    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:");
    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));
        }
    
    
    
    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
    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");
        }
            for(i=0;i<row_size1;i++)
            {
              free(A[i]);
            }
    free(A);
    }
    Last edited by zafy; 11-13-2012 at 08:45 PM.

  7. #22
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    What did you change from post 16, beside good indentation?

    I see that you haven't implemented any of my suggested changes from post 20. Do them and your code works.
    Fact - Beethoven wrote his first symphony in C

  8. #23
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    In particular, the part where I say "memory location of original A is lost." -> That is why you are seg faulting.
    Fact - Beethoven wrote his first symphony in C

  9. #24
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    i uncasted my malloc I edited my code so now the memory of A is not lost right?

  10. #25
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    You'll have to show us what you did. Please make sure you have some good indentation, like post #16
    Fact - Beethoven wrote his first symphony in C

  11. #26
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    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:");
    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));
        }
    
    
    
    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
    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");
        }
            for(i=0;i<row_size1;i++)
            {
              free(A[i]);
            }
    free(A);
    }
    
    

  12. #27
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    My main issue right now is that my program can't seem to understand that the first 2 values the user inputs are the values of the row and column and the rest are the matrix

  13. #28
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    956
    Quote Originally Posted by zafy View Post
    Code:
    A = malloc(1 * sizeof (int *));
    This line is still suspiciously present in your code.

  14. #29
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    Wait so you dont want that line at all?
    I thought I was supposed to put it after.

    Just to be clear this line allocates space for a matrix right? because I might want to add a second matrix later on to this code and I planned on changing the 1 to a 2 since it allocates space for 2 matrices

  15. #30
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Remove it completely.
    Fact - Beethoven wrote his first symphony in C

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