Thread: I need an explanation as to what this code means. (Beginner)

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    4

    I need an explanation as to what this code means. (Beginner)

    I have a task to find errors in this long line of code in order to correctly calculate the solutions of quadratic equations.

    Code:
    int main(int argc, char *argv[])
    {
        int    validInput, solution_type;
        double a, b, c, root_1, root_2, q;
        
        /***********************
         *  Input / Validation
         ***********************/
        
        /* Check numbers of arguments, and read input */
        validInput = (argc = 4);
        validInput = validInput && scanf(argv[1], "%lf", &a);
        validInput = validInput && scanf(argv[2], "%lf", &b);
        validInput = validInput && scanf(argv[3], "%lf", &c);
        
        /* Calculate roots if input validated */
        if(validInput)
        {
            if(a > 0)

    Is a section of the code (the first section). And as you can probably guess, it goes on to calculate for a > 0 etc...

    I dont really understand what the validinput section is saying? And a, b and c are never defined so Xcode is just saying a,b,c,root1,root2 are uninitialized and I also dont know what means.

    I just need some help really, anything. Do I need to define these values?


    -----------

    I'll post the whole code below which may help, but i dont expect you to look through for errors and what not:


    Code:
    int main(int argc, char *argv[])
    {
        int    validInput, solution_type;
        double a, b, c, root_1, root_2, q;
        
        /***********************
         *  Input / Validation
         ***********************/
        
        /* Check numbers of arguments, and read input */
        validInput = (argc = 4);
        validInput = validInput && scanf(argv[1], "%lf", &a);
        validInput = validInput && scanf(argv[2], "%lf", &b);
        validInput = validInput && scanf(argv[3], "%lf", &c);
        
        /* Calculate roots if input validated */
        if(validInput)
        {
            if(a > 0)
            {
                /* Quadratic formulas can be used, but only
                 if b*b-4*a*c is larger or equal to 0 */
                if(SOLUTION_EXISTS(a,b,c))
                {
                    q=sqrt((b*b)-(4*a*c));
                    root_1=-0.5*(b+q)/a;
                    root_2=-0.5*(b-q)/a;
                    if(fabs(root_1-root_2)>ERROR)
                    {
                        solution_type=SOLUTION_TYPE_TWO;
                    }
                    else
                    {
                        solution_type=SOLUTION_TYPE_ONE;
                    }
                }
                else
                {
                    solution_type=SOLUTION_TYPE_ZERO;
                }
            }
            else
            {
                /* If quadratic formulaes do not apply */
                if((b==0)||(c==0))
                {
                    solution_type=SOLUTION_TYPE_MANY;
                }
                else if(b==0)
                {
                    solution_type=SOLUTION_TYPE_ZERO;
                }
                else if(c==0)
                {
                    root_1=0; root_2=0;
                    solution_type=SOLUTION_TYPE_ONE;
                }
                    else
                {
                        root_1=-(c/b); root_2=0;
                        solution_type=SOLUTION_TYPE_ONE;
                    }
                }
                
                /* Print appropriate message */
                if(solution_type==SOLUTION_TYPE_TWO)
                    printf("Equation has two solutions: x1=%.3lf x2=%.3lf\n", root_1, root_2);
                else if(solution_type==SOLUTION_TYPE_ONE)
                    printf("Equation has one solution: x1=%.3lf\n", root_1);
                else if(solution_type==SOLUTION_TYPE_ZERO)
                    printf("Equation has no solutions\n");
                else
                    printf("Equation has infinitely many solutions\n");
            }
            else
            {
                printf("**************************************\n");
                printf("* !!!  User input error detected !!! *\n");
                printf("*   Execution method: prog2A a b c   *\n");
                printf("* where a, b and c are real numbers  *\n");
                printf("**************************************\n");
            }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ryan Gregg
    I dont really understand what the validinput section is saying? And a, b and c are never defined so Xcode is just saying a,b,c,root1,root2 are uninitialized and I also dont know what means.

    I just need some help really, anything. Do I need to define these values?
    The validInput thing is to validate the input, as you might guess from the name. The idea is to check that there are sufficient command line arguments (hint: there's a bug here that you're probably expected to fix), then parse those command line arguments to be the values of a, b and c (hint: there's a repeated bug in the parsing code). The parsing code also checks that the command line arguments can be validly parsed to be double, hence validInput is still involved.

    a, b and c are defined here:
    Code:
    double a, b, c, root_1, root_2, q;
    Xcode is correct to say that they are uninitialized. If they were initialized, you might see:
    Code:
    double a = 0.0, b = 0.0, c = 0.0, root_1, root_2, q;
    However, you do not need to initialize them since they will receive their values when the command line arguments are parsed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    4
    Thanks for the reply, the thing is I've only just started learning C and this is just an online task to improve my C knowledge but I still don't understand the specific definitions of the words people use. For example:

    "sufficient command line arguments (hint: there's a bug here that you're probably expected to fix), then parse those command line arguments to be the values of a, b and c (hint: there's a repeated bug in the parsing code)"

    When you say there is a bug at the command line argument I don't really know what that means same with parsing part?

    Maybe if you give me an example or just highlight the areas that need work that would help. Thanks

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ryan Gregg
    When you say there is a bug at the command line argument I don't really know what that means same with parsing part?
    The command line arguments refer to how you might provide input to your program when running it from a command prompt. This should have been covered in your learning material, but if it isn't, then read the tutorial on command line arguments.

    The parsing refers to the use of scanf to interpret/convert the command line argument string as a double. You should read what your learning material has to say about scanf and related functions.

    Quote Originally Posted by Ryan Gregg
    Maybe if you give me an example or just highlight the areas that need work that would help.
    I have already highlighted some areas that need work with my hints.

    I suggest that you do your best to spot the errors in the code first, but what you can do is to use your compiler to help you check your corrected version of the code: when you compile at a high warning level, it should report at least some of the mistakes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    88
    Ryan Gregg,
    Tell us what the value of validInput should be before and after each line 11-14

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Ryan Gregg -

    Are you possibly having trouble understanding how it's supposed to work?

    Suppose you wanted to add 4 values, w, x, y, and z, and store them in sum.

    You could do:

    Code:
    sum = w + x + y + z;
    You could also do this:

    Code:
    sum = w;
    sum = sum + x;
    sum = sum + y;
    sum = sum + z;
    Those values are combined arithmetically (added).

    You can also combine logical values, values which are either TRUE or FALSE.

    If w, z, y, and x are now logic values, you might have:

    Code:
    LogicResult = w && x && y && z;     // if w is TRUE AND x is TRUE AND y is TRUE AND z is TRUE, then LogicResult is TRUE.
    And similar to the code variation for the arithmetic combination, you can also have:

    Code:
    LogicResult = w;
    LogicResult = LogicResult && x;
    LogicResult = LogicResult && y;
    LogicResult = LogicResult && z;
    The purpose of the validinput section is to test all of the argument values for validity and combine the result into a single value, validInput

    Understanding what it is supposed to do should help in finding the errors, if you weren't clear on that.

    -

  7. #7
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Code:
           /* Check numbers of arguments, and read input */
        validInput = (argc = 4);
        validInput = validInput && scanf(argv[1], "%lf", &a);
        validInput = validInput && scanf(argv[2], "%lf", &b);
        validInput = validInput && scanf(argv[3], "%lf", &c);
    Think of 'validInput' as being a Boolean value (either it will be 'on' or 'off', 0 or 1 respectively). Your conditional checks (subsequent if's), are relying on 'validInput' being a 1 (or on) to execute their bodies (the parts surrounded in curly brackets).

    Now, in order for 'validInput' to be a useful value, it needs to be able to fail (to be a 0). When using the logical && operator, all compared values must be 1 in order for the output (the variable you are storing the value into) to be 1 as well. In the following piece of code, scanf calls will return the number of arguments correctly translated, so the return should be 1.

    What will the value of 'validInput' be when entering the following code? What do you need to do to make the value of 'validInput' useful (able to fail)? A hint is in the comment I modified above. Also, take a look at the link below, and keep in mind you are meant to be checking the number of command line arguments.

    Code:
        validInput = validInput && scanf(argv[1], "%lf", &a);    validInput = validInput && scanf(argv[2], "%lf", &b);
        validInput = validInput && scanf(argv[3], "%lf", &c);
    
    


    C++ Operator Precedence - cppreference.com
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, the point of the exercise is to spot the errors, which is why no one else has explicitly listed them.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by laserlight View Post
    Yes, the point of the exercise is to spot the errors, which is why no one else has explicitly listed them.
    Did NOT notice that; I deleted my post so, the OP might have missed it.

    Sorry.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What does this code means? Help please.
    By Bepanthol in forum C Programming
    Replies: 9
    Last Post: 08-28-2014, 11:33 AM
  2. Not sure what this code means.
    By kalenda in forum C Programming
    Replies: 5
    Last Post: 06-15-2012, 07:43 PM
  3. BEGINNER - word count C Program explanation
    By Sean Wilson in forum C Programming
    Replies: 7
    Last Post: 12-08-2011, 02:02 AM
  4. What does this code means?
    By junkeat90 in forum C++ Programming
    Replies: 6
    Last Post: 01-14-2008, 05:03 AM
  5. What does this code means??
    By dianazheng in forum C Programming
    Replies: 13
    Last Post: 10-12-2004, 09:45 PM