Thread: New coder, simple help please.

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174

    New coder, simple help please.

    I am completely new to C programming, so please go easy on the jargon, but I do accept all criticism.

    I want to make a code that solves a solution to any quadratic ax^2+bx+c. I made an attempt at it, but of course I came short with many errors.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void) {
        float x,a,b,c;
    
        printf("Enter your values of a b c that correspond to the quadratic
    ax^2+bx+c: ", a, b, c);
        scanf("%f %f %f", &a, &b, &c);
    
            x = (-b + sqrt(b*b-4*a*c))/(2*a);
    
            if (b*b >= 4*a*c) {
                    printf("A solution to your quadratic is:%f\n",x);
            else
                    printf("There are no real solutions to your
    quadratic\n");
    }
    return 0;
    }
    Thank you!

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    First problem, is in the first printf(). No need to call printf with the arguments a,b,c since you are not printing any values. a,b,c, haven't even been inputted yet.

    Code:
    printf("Enter your values of a b c that correspond to the quadratic
    ax^2+bx+c:\n ");
    2nd Problem: Do not calculate the solution x before knowing for sure that the determinant(i.e. b^2 - 4ac) is >= 0. If it is negative, sqrt(determinant) is going to fail. Test in an if statement that this is >= 0. Otherwise print something like : there are no real solutions.

    3rd Problem: There are ALWAYS two solutions to a quadratic equation. If the determinant is 0 the TWO SOLUTIONS ARE EQUAL (i.e. x1=x2)

    You are only computing one solution x1 = (-b + sqrt(determinant))/2a but not the second x2 = (-b - sqrt(determinant))/2a
    Last edited by claudiu; 03-21-2010 at 04:50 AM.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Thanks for the reply claudiu.

    This is for school and I wasn't required to give both solutions, but I guess there's no harm in it. This is what I have now after trying to implement your suggestions:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void) {
        float x1,x2,a,b,c;
    
        printf("Enter your values of a b c that correspond to the quadratic
    ax^2+bx+c: \n);
        scanf("%f %f %f", &a, &b, &c);
    
            if (b*b - 4*a*c >= 0) {
                    x1 = (-b + sqrt(b*b-4*a*c))/(2*a);
                    x2 = (-b - sqrt(b*b-4*a*c))/(2*a);
                    printf("The solutions to your quadratic are:%f %f
    \n",x1,x2);
            else
                    printf("There are no real solutions to your
    quadratic\n");
    }
    return 0;
    }

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Looks good to me. Is it working properly?

  5. #5
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Sadly, no
    I get a LOT of errors...



    Code:
    quadratic.c:7:12: error: missing terminating " character
    quadratic.c: In function 'main':
    quadratic.c:7: error: missing terminating " character
    quadratic.c:8: error: 'ax' undeclared (first use in this function)
    quadratic.c:8: error: (Each undeclared identifier is reported only once
    quadratic.c:8: error: for each function it appears in.)
    quadratic.c:8: error: 'bx' undeclared (first use in this function)
    quadratic.c:8: error: expected ')' before ':' token
    quadratic.c:8: error: stray '\' in program
    quadratic.c:14:10: error: missing terminating " character
    quadratic.c:14: error: missing terminating " character
    quadratic.c:15: error: stray '\' in program
    quadratic.c:15:3: error: missing terminating " character
    quadratic.c:15: error: missing terminating " character
    quadratic.c:15: error: 'n' undeclared (first use in this function)
    quadratic.c:16: error: expected ')' before 'else'
    quadratic.c:17:10: error: missing terminating " character
    quadratic.c:17: error: missing terminating " character
    quadratic.c:18: error: stray '\' in program
    quadratic.c:18:12: error: missing terminating " character
    quadratic.c:18: error: missing terminating " character
    quadratic.c:21: error: expected ';' before '}' token
    quadratic.c:21: error: expected declaration or statement at end of input

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Read the error messages: missing terminating " character
    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

  7. #7
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Oh yeah I missed that one ". I don't understand what the other errors mean though: for e.g.
    expected ';' before '}' token
    What does it mean by "it expected" those? Does it mean I'm missing a ; before the } or something?

    I've changed my code to this so far:


    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void) {
        float x1,x2,a,b,c;
    
        printf("Enter your values of a b c that correspond to the quadratic
    ax^2+bx+c: \n");
        scanf("%f %f %f", &a, &b, &c);
    
            if (b*b - 4*a*c >= 0) {
                    x1 = (-b + sqrt(b*b-4*a*c))/(2*a);
                    x2 = (-b - sqrt(b*b-4*a*c))/(2*a);
                    printf("The solutions to your quadratic are:%f %f
    \n",x1,x2) }
            else
                    printf("There are no real solutions to your
    quadratic\n");
    
    return 0;
    }
    But I still seem to keep getting a lot of missing terminating " character. I don't know where I'm missing them...

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do not write your string literals across multiple lines. If you really want to do so, then you should use adjacent string literals instead, e.g.,
    Code:
    printf("Enter your values of a b c that correspond to the quadratic"
           "ax^2+bx+c: \n");
    or use a \ to escape the newline, e.g.,
    Code:
    printf("Enter your values of a b c that correspond to the quadratic\
    ax^2+bx+c: \n");
    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
    Mar 2010
    Location
    Australia
    Posts
    174
    Oh thanks for that! The hundred errors are reduced to two now:

    Code:
    quadratic.c: In function 'main':
    quadratic.c:15: error: expected ';' before '}' token
    I'm using Putty and the code would jump to the next line on its own, unlike notepad which can have a line thousands of characters long.

  10. #10
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Let me refresh. I think I fixed that error that says: expected ';' before '}' token

    So this is the code I have so far:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void) {
        float x1,x2,a,b,c;
    
        printf("Enter your values of a b c that correspond to the"
    "quadratic ax^2+bx+c: \n");
        scanf("%f %f %f", &a, &b, &c);
    
            if (b*b - 4*a*c >= 0) {
                    x1 = (-b + sqrt(b*b-4*a*c))/(2*a);
                    x2 = (-b - sqrt(b*b-4*a*c))/(2*a);
                    printf("The solutions to your quadratic are:%f %f"
    "\n",x1,x2); }
            else
                    printf("There are no real solutions to your"
    "quadratic\n");
    
    return 0;
    }
    And when I try and compile it with:

    Code:
    gcc -Werror -Wall -O -o quadratic quadratic.c
    I get these errors:

    Code:
    /tmp/ccykv6lI.o: In function `main':
    quadratic.c:(.text+0x81): undefined reference to `sqrt'
    quadratic.c:(.text+0xc1): undefined reference to `sqrt'
    collect2: ld returned 1 exit status
    I remember in school they told us to add -ml for math library when using #include <math.h> but when I do this as so:

    Code:
    gcc -Werror -Wall -ml -O -o quadratic quadratic.c
    It says it doesn't recognize -ml.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You have a brace for your if statement that is not properly matched by a closing brace. To match it, introduce a closing brace before the else keyword, and then an opening brace after it.

    Quote Originally Posted by Mentallic
    I'm using Putty and the code would jump to the next line on its own, unlike notepad which can have a line thousands of characters long.
    I am not sure what is the problem, but it is likely that neither PuTTY nor the text editor that you use is imposing a line limit.

    Quote Originally Posted by Mentallic
    It says it doesn't recognize -ml.
    -lm
    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

  12. #12
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Quote Originally Posted by laserlight View Post
    You have a brace for your if statement that is not properly matched by a closing brace. To match it, introduce a closing brace before the else keyword, and then an opening brace after it.
    Is it necessary to have braces around the else part if it's just one line of code?


    Quote Originally Posted by laserlight View Post
    I am not sure what is the problem, but it is likely that neither PuTTY nor the text editor that you use is imposing a line limit.
    As I was typing in PuTTY it reached the end of the screen and jumped to the next line. That is where the problem arose.
    As long as I know a way to counter-act this, I'm happy


    Quote Originally Posted by laserlight View Post
    -lm
    Haha such a simple mistake.

    Thank you so much! My program works now!

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Mentallic
    Is it necessary to have braces around the else part if it's just one line of code?
    No, but it can be a good idea to avoid 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

  14. #14
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    I'll keep that in mind.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM