Thread: thinking process

  1. #61

  2. #62
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    Using the command sequence from message #27 and the code from message #55, after the first command is given, what is stored for matrix A? Please explain why you think so. Or use your debugger to step through the code and see what is stored for A.

  3. #63
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    well at the end of the if statement of 'A' it is freed so I am guessing nothing is stored. But that free is also necessary because if a person simultaneuosly writes A 2 2 1 2 3 4 and then A 2 2 1 2 3 4 then I only want the second one for A to be stored and that is only possible if I free the first one

  4. #64
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    So when do you want to free the matrix A? After the first one or when you get the second one? How can you tell if you already have a matrix A (or B or C or D)?

  5. #65
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    I should free it after the second one is assigned but I dont know how I can tell if there is already a matrix. What can I do?

  6. #66
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    Why isn't my multiplication function working its giving me a segmentation fault?
    Code:
    void matrix_multiply( int **A, int **B, int row_size1, int col_size1)
    {
      int mult[10][10];
      int i,j,k;
      for(i=0;i<row_size1;i++)
        {
          for(j=0;j<col_size1;j++)
            {
              mult[i][j]=0;
            }
        }
              for(k=0;k<col_size1;k++)
                {
                  mult[i][j] = mult[i][j]+ A[i][k]*B[k][j];
                }
              for(i=0; i<row_size1; i++) {
                for(j=0; j<row_size1; j++) {
    
    
              printf("%d\t",mult[i][j]);
                }
          printf("\n");
              }
    }

  7. #67
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    > Why isn't my multiplication function working its giving me a segmentation fault?
    Maybe it's because you can't even copy and paste properly.
    Nor does it seem like you're paying even the blindest bit of attention to all the cries of woe imploring you to INDENT YOUR CODE!!!!

    Your latest bug-fest
    Code:
    void matrix_multiply(int **A, int **B, int row_size1, int col_size1)
    {
      int mult[10][10];
      int i, j, k;
      for (i = 0; i < row_size1; i++) {
        for (j = 0; j < col_size1; j++) {
          mult[i][j] = 0;
        }
      }
      for (k = 0; k < col_size1; k++) {
        mult[i][j] = mult[i][j] + A[i][k] * B[k][j];
      }
      for (i = 0; i < row_size1; i++) {
        for (j = 0; j < row_size1; j++) {
          printf("%d\t", mult[i][j]);
        }
        printf("\n");
      }
    }
    Compare with Post #56 (56 FFS, we could have written an OS by now)
    Code:
    void matrix_multiply( int **A, int **B, int row_size1, int col_size1)
    {
      int mult[10][10];
      int i,j,k;
      for(i=0;i<row_size1;i++)
        {
          for(j=0;j<col_size1;j++)
            {
              mult[i][j]=0;
              for(k=0;k<row_size1;k++)
                {
                  mult[i][j]+=A[i][k]*B[k][j];
                }
              printf("%d\t",mult[i][j]);
            }
          printf("\n");
        }
    }
    Now look really carefully.
    How does the "for k" loop you wrote compare to the "for k" loop posted by Click_here?

    I'm having trouble deciding whether you're a help vampire or just a troll.
    Right about now, you're verging on being a troll deliberately getting things wrong just to ........ everyone off.
    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.

  8. #68
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by zafy View Post
    storing my matrices in an array.. so you mean indexing?
    I haven't really done indexing before if you could provide me with a link for me to look over.
    And what do you think you are doing with all your matrices????

    Quote Originally Posted by zafy View Post
    Also no i don't know which type this array should have.
    but i like this method you suggested and want to try it
    If you are still interested I give you a hint:
    Assume you have 4 integer variables A, B, C and D. Now show me how you would declare these variables, declare an additional array which can contain 4 integers and then assign each of the 4 variables to an element of the array. All in all I want you to show me 9 lines: 5 declarations and 4 assignments (one declaration/assignment per line).

    If you are able to do this, you should be able to a) know which type the matrices array should have and b) how to use it with your 4 matrices.

    If you can't do this, I can assure you the assignment you have to do is way over your capability.

    Bye, Andreas

  9. #69
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    After many strenous hours THANK GOD I got it!
    still alot of glitches here and there but... it's working
    thanks all

    Code:
    #include<stdio.h>
    #include <stdlib.h>
    
    
    void matrix_display(int **A, int row_size1, int col_size1);
    void matrix_free(int **A, int row_size1);
    int **matrix_alloc(int row_size1, int col_size1);
    void matrix_fill(int **A, int row_size1, int col_size1);
    void matrix_add( int **A, int **B, int row_size1[], int col_size1[],int,int);
    void matrix_multiply( int **A, int **B, int row_size1[], int col_size1[],int,in\
    t);
    void matrix_transpose( int **A, int row_size1[], int col_size1[], int);
    
    
    
    
    int main(void)
    {
     int row_size1[5],col_size1[5],flag=1;
      int **A = NULL;
      int **B = NULL;
      int **C = NULL;
      int **D = NULL;
      int **p1, **p2;
      char c,k1,k2;
    
    
      do {
        printf("\ncmd > ");
        c = getchar();
     if (c == 'A') {
          scanf("%d %d", &row_size1[0], &col_size1[0]);
          A = matrix_alloc(row_size1[0],col_size1[0]);
          matrix_fill(A,row_size1[0],col_size1[0]);
          matrix_display(A,row_size1[0],col_size1[0]);
          //    matrix_free(A,row_size1);
        }
         if (c == 'B') {
          scanf("%d %d", &row_size1[1], &col_size1[1]);
          B = matrix_alloc(row_size1[1],col_size1[1]);
          matrix_fill(B,row_size1[1],col_size1[1]);
          matrix_display(B,row_size1[1],col_size1[1]);
          // matrix_free(B,row_size1);
    
    
        }
        if (c == 'C') {
          scanf("%d %d", &row_size1[2], &col_size1[2]);
          C = matrix_alloc(row_size1[2],col_size1[2]);
          matrix_fill(C,row_size1[2],col_size1[2]);
          matrix_display(C,row_size1[2],col_size1[2]);
          //      matrix_free(C,row_size1);
    
    
        }
     if (c == 'D') {
          scanf("%d %d", &row_size1[3], &col_size1[3]);
          D = matrix_alloc(row_size1[3],col_size1[3]);
          matrix_fill(D,row_size1[3],col_size1[3]);
          matrix_display(D,row_size1[3],col_size1[3]);
          // matrix_free(D,row_size1);
    
    
          }
        int num1, num2;
         if ( c == '+'){
     scanf(" %c %c", &k1, &k2);
           num1= k1-65;
           num2=k2-65;
           if (k1=='A')
             p1 = A;
           else
             if (k1=='B')
               p1= B;
             else
               if (k1 == 'C')
                 p1 = C;
               else
                 if (k1=='D')
                   p1 =D;
           if (k2=='A')
             p2 = A;
           else
             if (k2=='B')
               p2= B;
             else
               if (k2 == 'C')
                 p2 = C;
               else
                 if (k2=='D')
                   p2 =D;
           /* A = A + B */
           matrix_add(p1, p2, row_size1, col_size1,num1,num2);
      matrix_free(A,row_size1);
           matrix_free(B,row_size1);
         }
         if ( c == '*'){
           scanf(" %c %c", &k1, &k2);
           printf ("%c %c\n",k1,k2);
           num1= k1-65;
           num2=k2-65;
           if (k1=='A')
             p1 = A;
           else
             if (k1=='B')
     p1= B;
             else
               if (k1 == 'C')
                 p1 = C;
               else
                 if (k1=='D')
                   p1 =D;
           if (k2=='A')
             p2 = A;
           else
             if (k2=='B')
               p2= B;
             else
               if (k2 == 'C')
                 p2 = C;
               else
                 if (k2=='D')
                   p2 =D;
      matrix_multiply(p1, p2, row_size1, col_size1,num1,num2);
    
    
    
    
         }
         if ( c == '^'){
           scanf(" %c", &k1);
           printf (" %c\n",k1);
           num1= k1-65;
     if (k1=='A')
             p1 = A;
           else
             if (k1=='B')
               p1= B;
             else
               if (k1 == 'C')
                 p1 = C;
               else
                 if (k1=='D')
                   p1 =D;
      matrix_transpose(p1, row_size1, col_size1,num1);
      }
    
    
    
    
        if (c == '$') {
          flag = 0;
        }
      }
    
    
      while (flag == 1);
      return 0;
    }
    
    
    void matrix_display(int **A, int row_size1, int col_size1)
    {
      int i, j;
     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");
      }
    }
    
    
    void matrix_free(int **A, int row_size1)
    {
      int i;
      for (i = 0; i < row_size1; i++) {
        free(A[i]);
      }
      free(A);
    }
    
    
    int **matrix_alloc(int row_size1, int col_size1)
    {
      int i;
     int **result = malloc(row_size1 * sizeof(int *));
      for (i = 0; i < row_size1; i++) {
        result[i] = malloc(col_size1 * sizeof(int));
      }
      return result;
    }
    
    
    void matrix_fill(int **A, int row_size1, int col_size1)
    {
      int i, j;
      printf("Enter matrix of %d rows and %d columns\n", row_size1, col_size1);
      for (i = 0; i < row_size1; i++) {
        for (j = 0; j < col_size1; j++) {
          scanf("%d", &A[i][j]);
        }
      }
    }
    void matrix_add( int **A, int **B, int row_size1[], int col_size1[],int x,int y\
    )
    {
     if (row_size1[x]!= row_size1[y]||col_size1[x]!=col_size1[y])
        {
          fprintf (stderr,"Matrix incompatible for addition\n");
          exit (1);
        }
      int i,j;
      for(i=0;i<row_size1[x];i++)
        for(j=0;j<col_size1[x];j++)
          A[i][j]=A[i][j]+B[i][j];
    
    
      printf("\n\nThe Sum Of Matrices Is:");
      for(i=0;i<row_size1[x];i++){
        printf("\n");
        for(j=0;j<col_size1[x];j++)
          printf("%d ",A[i][j]);
      }
    }
    void matrix_multiply( int **A, int **B, int row_size1[], int col_size1[],int x,\
     int y)
    {
      printf ("\n\n");
      int mult[10][10];
      int i,j,k;
      printf ("lets see");
        for(i=0;i<row_size1[x];i++)
        {
          for(j=0;j<col_size1[y];j++)
            {
              mult[i][j]=0;
            }
        }
     for(i=0; i<row_size1[x]; i++) {
                for(j=0; j<col_size1[y]; j++) {
                  for(k=0;k<row_size1[y];k++)
                    {
                      mult[i][j] = mult[i][j]+ A[i][k]*B[k][j];
                      //printf("%d\t",mult[i][j]);
                    }
                  printf ("%d\t",mult[i][j]);
                }
          printf("\n");
              }
    }
    void matrix_transpose( int **A, int row_size1[], int col_size1[], int x)
    {
      printf ("\n\n");
      int transpose [10][10];
    int i,j;
    for( i = 0 ; i < row_size1[x] ; i++ )
      {
     for( j = 0 ; j < col_size1[x] ; j++ )
          {
            transpose[j][i] = A[i][j]; //defining the transpose
          }
      }
    printf("Transpose of entered matrix :-\n");
    for( i = 0 ; i < col_size1[x] ; i++ ) //changing entries of the for loop t
      {
        for( j = 0 ; j < row_size1[x] ; j++ )
          {
            printf("%3d\t",transpose[i][j]);
          }
    
    
        printf("\n");
      }
    }
    dont mind the indenting I am just so excited its working and I can finally show you guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thinking about learning something else
    By tiachopvutru in forum A Brief History of Cprogramming.com
    Replies: 31
    Last Post: 06-24-2008, 02:45 PM
  2. too much thinking?
    By willc0de4food in forum Windows Programming
    Replies: 9
    Last Post: 10-03-2006, 05:34 AM
  3. Child Process & Parent Process Data :: Win32
    By kuphryn in forum Windows Programming
    Replies: 5
    Last Post: 09-11-2002, 12:19 PM
  4. I know you've been thinking about this...
    By Aran in forum A Brief History of Cprogramming.com
    Replies: 43
    Last Post: 11-08-2001, 04:13 PM