Thread: I want to understand this code

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    9

    I want to understand this code

    Please Guys, this code is from a math program, i want to understand it:
    at line 5 what is done to rt?
    at line 8 what does continue do?
    at line 9 and 11 what is done to rt?

    and thanks to you all
    Code:
    1    double evaluate(int g) 
    2    { 
    3    int u; 
    4    double rt=0; 
    5    rt=sys[g][nun]; 
    6    for(u=0;u<nun;u++) 
    7    { 
    8    if(g==u) continue; 
    9     rt-=sys[g]*back; 
    10   } 
    11   rt/=sys[g][g]; 
    12   return rt; 
    13   }
    note that sys and back are defined previously as double **sys, *back;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    How about you saying what you think happens (or try running the code in a debugger and seeing what happens)
    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.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    sys is a two-dimensional array. sys[g][nun] holds some value which is assigned to rt in line 5.

    continue will break-out of the for-loop. That one should have been easy to look-up in any begining C++ book. Or, online... cppreference.com is a good place to look-up keywords and standard library functions.

    Your book should have -= too. Or, at least +=.
    x -=2 is the same as x = x - 2.

    You might need to study arrays and pointers too. (And, pointers to arrays.)
    Last edited by DougDbug; 12-15-2003 at 06:47 PM.

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    9
    actually continue will not break out of the loop, but it will go to the next iteration of the for loop, thus skipping the "rt-=sys[g]*back;" part.

  5. #5
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Originally posted by epic
    actually continue will not break out of the loop, but it will go to the next iteration of the for loop, thus skipping the "rt-=sys[g]*back;" part.
    And in this case it is also bad style.

    Code:
    6 for(u=0; u < nun; u++)  
    8   if(g != u)  
    9     rt-= sys[g] * back;
    10
    would have done the trick.
    [code]

    your code here....

    [/code]

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    if you indent the code it might help a little to understand.
    Code:
        
        double evaluate(int g) 
      
       { 
            int u; 
            double rt=0; 
            rt=sys[g][nun]; 
            
            for(u=0;u<nun;u++) { 
                if(g==u) continue; 
                rt-=sys[g]*back; 
            } 
             
             t/=sys[g][g]; 
             return rt; 
      }
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    9
    Thank you very much guys, i didn't expect you to be very kind to help me, Thanks for
    gamer4life687
    darksaidin
    epic
    DougDbug
    and for Salem although you weren't helpful

    I really understood the code now, and specially when i understood the function of "Continue", when darksaidin gave us a similiar code.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > and for Salem although you weren't helpful
    Many others disagree
    But then again, I seldom go for the degree of spoon feeding which the others do, which in the long run doesn't actually help you figure things out for yourself.

    So if you really want to learn, you'll get into the habit of actually trying stuff for yourself, rather than running to the message boards every time you have even the tiniest problem.

    Or carry on as you are and forever remain as your name suggests, a bad programmer.
    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.

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    9
    > Or carry on as you are and forever remain as your name suggests, a bad programmer.

    That's so funny,
    but, you know, when i want to learn, i do what you are saying, i'm even following a great plan to improve my self in many fields : Java, Oracle, assembly(soon), C++(soon)
    but when i don't have time, i ask for help in some clubs. For example the previous code is for a Numerical Methods theorem, i found a similar code over the internet, and i wanted to modify it so it can satify my needs, and it was just two days to submit my work. I needed quick help at that time, and this is what i got with the great people here and in other clubs too. and i have done a great job for my course, that job wasn't possible in this small period of time, to study C++ from zero, until i'm able to write the program. it's really impossible.

    In short, i'm improving myself with a hard-work daily plan in many fields, and i just needed to understand that code quickly for some purposes.

    Thank you
    Yours Sincerely
    BaD pRoGrAmMeR

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Well I wouldn't recommend getting into it with Salem. He knows his stuff and if he tells you to do a certain thing, you probably would be wise in heeding his advice.

    Spoon feeding, although I do it at times, does not help you at all. In fact IMHO sometimes it complicates matters because other people's code can be hard to read even if well commented.
    So you might get confused simply because of the way it was coded and not because of the principle involved.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM