Thread: Negative value does not get carried forward into function

  1. #1
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463

    Negative value does not get carried forward into function

    Doing a small program for calculating 2x2 matrix inverses. It inverts fine, however when I added an extra function to do an indentity matrix check, it goes a bit haywire, and the reason I found is due to the negative value from the main function not getting passed into the function, essentially only the value get's passed, not the sign.

    Code:
    //Matrix
    
    #include<stdio.h>
    
    void matMul(float org[4], float inv[4]);
    int main (void) {
    
      float mat[4] = {0,0,0,0};  //USER ENTERED MATRIX
      float coef;
    
      printf("Enter numbers\n");
      scanf("&#37;f %f %f %f", &mat[0], &mat[1], &mat[2], &mat[3]);
    
      if((mat[0]*mat[3]) - (mat[1]*mat[2]) != 0){
    
        coef = 1/((mat[0]*mat[3]) - (mat[1]*mat[2]));
      }
    
      else {
         printf("Division by zero error\n");
         return 0;
      }
    
      float mat2[4] = {0,0,0,0};    //CALCULATION FOR THE INVERTED MATRIX
      mat2[0] = mat[3];
      mat2[3]= mat[0];
      mat2[2] = mat[2]*-1;
      mat2[1] = mat[1]*-1;
    
    
    printf("\n");                                                     //FURTHER CALCULATION AND PRINTING THE INVERTED MATRIX
    printf("Inverted Matrix is:\n");
    printf("\n");
    printf("|%1.2f %1.2f|\n", mat2[0]*coef, mat2[1]*coef);
    printf("|%1.2f %1.2f|\n", mat2[2]*coef, mat2[3]*coef);
    printf("\n");
    
    printf("Identity check, y or n?\n");
    
      char c;
      getchar();
      c = getchar();
    
      while(( c = getchar()) != EOF ) {
        if(c == 'n'){ 
          return 0;
        }
        else if(c == 'y'){                                          //PASSING THE INVERTED MATRIX AND ORIGINAL MATRIX TO A NEW FUNCTION
          matMul(mat,mat2);
        }
       }
    return 0;
    }
    
    void matMul(float org[4], float inv[4]){
    
      float ide[4] = {0,0,0,0};
    
      ide[0] = (org[0]*inv[0]) + (org[1]*inv[2]);
      ide[1] = (org[0]*inv[1]) + (org[1]*inv[3]);
      ide[2] = (org[2]*inv[0]) + (org[3]*inv[2]);
      ide[3] = (org[2]*inv[1]) + (org[3]*inv[3]);
      
      printf("\n");
      printf("|%1.2f %1.2f|\n", ide[0], ide[1]);
      printf("|%1.2f %1.2f|\n", ide[2], ide[3]);
      printf("\n");
    }
    Last edited by JFonseka; 10-16-2007 at 12:52 AM.

  2. #2
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Nevermind, I found the error

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM