#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Dividing and arrays


    Hello there!
    I learning C (newbie) and I am writing a program to eliminate the equation by Gauss method.
    My question is:
    In following code Step 3 working fine, but Step 4 and Step 5 no,
    all of them are using same code, can you tell me why 4 and 5 working in not estimated way?

    Code:
        
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
         
    main(){
        float a;
        int c, i;
        float matrix[15];
         
        a = i = 0;
        printf("Gauss method\n");
        printf("\nInput:\n");
         
        for(i = 0; i < 12; i++)
        scanf("%3e", &matrix[i]);
         
    //Step 1
    /* Checking [0] = 0, if true - replace 1 and 2 row*/
        if(matrix[0] == 0){
        for(i = 0; i < 4; i++){
        matrix[i+12] = matrix[i];
        }
        for(i = 4; i < 8; i++){
        matrix[i-4] = matrix[i];
        }
        for(i = 4; i < 8; i++){
        matrix[i] = matrix[i+8];
        }
        }
        printf("\nStep 1\n");
        for(i=0; i<4; i++)
        printf("%2.3f ",matrix[i]);
        printf("\n");
        for(i=4; i<8; i++)
        printf("%2.3f ",matrix[i]);
        printf("\n");
        for(i=8; i<12; i++)
        printf("%2.3f ",matrix[i]);
        printf("\n");
        for(i=12; i<16; i++)
        printf("%2.3f ",matrix[i]);
        printf("\n");
    //Step 2
    /* Checking [0] = 0, if true - replace 1 and 3 row*/
        if(matrix[0] == 0){
        for(i = 0; i < 4; i++){
        matrix[i+12] = matrix[i];
        }
        for(i = 8; i < 12; i++){
        matrix[i-8] = matrix[i];
        }
        for(i = 8; i < 12; i++){
        matrix[i] = matrix[i+4];
        }
        }
        printf("\nStep 2\n");
        for(i=0; i<4; i++)
        printf("%2.3f ",matrix[i]);
        printf("\n");
        for(i=4; i<8; i++)
        printf("%2.3f ",matrix[i]);
        printf("\n");
        for(i=8; i<12; i++)
        printf("%2.3f ",matrix[i]);
        printf("\n");
        for(i=12; i<16; i++)
        printf("%2.3f ",matrix[i]);
        printf("\n");
    //Step 3
    // Lead down [4] to 0
        if(matrix[4] != 0){
        a = - (double) matrix[0] / matrix[4];
        for(i = 4; i < 8; i++)
        matrix[i] = matrix[i] * a + matrix[i-4];
        }
         
        printf("\nStep 3\n");
        for(i=0; i<4; i++)
        printf("%2.3f ",matrix[i]);
        printf("\n");
        for(i=4; i<8; i++)
        printf("%2.3f ",matrix[i]);
        printf("\n");
        for(i=8; i<12; i++)
        printf("%2.3f ",matrix[i]);
        printf("\n");
        for(i=12; i<16; i++)
        printf("%2.3f ",matrix[i]);
        printf("\n");
         
        //if(matrix[4] != 0)
        //printf("ERROR");
    //Step 4
    // Lead down [8] to 0
        if(matrix[4] == 0 && matrix[8] != 0){
        a = - (double) matrix[0] / matrix[8];
        for(i = 8; i < 12; i++)
        matrix[i] = matrix[i] * a + matrix[i-8];
        }
         
        printf("\nStep 4\n");
        for(i=0; i<4; i++)
        printf("%2.3f ",matrix[i]);
        printf("\n");
        for(i=4; i<8; i++)
        printf("%2.3f ",matrix[i]);
        printf("\n");
        for(i=8; i<12; i++)
        printf("%2.3f ",matrix[i]);
        printf("\n");
        for(i=12; i<16; i++)
        printf("%2.3f ",matrix[i]);
        printf("\n");
    // Step 5
    // Lead down [9] to 0
        if(matrix[8] == 0 && matrix[4] == 0 && matrix[9] != 0){
        a = - (double) matrix[5] / matrix[9];
        for(i = 8; i < 12; i++)
        matrix[i] = matrix[i] * a + matrix[i-4];
        } else if(matrix[9] != 0)
        printf("a32 = 0");
        printf("\nStep 5\n");
        for(i=0; i<4; i++)
        printf("%2.3f ",matrix[i]);
        printf("\n");
        for(i=4; i<8; i++)
        printf("%2.3f ",matrix[i]);
        printf("\n");
        for(i=8; i<12; i++)
        printf("%2.3f ",matrix[i]);
        printf("\n");
        for(i=12; i<16; i++)
        printf("%2.3f ",matrix[i]);
        printf("\n");
         
         
        return 0;
        }
    Thanks in advance!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480

    a picture could help greatly....


    I assume that if you want to solve this equation
    Code:
       (40+i.3 3);'x';' = ';,.1 2 3
    ┌────────┬─┬───┬─┐
    │40 41 42│x│ = │1│
    │43 44 45│ │   │2│
    │46 47 48│ │   │3│
    └────────┴─┴───┴─┘
       
    
       1 2 3%.40+i.3 3
    _8.47292e10 1.69458e11 _8.47292e10
    your memory layout is

    matrix[0] through matrix[11] are
    40 41 42 1 43 44 45 2 46 47 48 3

    and you use matrix[12] through matrix[15] as temporary space.

    OK, the first error I see is that you've allocated space for only 15 entries in array, array[0] through array[14].

    you need
    float matrix[16];


    Please call me if my assumptions are incorrect, I value my time.
    607-962-xxxx usa
    never mind, just write a post as soon as possible. email notification is sufficient.

    Meanwhile, I'll fix your declaration and see if I can get your program to solve the problem I showed without too many changes.

    Dave.

    Comments on this post

    • YaR_ agrees
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Dave.
    Here is array legend.
    http://imghost.me/images/2012/10/22/fqN91.jpg
    Code:
    	float matrix[16];
    Done, but still something wrong on step 4 and 5
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    ok, the sample problem solved in executable Iverson notation
    Code:
       'A B'=:(3 3&{. ; 0 3&}.)i.3 4x
       A
    0 1  2
    4 5  6
    8 9 10
    
       B
     3
     7
    11
       NB.   removed.  incorrect problem statement A %. B   NB. use rational arithmetic
       NB. removed.  garbage   116r179 137r179 158r179
    
    
       NB. A is singular, we change the problem.
    
       [A_NONSINGULAR =: 1(<0 0)}A
    1 1  2
    4 5  6
    8 9 10
    
       B %. A_NONSINGULAR
     0
    _1
     2
    We can now drive the c program in bash with

    $ echo 1 1 2 3 4 5 6 7 8 9 10 11 | ./c

    Comments on this post

    • YaR_ agrees
    Last edited by b49P23TIvg; October 22nd, 2012 at 10:05 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Dave
    Explain please little more.
    I can guess, but not sure that understood you correctly.
    Are you proposing to use another way of calculation?
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    Sure! I advertise the j dialect of APL.

    In your case, you're trying to learn c and to write a numerical program in c. My previous post was primarily to adjust the sample problem to agree with yours. I could have edited my first post in this column. That didn't seem nice so I entered a new post. It also happens that

    1) The solution I showed is incorrect, my fault not that of j,

    2) your matrix is singular.
    Code:
       -/ . * A     NB. -/ .* computes the determinant in j
    0
    I will now edit my previous silly post with a new system, then go back to examining your c program.

    Comments on this post

    • YaR_ agrees
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    I conclude that your program works. You simply didn't understand that your example involved a dependent system of equations.

    In this slightly modified version of your program I've included a display function. I sure hope you figure out that functions are useful.
    Code:
    /* $ cc -Wall -g c.c -o c */
    /* $ echo 1 1 2 3 4 5 6 7 8 9 10 11 | ./c   */
    
    #include<stdio.h>
    #include<math.h>
    
    void display(char*title,float*a,int rows,int cols) {
      int i,j;
      printf("\n%s\n",title);
      for (j=0; j<rows; ++j) {
        for(i=0; i<cols; i++)
          printf("%2.3f ",a[j*cols+i]);
        putchar('\n');
      }
    }
    
    int main() {
      float a;
      int i;
      float matrix[15];
      a = i = 0;
      printf("Gauss method\n");
      printf("\nInput:\n");
      for(i = 0; i < 12; i++)
        scanf("%3e", matrix+i);
    
      //Step 1
      /* Checking [0] = 0, if true - replace 1 and 2 row*/
      if(matrix[0] == 0) {
        for(i = 0; i < 4; i++)
          matrix[i+12] = matrix[i];
        for(i = 4; i < 8; i++)
          matrix[i-4] = matrix[i];
        for(i = 4; i < 8; i++)
          matrix[i] = matrix[i+8];
      }
      display("step 1, swap rows to put non-zero at top left",matrix,4,4);
    
      //Step 2
      /* Checking [0] = 0, if true - replace 1 and 3 row*/
      if(matrix[0] == 0) {
        for(i = 0; i < 4; i++)
          matrix[i+12] = matrix[i];
        for(i = 8; i < 12; i++)
          matrix[i-8] = matrix[i];
        for(i = 8; i < 12; i++)
          matrix[i] = matrix[i+4];
      }
      display("step 2, swap rows to put non-zero at top left",matrix,4,4);
    
      //Step 3
      // Lead down [4] to 0
      if(matrix[4] != 0){
        a = - (double) matrix[0] / matrix[4];
        for(i = 4; i < 8; i++)
          matrix[i] = matrix[i] * a + matrix[i-4];
      }
      display("step 3, put a 0 in the first column of the second row",matrix,4,4);
    
      //if(matrix[4] != 0)
      //printf("ERROR");
      //Step 4
      // Lead down [8] to 0
      if(matrix[4] == 0 && matrix[8] != 0){
        a = - (double) matrix[0] / matrix[8];
        for(i = 8; i < 12; i++)
          matrix[i] = matrix[i] * a + matrix[i-8];
      }
      display("step 4, combine rows 0 and 2 to stick a 0 at first column of last row",matrix,4,4);
    
      // Step 5
      // Lead down [9] to 0
      if (matrix[8] == 0 && matrix[4] == 0 && matrix[9] != 0) {
        a = - (double) matrix[5] / matrix[9];
        for(i = 8; i < 12; i++)
          matrix[i] = matrix[i] * a + matrix[i-4];
      } else if(matrix[9] != 0)
        printf("a32 = 0");
      display("step 5, zero the final cell of the lower triangle (excluding the diagonal)",matrix,4,4);
      return 0;
    }

    Comments on this post

    • YaR_ agrees
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0
    Man! Thanks you a lot! I got to do this code for Friday, i stuck there and you help me! Thanks a lot!

IMN logo majestic logo threadwatch logo seochat tools logo