Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Read coefficients of linear equations into 2d array

Read coefficients of linear equations into 2d array

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialdata-structures
27 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Omar Al Qady

    Hi, I'm working on a program that reads linear equations from a file such as those:

    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:

    [+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 :)

    C Offline
    C Offline
    chandu004
    wrote on last edited by
    #2

    here i have a rough idea.

    Omar Al Qady wrote:

    3x+2y-2.5z=9-2x+9y+12z=23.44.2x-7y+9.6z=45.3

    fscanf(fp,"%fx%fy%fz=5f",el1,el2,el3,el4); should fetch you the required values into el1....el4. try it out if it does not work we shall find out some other way. good luck.

    -------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.

    O 1 Reply Last reply
    0
    • O Omar Al Qady

      Hi, I'm working on a program that reads linear equations from a file such as those:

      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:

      [+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 :)

      S Offline
      S Offline
      suthakar56
      wrote on last edited by
      #3

      hi, i have a small idea that, based on the ascii value you can separte the number,x,y,z and = sign.For x,y,z you just read each character and check with in the range(ie 120,121,122,61 for x,y,z,= respectively) and you can use isalpha() for whether the reading data is albhapet or numeric. regards, Suthakar

      O 1 Reply Last reply
      0
      • C chandu004

        here i have a rough idea.

        Omar Al Qady wrote:

        3x+2y-2.5z=9-2x+9y+12z=23.44.2x-7y+9.6z=45.3

        fscanf(fp,"%fx%fy%fz=5f",el1,el2,el3,el4); should fetch you the required values into el1....el4. try it out if it does not work we shall find out some other way. good luck.

        -------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.

        O Offline
        O Offline
        Omar Al Qady
        wrote on last edited by
        #4

        The problem is that the variables aren't always x,y,z because the program should solve n equations with n variables, so they can be more than 3 or less, so I can't assume when reading the data it's only three variables. I thought about something but I just can't implement, if I can read double by double into the array until I reach the equal sign then the double after is read into a different n sized array for the solutions, and when a new line is started I'd repeat the process in the next row of the 2d array. I don't know if what I said above is even possible but if you can help me in implementing it I'd be grateful :) Thanks for your reply :) and thanks in advance for any additional help you can give :)

        C 1 Reply Last reply
        0
        • S suthakar56

          hi, i have a small idea that, based on the ascii value you can separte the number,x,y,z and = sign.For x,y,z you just read each character and check with in the range(ie 120,121,122,61 for x,y,z,= respectively) and you can use isalpha() for whether the reading data is albhapet or numeric. regards, Suthakar

          O Offline
          O Offline
          Omar Al Qady
          wrote on last edited by
          #5

          So maybe I'd read each line into a string, and then go through it char by char checking for letters and = and if not I'd store in the 2d double array and after I find a letter I move to the next column, and so on till the = and store what's after it in the solutions array. I'm getting a vague idea now of how to do this, but are the signs and decimal points also readable and convertible in the same way? For example when reading -2.9 , the - & . wouldn't fall in the range for the letters and the = but can I convert them along with the numbers into a double??

          L 1 Reply Last reply
          0
          • O Omar Al Qady

            The problem is that the variables aren't always x,y,z because the program should solve n equations with n variables, so they can be more than 3 or less, so I can't assume when reading the data it's only three variables. I thought about something but I just can't implement, if I can read double by double into the array until I reach the equal sign then the double after is read into a different n sized array for the solutions, and when a new line is started I'd repeat the process in the next row of the 2d array. I don't know if what I said above is even possible but if you can help me in implementing it I'd be grateful :) Thanks for your reply :) and thanks in advance for any additional help you can give :)

            C Offline
            C Offline
            chandu004
            wrote on last edited by
            #6

            if i understood your problem correctly, then open the src file in read mode, open another dest file in write mode, copy character by character from src file to dest file. if any alphabet is read, then write some standard character like ':' or any other char which is not in your expressions. for example, 3x-2y+7z-3q=2.5 will become 3:-2:+7:-3:=2.5 then do something like fscanf("%f:",&el1); still some more logics we may have to implement like finding size of the 2d array etc. we can work around them subsequently if u have understood till here.

            -------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.

            L 1 Reply Last reply
            0
            • O Omar Al Qady

              Hi, I'm working on a program that reads linear equations from a file such as those:

              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:

              [+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 :)

              _ Offline
              _ Offline
              _Superman_
              wrote on last edited by
              #7

              First read the file one line at a time. You could use ifstream::getline[^] for this. Then you could use the strtok_s function[^] to separate the coefficients.

              «_Superman_» I love work. It gives me something to do between weekends.

              O 1 Reply Last reply
              0
              • C chandu004

                if i understood your problem correctly, then open the src file in read mode, open another dest file in write mode, copy character by character from src file to dest file. if any alphabet is read, then write some standard character like ':' or any other char which is not in your expressions. for example, 3x-2y+7z-3q=2.5 will become 3:-2:+7:-3:=2.5 then do something like fscanf("%f:",&el1); still some more logics we may have to implement like finding size of the 2d array etc. we can work around them subsequently if u have understood till here.

                -------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #8

                I don't think so. First you don't need to create a file in order to read and parse an existing file. Second, there is no guarantee the variables are ordered the same in every line and/or a coefficient of zero gets mentioned at all, so one really has to check the variable names. All it takes is reading the characters of a line, then parse it into numbers, identifiers and operators. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


                C 1 Reply Last reply
                0
                • O Omar Al Qady

                  So maybe I'd read each line into a string, and then go through it char by char checking for letters and = and if not I'd store in the 2d double array and after I find a letter I move to the next column, and so on till the = and store what's after it in the solutions array. I'm getting a vague idea now of how to do this, but are the signs and decimal points also readable and convertible in the same way? For example when reading -2.9 , the - & . wouldn't fall in the range for the letters and the = but can I convert them along with the numbers into a double??

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #9

                  yes, that is about right, except you are not sure the variables will appear in the same order every time (one even could be omitted when its coefficient happens to be zero). So you must parse the line into numbers, identifier names and operators; then keep an array of known identifiers (=variable names) which leads you to the right column in the 2D matrix. :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


                  O 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    I don't think so. First you don't need to create a file in order to read and parse an existing file. Second, there is no guarantee the variables are ordered the same in every line and/or a coefficient of zero gets mentioned at all, so one really has to check the variable names. All it takes is reading the characters of a line, then parse it into numbers, identifiers and operators. :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


                    C Offline
                    C Offline
                    chandu004
                    wrote on last edited by
                    #10

                    Luc Pattyn wrote:

                    there is no guarantee the variables are ordered the same in every line

                    yes it is a valid point. even iam thinking in that perspective.i assumed the same sequence of variables while posting. let the op confirm this point, i.e. if the same sequence is guarenteed or not..

                    -------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.

                    O 1 Reply Last reply
                    0
                    • L Luc Pattyn

                      yes, that is about right, except you are not sure the variables will appear in the same order every time (one even could be omitted when its coefficient happens to be zero). So you must parse the line into numbers, identifier names and operators; then keep an array of known identifiers (=variable names) which leads you to the right column in the 2D matrix. :)

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


                      O Offline
                      O Offline
                      Omar Al Qady
                      wrote on last edited by
                      #11

                      I was just thinking about what would happen if a coefficient is zero and I just freaked out and thought I'd skip it before your reply :D But I'll try your way and see how it goes :) Thanks a lot :)

                      1 Reply Last reply
                      0
                      • _ _Superman_

                        First read the file one line at a time. You could use ifstream::getline[^] for this. Then you could use the strtok_s function[^] to separate the coefficients.

                        «_Superman_» I love work. It gives me something to do between weekends.

                        O Offline
                        O Offline
                        Omar Al Qady
                        wrote on last edited by
                        #12

                        Yes I think I'll try getline() and save each line into a string in a string array, and then parse it like I was advised to do above and save the coefficients in the 2d array while checking for variable in the variable array each time. I think this would be the way to go :) Thanks for replying :)

                        L 1 Reply Last reply
                        0
                        • C chandu004

                          Luc Pattyn wrote:

                          there is no guarantee the variables are ordered the same in every line

                          yes it is a valid point. even iam thinking in that perspective.i assumed the same sequence of variables while posting. let the op confirm this point, i.e. if the same sequence is guarenteed or not..

                          -------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.

                          O Offline
                          O Offline
                          Omar Al Qady
                          wrote on last edited by
                          #13

                          No the sequence is not constant and there might be coefficients of 0 which would the variable would not be at all present in the line.

                          C 1 Reply Last reply
                          0
                          • O Omar Al Qady

                            No the sequence is not constant and there might be coefficients of 0 which would the variable would not be at all present in the line.

                            C Offline
                            C Offline
                            chandu004
                            wrote on last edited by
                            #14

                            fine then, i will also work aroundit. as of now, do as luc pattyn suggested. if you get the solution please specify it here. otherwise i shall attempt it on monday. all the best.

                            -------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.

                            O 1 Reply Last reply
                            0
                            • C chandu004

                              fine then, i will also work aroundit. as of now, do as luc pattyn suggested. if you get the solution please specify it here. otherwise i shall attempt it on monday. all the best.

                              -------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.

                              O Offline
                              O Offline
                              Omar Al Qady
                              wrote on last edited by
                              #15

                              OK then, thank you all for your help and I'll post here if I get it done :)

                              L 1 Reply Last reply
                              0
                              • O Omar Al Qady

                                OK then, thank you all for your help and I'll post here if I get it done :)

                                L Offline
                                L Offline
                                Lost User
                                wrote on last edited by
                                #16

                                Hmm, How about a RegExp? You could scrub data into a tonenized string then convert it to an array. There is an open source mathematics program called 'sage', I would look atthe aproach they are implimenting. Also, you might find other good ideas by searching for OpenML. There are many examples supported by many frameworks.

                                O 1 Reply Last reply
                                0
                                • O Omar Al Qady

                                  Yes I think I'll try getline() and save each line into a string in a string array, and then parse it like I was advised to do above and save the coefficients in the 2d array while checking for variable in the variable array each time. I think this would be the way to go :) Thanks for replying :)

                                  L Offline
                                  L Offline
                                  Lost User
                                  wrote on last edited by
                                  #17

                                  Oh yeah, forgot to mention another idea. Create a Red / Black tree from the code. Look at examples for expanding lambda expressions.

                                  O 1 Reply Last reply
                                  0
                                  • L Lost User

                                    Hmm, How about a RegExp? You could scrub data into a tonenized string then convert it to an array. There is an open source mathematics program called 'sage', I would look atthe aproach they are implimenting. Also, you might find other good ideas by searching for OpenML. There are many examples supported by many frameworks.

                                    O Offline
                                    O Offline
                                    Omar Al Qady
                                    wrote on last edited by
                                    #18

                                    I'll look into the source of 'sage' then and see if it's of any help. The other things you mentioned I'm afraid I don't understand as I am not a very advanced programmer (yet I hope :) ). Thanks for replying :)

                                    1 Reply Last reply
                                    0
                                    • L Lost User

                                      Oh yeah, forgot to mention another idea. Create a Red / Black tree from the code. Look at examples for expanding lambda expressions.

                                      O Offline
                                      O Offline
                                      Omar Al Qady
                                      wrote on last edited by
                                      #19

                                      I don't know about this either, so I'll try working with the hints and ideas from above and if it doesn't work out I'll research this and work on it :) Thanks for your ideas :)

                                      C 1 Reply Last reply
                                      0
                                      • O Omar Al Qady

                                        I don't know about this either, so I'll try working with the hints and ideas from above and if it doesn't work out I'll research this and work on it :) Thanks for your ideas :)

                                        C Offline
                                        C Offline
                                        chandu004
                                        wrote on last edited by
                                        #20

                                        hope your problem is solved. i did some paperwork onit yesterday and got some ideas. do you require any more discussion on that today?

                                        -------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.

                                        O 1 Reply Last reply
                                        0
                                        • C chandu004

                                          hope your problem is solved. i did some paperwork onit yesterday and got some ideas. do you require any more discussion on that today?

                                          -------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.

                                          O Offline
                                          O Offline
                                          Omar Al Qady
                                          wrote on last edited by
                                          #21

                                          Actually I was very busy yesterday, so I didn't have time to implement the ideas that I got from the discussion here yet but I'll start working on it today and let you know how it works out. Post your ideas anyway they might help me now :) I need all the help I can get :) Thank you for taking the time to think about the problem :)

                                          C 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups