Thread: how to compile the blue part?

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    3

    how to compile the blue part?

    prob2(): By using a pointer to pointer **A and the function malloc()
    allocate the memory for the 4x4 matrix A[][]. By using a pointer *b
    and the function malloc() allocate the memory for the 4-dimensional vector
    b[]. Read the components of A and b from the given input file matrix.dat.
    The last line of the input file contains the components of b. The rows of
    matrix A are the first four lines of the input file. Print the components
    of A[][] row by row, and the components of b[]. Using the function gauss()
    given in the lecture notes week9.txt, solve the system of linear algebraic
    equations A[][]*x[]=b[]. Print the components of the solution vector x[].
    Calculate and print the length of this vector. Free the allocated memory.
    ..


    the code that i constructed is as followed starting with the gauss
    Code:
    double **A, *b;
          int row;
            s=gauss(4,A,b);
            printf("x[]={%7.3f", s);
    
            for(row=0; row<4; row++) free(A[row]);
            printf("\nThe length of x[] is");
            free(A);
            free(b);
            fclose(input);
    }
            return;
    }
    
    
    double gauss(int n, double **A, double *b)
            {
            double one = 1.0, zero=0.0;
            double i, j;
            double s;
            int nm, row, col, Arow;
            nm = n-1;
            if(n==1)
            {
            b[0] /= A[0][0]; A[0][0] = one;
            return;
            }
            for(row=0; row<nm; row++)
            {
            i = A[row][row]; A[row][row] = one;
            for(col=row+1; col<n; col++) A[row][col] /=i;
            b[row] /=i;
            for(Arow=row+1; Arow<n; Arow++)
            {
            j=A[Arow][row]; A[Arow][row]=zero;
            for(col=row+1; col<n; col++) A[Arow][col] -= j * A[row][col];
            b[Arow] -= j * b[row];
            }
            }
            b[nm] /= A[nm][nm];
            for(row=nm-1; row>=0; row--)
            {
            for(col=row+1; col<n; col++) b[row] -=A[row][col]*b[col];
            s=b[row];
            }
            return s;
    Last edited by Salem; 03-10-2010 at 11:33 AM. Reason: Added [code][/code] tags - learn to use them yourself

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Whata do you mean? What's the problem?
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    I tried to printf x[];
    but the result gave me nothing! is there something wrong with "double gauss(int n, double **A, double *b)?

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You never allocate any memory for A and b, only free it.

    Also the code is a bit fragmented, and you should use a [code] [/code] box.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Meh, added tags, but the indentation is still crap.
    SourceForge.net: Indentation - cpwiki
    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.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    i still dont understand...if you were to write it? how would you structure it? since it uses fscanf/sscanf...im confused

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps if the code was indented half-way decent,
    a) you might figure out the answer
    b) people might be more inclined to help you.

    Code:
    double gauss(int n, double **A, double *b)
    {
      double one = 1.0, zero=0.0;
      double i, j;
      double s;
      int nm, row, col, Arow;
    
      nm = n-1;
    
      if(n==1)
      {
        b[0] /= A[0][0]; 
        A[0][0] = one;
        return;
      }
    
      for(row=0; row<nm; row++)
      {
        i = A[row][row]; 
        A[row][row] = one;
        for(col=row+1; col<n; col++) 
          A[row][col] /=i;
        b[row] /=i;
    
        for(Arow=row+1; Arow<n; Arow++)
        {
          j=A[Arow][row]; 
          A[Arow][row]=zero;
          for(col=row+1; col<n; col++) 
            A[Arow][col] -= j * A[row][col];
          b[Arow] -= j * b[row];
        }
      }
    
      b[nm] /= A[nm][nm];
      for(row=nm-1; row>=0; row--)
      {
        for(col=row+1; col<n; col++) 
          b[row] -=A[row][col]*b[col];
        s=b[row];
      }
      return s;
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-27-2005, 10:24 PM
  2. compile once, compile twice ...error
    By Benzakhar in forum Windows Programming
    Replies: 6
    Last Post: 12-28-2003, 06:00 AM
  3. compile prob: sched.h (win32 POSIX threads) - pid_t
    By BrianK in forum Windows Programming
    Replies: 6
    Last Post: 04-11-2003, 05:52 PM
  4. How can I compile C or C++ with Visual Studio .NET?
    By Dakkon in forum C Programming
    Replies: 8
    Last Post: 02-11-2003, 02:58 PM
  5. compile a *.rc file
    By lshome in forum Windows Programming
    Replies: 1
    Last Post: 10-06-2001, 10:59 AM