Read coefficients of linear equations from file into 2d array

This is a discussion on Read coefficients of linear equations from file into 2d array within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm working on a program that reads linear equations from a file such as those: Code: 3x+2y-2.5z=9 -2x+9y+12z=23.4 4.2x-7y+9.6z=45.3 ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    Cairo, Egypt
    Posts
    7

    Read coefficients of linear equations from file into 2d array

    Hi,
    I'm working on a program that reads linear equations from a file such as those:
    Code:
    3x+2y-2.5z=9
    -2x+9y+12z=23.4
    4.2x-7y+9.6z=45.3
    The file is supposed to contain n equations with n variables which I would read into a 2d dynamic array of doubles and then I'm supposed to solve the resulting matrix using Gauss's method. I've already implemented Gauss's method but I can't figure out when reading from file how to get only the numbers and the signs for example from the above equations I'm supposed to have the following in the array:
    Code:
    [+3][+2][-2.5]
    [-2][+9][+12]
    [+4.2][-7][+9.6]
    I need the numbers after the equal sign to be stored in a separate n sized array as well.

    If anyone has any ideas I'd really appreciate it

  2. #2
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Let me look at your code. Else, I'd suggest you to use formatted input.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Location
    Cairo, Egypt
    Posts
    7
    What part of the code exactly? It's too large and most if not all of it is irrelevant to this remaining part.
    I just need to read double by double until I reach the equal sign and store the double after it in a n sized array for solutions and when a new line starts I'd move to the next row in the 2d array and repeat the process. Is that possible?

    Thanks for replying

  4. #4
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    If you're sure the input is all correct, ol' fscanf might help you. Or read "double - char - double - char - char - double" (something like this).

  5. #5
    Registered User
    Join Date
    Mar 2008
    Location
    Cairo, Egypt
    Posts
    7
    If I don't know the number of variable but can calculate it once I read the file, can I just loop reading one double and one char for the number of variables without moving to the next line until I pass the = sign?

  6. #6
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Read a double, then a char, then an operator. Keep doing until the operator is '='. The number of variables is the number of times you did that. Read another double. You have everything you need.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Location
    Cairo, Egypt
    Posts
    7

    Smile

    All right then Thank you very much for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read file into array
    By neobarn in forum C Programming
    Replies: 6
    Last Post: 01-09-2009, 12:41 PM
  2. Read .txt file into 2D array.
    By crazygopedder in forum C Programming
    Replies: 11
    Last Post: 10-21-2008, 09:42 PM
  3. read file list into array
    By deltaxfx in forum C Programming
    Replies: 1
    Last Post: 01-07-2006, 11:41 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21