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

    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
            • O Omar Al Qady

              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 Offline
              C Offline
              chandu004
              wrote on last edited by
              #22

              so, kindly confirm these points for me as per my understanding of the problem. 1. the coefficients may be 0 means, one of the variables may be absent in the expression. 2. the sequence of the variables may change. 3. Will a variable be only 1 character or may be more? 4. what will be the maximum n? (size of expression/number of expressions) because, the solution i have sketched are dependant onthese inputs.

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

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #23

                Omar Al Qady wrote:

                If anyone has any ideas I'd really appreciate it

                Here's a half-baked one:

                void main( void )
                {
                char szTemp[16],
                *pInput[3] = { "3x+2y-2.5z=9",
                "-2x+9y+12z=23.4",
                "4.2x-7y+9.6z=45.3" };

                int x = 0;
                double d1, d2, d3;
                
                for (char \*p = pInput\[0\]; p && \*p != '\\0'; p++)
                {
                    if (isalpha(\*p))
                    {
                        szTemp\[x\] = '\\0';
                        // use atof(szTemp) here
                        x = 0;
                    }
                    else if ('=' == \*p)
                        break;
                    else
                    {
                        szTemp\[x\] = \*p;
                        x++;
                    }
                }
                

                }

                "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                O 1 Reply Last reply
                0
                • C chandu004

                  so, kindly confirm these points for me as per my understanding of the problem. 1. the coefficients may be 0 means, one of the variables may be absent in the expression. 2. the sequence of the variables may change. 3. Will a variable be only 1 character or may be more? 4. what will be the maximum n? (size of expression/number of expressions) because, the solution i have sketched are dependant onthese inputs.

                  -------------------------------------------- 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
                  #24

                  1. Yes, that's possible, but a friend gave an idea of initializing the whole 2d array to equal zero, so when a variable isn't found in an equation the value isn't changed and would be correct for further calculations. 2. Yes, the sequence may change from one equation to another. 3. Variables could be one or more character so I'll just save them in an array of strings. 4. No maximum size was stated, but I don't think that matters as long as the arrays are dynamic, or does it?? :doh:

                  C 1 Reply Last reply
                  0
                  • O Omar Al Qady

                    1. Yes, that's possible, but a friend gave an idea of initializing the whole 2d array to equal zero, so when a variable isn't found in an equation the value isn't changed and would be correct for further calculations. 2. Yes, the sequence may change from one equation to another. 3. Variables could be one or more character so I'll just save them in an array of strings. 4. No maximum size was stated, but I don't think that matters as long as the arrays are dynamic, or does it?? :doh:

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

                    yes, my ideas seem to resemble with those you got yourself or my be from our friends here. go ahead, and you will surely achieve it. we will also co operate withyou.

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

                    O 1 Reply Last reply
                    0
                    • D David Crow

                      Omar Al Qady wrote:

                      If anyone has any ideas I'd really appreciate it

                      Here's a half-baked one:

                      void main( void )
                      {
                      char szTemp[16],
                      *pInput[3] = { "3x+2y-2.5z=9",
                      "-2x+9y+12z=23.4",
                      "4.2x-7y+9.6z=45.3" };

                      int x = 0;
                      double d1, d2, d3;
                      
                      for (char \*p = pInput\[0\]; p && \*p != '\\0'; p++)
                      {
                          if (isalpha(\*p))
                          {
                              szTemp\[x\] = '\\0';
                              // use atof(szTemp) here
                              x = 0;
                          }
                          else if ('=' == \*p)
                              break;
                          else
                          {
                              szTemp\[x\] = \*p;
                              x++;
                          }
                      }
                      

                      }

                      "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

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

                      Good ideas, I'll try to incorporate them into my solution :) Thanks :)

                      1 Reply Last reply
                      0
                      • C chandu004

                        yes, my ideas seem to resemble with those you got yourself or my be from our friends here. go ahead, and you will surely achieve it. we will also co operate withyou.

                        -------------------------------------------- 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
                        #27

                        Working on it now .... :D

                        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