Thread: Need your help guys :'(

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    12

    Unhappy Need your help guys :'(

    Hello,

    I have this assigment to solve a quadratic equation, and the professor started us with the code..

    He told us to use a,b,c and the Determinant and root1 and root2 and ANS, as the identifiers.

    I tried finishing the code but keep getting errors or it wont work it's 4:55AM and am still trying to figure this out.

    Just to let you know, am new to C programming

    this is my final code that i did with no errors, however it doesn't seem to work quite well.

    I hope to find help here as I have no other way to get help now.

    Code:
    #include <stdio.h>
    #include <math.h>
    const int TWO = 2;
    
    int main()
    {
            int a,b,c, Determinant;
            char ANS;
            float root1;
            float root2;
    
    
            More: printf("Enter any three numbers: \n"); /*if more data to be processed */
            scanf("%d %d %d ", &a, &b, &c);
            Determinant = b*b-4*a*c;
            root1 = -b + sqrt(Determinant) / 2 * a;
            root2 = -b - sqrt(Determinant) / 2 * a;
    
            if (Determinant < 0)
            {
            printf("You cannot have a negative number for the determinant. Non-real result\n");
            }
            else if (Determinant > 0)
            {
            printf("The Determinant equals to: 'Determinant'\n");
    
            }
            else
            {
            printf("root1 is equal to: 'root1'\n");
            printf("root2 is equal to: 'root2'\n");
    }
    
            printf("If more data to be processed, type Y or y: \n");
            printf("else type Q or q to quit: \n");
            scanf("%c", &ANS);
    
            if (ANS == 'Y' || ANS == 'y')
                    goto More;
    
    	else
                    printf("**** End of the program **** \n");
    
            return 0;}
    Hope to get some help Thank you

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    8
    Maybe you should post the errors given by the compiler?! If any, or describe what does not work...

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    12
    Well, after i put the numbers it doesn't work probably

    for example the following

    Code:
    root@o [/home]# ./a.out
    Enter any three numbers:
    0,0,0
    root1 is equal to: 'root1'
    root2 is equal to: 'root2'
    If more data to be processed, type Y or y:
    else type Q or q to quit:
    **** End of the program ****
    
    
    root@o [/home]# ./a.out
    Enter any three numbers:
    5,4,2
    You cannot have a negative number for the determinant. Non-real result
    If more data to be processed, type Y or y:
    else type Q or q to quit:
    **** End of the program ****
    root@o [/home]#
    See it just don't work right i guess, it doesn't wait for a Y or N answer, also it doesn't display the value of root1 or root2

    hope that clears things up

    Thank you

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    8
    Well I'm just a newbie to c programming myself but I have some remarks:

    1) You should enter the data without the commas I think, just use spaces

    2) The printf statement is wrong, should be more like

    printf("root1 is equal to: %f\n", root1)

    3) I don't think goto statements are proper practice, they should be avoided because they can lead to inreadable code

    4) check indentation

    5) why work as root? you should use a normal user account... (but that's lightly off topic... )

    Hope it helps...

  5. #5
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Well, take a look at your printf statements.
    Code:
            printf("root1 is equal to: 'root1'\n");
            printf("root2 is equal to: 'root2'\n");
    Are they really printing out the value of your variables?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    12
    Thank you for the replies

    yes I am a newbie too, it's my 1st class :$

    I did think that there might be a problem with the printf statements, I have changed them but i still can't get it to work.

    here is the code after I changed it

    Code:
    #include <stdio.h>
    #include <math.h>
    const int TWO = 2;
    
    int main()
    {
            int a,b,c, Determinant;
            char ANS;
            float root1;
            float root2;
    
    
            More: printf("Enter any three numbers: \n"); /*if more data to be processed */
            scanf("%d %d %d ", &a, &b, &c);
            Determinant = b*b-4*a*c;
            root1 = -b + sqrt(Determinant) / 2 * a;
            root2 = -b - sqrt(Determinant) / 2 * a;
    
            if (Determinant < 0)
            {
            printf("You cannot have a negative number for the determinant. Non-real result\n");
            }
            else if (Determinant > 0)
            {
            printf("The Determinant equals to: %d\n", Determinant);
    
            }
            else
            {
            printf("root1 is equal to: %f\n", root1);
            printf("root2 is equal to: %f\n", root2);
    }
    
            printf("If more data to be processed, type Y or y: \n");
            printf("else type Q or q to quit: \n");
            scanf("%c", &ANS);
    
            if (ANS == 'Y' || ANS == 'y')
                    goto More;
    
            else
                    printf("**** End of the program **** \n");
    
            return 0;}
    Well I still don't get to type my answer for ( If more data to be processed, type Y or y ) and ( else type Q or q to quit )

    Also here are some numbers i tried and thats what i got :/

    Code:
    root@o [/home]# ./a.out
    Enter any three numbers:
    0,1,3
    root1 is equal to: 0.000000
    root2 is equal to: 0.000000
    If more data to be processed, type Y or y:
    else type Q or q to quit:
    **** End of the program ****
    
    root@o [/home]# ./a.out
    Enter any three numbers:
    2,8,3
    You cannot have a negative number for the determinant. Non-real result
    If more data to be processed, type Y or y:
    else type Q or q to quit:
    **** End of the program ****
    So what part is not working or i might used statements that dont work, cause i read my book and tried to search for what might be right

    thanks again for any help hope to get it working

  7. #7
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Quote Originally Posted by Saarah
    this is my final code that i did with no errors, however it doesn't seem to work quite well.

    Code:
            root1 = -b + sqrt(Determinant) / 2 * a;
            root2 = -b - sqrt(Determinant) / 2 * a;
    I think your assessment of "no errors" needs revisiting :-)

    One thing you should look at is operator precedence. Division and multiplication are carried out before addition and subtraction. Where there are multiple operators having the same precedence they are processed from left to right. In your code, above, the square root of the determinant will be halved first, the result will then be multiplied by a, next b will be negated and then added (or subtracted) to the previous result.

    These extra parentheses (you may keep them as I have plenty to spare) will force the order of processing in a way that you may find useful:
    Code:
            root1 = (-b + sqrt(Determinant)) / (2 * a);
            root2 = (-b - sqrt(Determinant)) / (2 * a);
    Last edited by risby; 09-06-2006 at 04:38 AM.

  8. #8
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Quote Originally Posted by Blur
    Well I'm just a newbie to c programming myself but I have some remarks:

    3) I don't think goto statements are proper practice, they should be avoided because they can lead to inreadable code

    5) why work as root? you should use a normal user account... (but that's lightly off topic... )
    I entirely agree with all your comments blur but I guess, being the first assignment, Saaaarah has being given an environment to work in (root user) and a template program (goto statement). Justification for the apparent abomination of teaching goto's may be that they haven't got to structured loops yet in the course and, like fourteen year olds are taught that electrons are like billiard balls, the goto is a simpler 'structure' and, on face value, easier to understand.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    12
    hello risby

    i meant that when i typed cc program.c -lm it gave me no errors :$

    yes that is right what you wrote, i thought that if i used the brackets it would cause problems, thanks i have edited the code.

    however i still can't figure why it's not working

    i preciate any help

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    12
    this root thing is my friend website, he told me that i can work with it because i couldn't start the putty software and connect it to the university account, i have to use it from the labs at the universitry

    the goto was already written in the code, there were some parts missing that we have to figure out

    we will learn the loops next class

    thanks
    Last edited by Saarah; 09-06-2006 at 04:43 AM.

  11. #11
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Quote Originally Posted by Saarah
    we will learn the loops next class
    Good. Once learnt never ever ever use the goto statement again.

    Unless you have to.

    If, for instance, you learn assembler.

    You have a couple more errors in your code that the compiler couldn't possibly spot.

    [aside]
    I knew a chap who produced a compiler which, if there were no syntax errors to report, wrote out:

    "None of your errors have been found"
    [/aside]

    the argument to the sqrt function is a double whereas you have an int. Since it returns a double I would prefer to see root1 and root2 declared as doubles also.

    Your else if structure means that root1 and root2 are only written out if the determinant is zero. This is not what you intended. Also, for future reference, you should not check a real variable for equality with zero (you need to check whether or not they are within the range of a very tiny bit more than zero and a very tiny bit less than zero).

    Also
    Quote Originally Posted by Saarah
    scanf("%d %d %d ", &a, &b, &c);
    the space at the end of your format specifier may cause some problems when you come to the next scanf at the bottom of your goto loop.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    1. Make Determinant a float
    2. Change the printfs which print it to be %f
    3. if (Determinant < 0) should be before you do any square roots.
    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.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    12
    Thank you all for the help

    after I did some modification i got this final code

    Code:
    #include <stdio.h>
    #include <math.h>
    const int TWO = 2;
    
    int main()
    {
            int a,b,c;
    	float Determinant;
            char ANS;
            double root1;
            double root2;
    
    
            More:
    	printf("Please enter 3 integers separated by a space:\n");
    	printf("These numbers will be calculated as A, B and C in a quadratic equation and the output will be the two calculated roots.\n");
    
    	scanf("%d,%d,%d,",&a,&b,&c);
    	
    	if ((a==0) && (b==0))
    {
    	printf ("Since 0 was your input for A and B, the equation has no solution.\n");
    }
    else
    {
            Determinant = (b*b)-(4*a*c);
    
    }
    
            if (Determinant > 0)
            {
            printf("The Determinant equals to: %f\n", Determinant);
    	root1=(-b+sqrt(Determinant))/(2*a);
    	root2=(-b-sqrt(Determinant))/(2*a);
    	printf("The two roots are %d and %d\n",root1, root2);
            }
            else if (Determinant < 0)
            {
            printf ("There are no real roots.\n");
            }
    
            printf("If more data to be processed, type Y or y: \n");
            printf("else type Q or q to quit: \n");
            scanf("%c",&ANS);
    
            if (ANS == 'Y' || ANS == 'y')
                    goto More;
    	else
    {
                    printf("**** End of the program **** \n");
    }
    
            return 0;}
    Now I did some examples but still I dont get to type the answer for the last two printf statements, and I keep getting ( there are no real roots )

    these are some examples i did

    Code:
    Please enter 3 integers separated by a space:
    These numbers will be calculated as A, B and C in a quadratic equation and the output will be the two calculated roots.
    1 8 4
    There are no real roots.
    If more data to be processed, type Y or y:
    else type Q or q to quit:
    **** End of the program ****
    
    
    Please enter 3 integers separated by a space:
    These numbers will be calculated as A, B and C in a quadratic equation and the output will be the two calculated roots.
    0 0 0
    Since 0 was your input for A and B, the equation has no solution.
    The Determinant equals to: 0.000000
    The two roots are 0 and 2146435072
    If more data to be processed, type Y or y:
    else type Q or q to quit:
    **** End of the program ****

    this is driving me crazy ,, but i feel so close to finish it ..

    also the scanf for the last part doesn't seem to work and doesn't go back to More

    any help on what to do , or if i have some mistakes in the code

    thank you all again

  14. #14
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    I think the line:

    Code:
     const int TWO = 2
    should be placed in the main function. Now, depending on the tutor, constant varianbles in C
    are usually defined using the #define prepocessor direcrive, eg:

    Code:
    #define TWO 2
    I could be wrong, as I do C++ and I never user #define except in macros and header files

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    12
    Quote Originally Posted by swgh
    I think the line:

    Code:
     const int TWO = 2
    should be placed in the main function. Now, depending on the tutor, constant varianbles in C
    are usually defined using the #define prepocessor direcrive, eg:

    Code:
    #define TWO 2
    I could be wrong, as I do C++ and I never user #define except in macros and header files

    Thank you for the reply

    I tried it but it did not work

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hey guys, I'm new!
    By MrDoomMaster in forum C++ Programming
    Replies: 15
    Last Post: 10-31-2003, 05:47 PM
  2. How long have you guys been working with C/C++??
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 08-01-2003, 03:41 PM
  3. Tic Tac Toe -- Can you guys rate this please?
    By Estauns in forum Game Programming
    Replies: 2
    Last Post: 09-15-2001, 10:22 AM
  4. hello guys
    By lupi in forum C++ Programming
    Replies: 4
    Last Post: 09-13-2001, 06:42 AM
  5. hello guys
    By lupi in forum C++ Programming
    Replies: 1
    Last Post: 09-09-2001, 01:00 AM