Thread: Read coefficients of linear equations from file and use jacobi method

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    6

    Read coefficients of linear equations from file and use jacobi method

    hi;
    i did a function that get inputs of equation from user but i want to get from file


    Code:
    void getInput( int numUnKnowns, double** mat ){
        int i, j;
    
    
        printf
        (
    	"\nEnter values for the specified row and column below ...\n"
    	"(The last column is the value for the RHS of the equation.)\n"
        );
    
    
        for( i = 0 ; i < numUnKnowns ; i++ )
        {
    	mat[i] = (double*) malloc( (numUnKnowns+1)*sizeof(double) );
    	puts("");
    	for( j = 0 ; j < numUnKnowns+1 ; j++ )
    	{
    	    printf("matrix[%d][%d] : ", i, j);
    	    if( scanf("%lf", &mat[i][j]) != 1 )
    	    {
    		--j;
    		puts("Bad entry ... try again with a 'real' number.");
    	    }
    	    while( getchar() != '\n' ) ; /* flush stdin ... */
    	}
        }
    
    
        printf("\nThe matrix entered:\n\n");
        for( i = 0 ; i < numUnKnowns ; i++ )
        {
    	for( j = 0 ; j < numUnKnowns+1 ; j++ ) printf("%+9f ", mat[i][j]);
    	puts("");
        }
    
    
        printf("\nPress 'Enter' to start iteration ... ");
        getchar();
    }
    let file :
    7a - 2b + c + 2d = 3
    2a + 8b + 3c + d = -2
    -a + 5c + 2d = 5
    2b - c + 4d = 4

    can any one help me how function read from file.
    thanks

  2. #2
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Is this for a homework assignment or what? If it's for a homework assignment then fine, otherwise the Jacobi method is a waste of time. If you're going to use something that updates each value one by one then at least use Gauss-Seidel or Successive Over-relaxation (SOR).

    Looking at the system of equations you provided, I'm not sure the diagonal values are prominent enough to converge using any of those methods anyhow, you might need to use Gaussian elimination.

    Anyway, concerning your original question, you could make things much easier on yourself if you had a file like this:
    7 2 1 2 3
    2 8 3 1 -2
    -1 0 5 2 5
    0 2 -1 4 4

    Then you could use getline using a space as the delimiter to split those coefficients up into individual values, converting from string to a numerical type, of course.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    Quote Originally Posted by Epy View Post
    Is this for a homework assignment or what? If it's for a homework assignment then fine, otherwise the Jacobi method is a waste of time. If you're going to use something that updates each value one by one then at least use Gauss-Seidel or Successive Over-relaxation (SOR).

    Looking at the system of equations you provided, I'm not sure the diagonal values are prominent enough to converge using any of those methods anyhow, you might need to use Gaussian elimination.

    Anyway, concerning your original question, you could make things much easier on yourself if you had a file like this:
    7 2 1 2 3
    2 8 3 1 -2
    -1 0 5 2 5
    0 2 -1 4 4

    Then you could use getline using a space as the delimiter to split those coefficients up into individual values, converting from string to a numerical type, of course.

    yes for a homework
    Gauss-Seidel or Successive Over-relaxation (SOR) are better than jacobi
    In homework assume for for strictly diagonally dominant matrices.
    just i want to read from file that have linear equations which are strictly diagonall

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This looks like C, not C++. Did you post it in the wrong section?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    Quote Originally Posted by Elysia View Post
    This looks like C, not C++. Did you post it in the wrong section?
    yes in C
    thanks

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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. Jacobi Iteration Method
    By elleshlar in forum C Programming
    Replies: 6
    Last Post: 05-10-2011, 05:32 PM
  2. Read coefficients of linear equations from file into 2d array
    By omaralqady in forum C++ Programming
    Replies: 6
    Last Post: 06-20-2009, 07:39 AM
  3. LINEAR EQUATIONS please help
    By apple_ranger in forum C Programming
    Replies: 18
    Last Post: 09-08-2008, 05:49 AM
  4. MPI - linear pipeline solution for jacobi iteration
    By eclipt in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2006, 05:25 AM
  5. Linear equations
    By amandad40 in forum C Programming
    Replies: 5
    Last Post: 10-24-2002, 09:06 AM