Thread: Solving some problems with C

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

    Solving some problems with C

    Hi everyone. I'm new here.
    I've taken a course of programming. I'm new to this subject. And it's been just two weeks I've started coding. So here are some of problem given by my instructor that I'm having problems with.
    The first problem is finding the root of a quadratic equation ax.x+bx+c=0 using the formula x=-b+-+sqrt(b.b-4ac)/2a.
    This is what I've done. But not working.
    Code:
    
    #include<stdio.h>
    int main()
    {float a,b,c,x1,x2;
    printf("\nenter the coefficient of a,b,c:");
    scanf("%f%f%f",&a,&b,&c);
    x1=-b+sqrt(b*b-4*a*c)/2*a;
    x2=-b-sqrt(b*b-4*a*c)/2*a;
    printf("\n-b+sqrt(b*b-4*a*c)/(2a) = %.2f\n",x1);
    printf("\n-b-sqrt(b*b-4*a*c)/(2a)   = %.2f\n",x2);
    return 0;
    }
    
    Another one is to Convert Celsius temperature to Fahrenheit & Kelvin.
    F=9/5C+32 Celsius to Fahrenheit
    K=C+273.15 Celsius to Kelvin.
    Test the code with 25 degree Celsius.
    This is what I did.

    Code:
    #include<stdio.h>
    int main()
    {float C,F,K;
    printf("\nenter the temperature in Celsius:");
    scanf("%f",&C);
    F=9/5*C+32;
    K=C+27.15;
    printf("\n9/5*C+32 = %.2f\n",F);
    printf("\nC+273.15   = %.2f\n",K);
    return 0;
    }
    But the values I'm getting are wrong.
    Also if someone could show me the code of a sample chart with 3 arrays it'd be help.(like a mark sheet of a student containing name marks & grade)
    I'm new to programming. And I barely have a idea what I'm doing. So, some help would be much appreciated.Thanks in advance.

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Quote Originally Posted by philia View Post
    But the values I'm getting are wrong.
    What did you enter, and what was the result? How did it differ from what you expected?

    For one thing, I can see that 27.15 != 273.15
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    First program:

    You should tell us exactly how it's not working; i.e. input you're entering, output you're expecting, and actual output you're getting.

    Also, neatly formatted and indented code is much nicer to read:

    Code:
    #include <stdio.h>
    
    int main()
    {
        float a,b,c,x1,x2;
    
        printf("\nenter the coefficient of a,b,c:");
        scanf("%f%f%f",&a,&b,&c);
    
        x1=-b+sqrt(b*b-4*a*c)/2*a;
        x2=-b-sqrt(b*b-4*a*c)/2*a;
    
        printf("\n-b+sqrt(b*b-4*a*c)/(2a) = %.2f\n",x1);
        printf("\n-b-sqrt(b*b-4*a*c)/(2a)   = %.2f\n",x2);
    
        return 0;
    }
    Check your warnings:

    Code:
    /*
    main.c||In function 'main':|
    main.c|10|warning: implicit declaration of function 'sqrt'|
    main.c|10|warning: incompatible implicit declaration of built-in function 'sqrt'|
    ||=== Build finished: 0 errors, 2 warnings ===|
    */
    You should be including "math.h".

    Also, you need to be careful of your input - the quadratic equation could potentially yield an imaginary number.

    Quote Originally Posted by 7.12.7.5, paragraph 2
    The sqrt functions compute the nonnegative square root of x. A domain error occurs if
    the argument is less than zero.
    Finally, and this is the most important bit - be very careful that your equation takes order of operations correctly into account. I'd suggest grouping the entire numerator in parathesis, and the entire denominator in parenthesis.



    Second program:

    Ditto above advice, on asking clear questions and telling us exactly what is wrong.

    Also ditto on the neatly formatted and indented code.

    Code:
    #include<stdio.h>
    
    int main()
    {
        float C,F,K;
    
        printf("\nenter the temperature in Celsius:");
        scanf("%f",&C);
    
        F=9/5*C+32;
        K=C+27.15;
    
        printf("\n9/5*C+32 = %.2f\n",F);
        printf("\nC+273.15   = %.2f\n",K);
    
        return 0;
    }
    This line of code is where you want to start looking:

    Code:
    F=9/5*C+32;
    9/5 is integer division, and as such yields a (truncated) integer result. In this case, 9/5 would be equal to 1. If you want a floating point result, at least one of the operands has to be floating point. This could be as simple as changing it to "9.0/5.0",

    The Kelvin calculation also has an error, pointed out by CodeMonkey above.



    Also if someone could show me the code of a sample chart with 3 arrays it'd be help.(like a mark sheet of a student containing name marks & grade)
    I'm not quite sure what you're asking here. Can you clarify?

  4. #4
    Registered User
    Join Date
    Oct 2014
    Posts
    33
    Thanks a lot for the answers.

    Quote Originally Posted by Matticus View Post
    First program:

    You should tell us exactly how it's not working; i.e. input you're entering, output you're expecting, and actual output you're getting.

    Also, neatly formatted and indented code is much nicer to read:

    Code:
    #include <stdio.h>
    
    int main()
    {
        float a,b,c,x1,x2;
    
        printf("\nenter the coefficient of a,b,c:");
        scanf("%f%f%f",&a,&b,&c);
    
        x1=-b+sqrt(b*b-4*a*c)/2*a;
        x2=-b-sqrt(b*b-4*a*c)/2*a;
    
        printf("\n-b+sqrt(b*b-4*a*c)/(2a) = %.2f\n",x1);
        printf("\n-b-sqrt(b*b-4*a*c)/(2a)   = %.2f\n",x2);
    
        return 0;
    }
    Check your warnings:

    Code:
    /*
    main.c||In function 'main':|
    main.c|10|warning: implicit declaration of function 'sqrt'|
    main.c|10|warning: incompatible implicit declaration of built-in function 'sqrt'|
    ||=== Build finished: 0 errors, 2 warnings ===|
    */
    You should be including "math.h".

    Also, you need to be careful of your input - the quadratic equation could potentially yield an imaginary number.



    Finally, and this is the most important bit - be very careful that your equation takes order of operations correctly into account. I'd suggest grouping the entire numerator in parathesis, and the entire denominator in parenthesis.
    Tried it this way by using math.h & parenthesis. But still getting the wrong output.
    Code:
    #include <stdio.h>
    #include#<math.h>
    int main()
    {
        float a,b,c,x1,x2;
    
        printf("\nenter the coefficient of a,b,c:");
        scanf("%f%f%f",&a,&b,&c);
    
        x1=(-b+(sqrt(b*b-4*a*c)))/(2*a);
        x2=(-b-(sqrt(b*b-4*a*c)))/(2*a);
    
        printf("\n(-b+(sqrt(b*b-4*a*c)))/(2*a) = %.2f\n",x1);
        printf("\n-b-sqrt(b*b-4*a*c)/(2*a)   = %.2f\n",x2);
    
        return 0;
    }
    The value I'm getting is -.1#J





    Quote Originally Posted by Matticus View Post
    Second program:

    Ditto above advice, on asking clear questions and telling us exactly what is wrong.

    Also ditto on the neatly formatted and indented code.

    Code:
    #include<stdio.h>
    
    int main()
    {
        float C,F,K;
    
        printf("\nenter the temperature in Celsius:");
        scanf("%f",&C);
    
        F=9/5*C+32;
        K=C+27.15;
    
        printf("\n9/5*C+32 = %.2f\n",F);
        printf("\nC+273.15   = %.2f\n",K);
    
        return 0;
    }
    This line of code is where you want to start looking:

    Code:
    F=9/5*C+32;
    9/5 is integer division, and as such yields a (truncated) integer result. In this case, 9/5 would be equal to 1. If you want a floating point result, at least one of the operands has to be floating point. This could be as simple as changing it to "9.0/5.0",

    The Kelvin calculation also has an error, pointed out by CodeMonkey above.
    Ok did this.
    Code:
    #include<stdio.h>
    int main()
    {float C,F,K;
    printf("\nenter the temperature in Celsius:");
    scanf("%f",&C);
    F=9/5*C+32;
    K=C+273.15;
    printf("\n9.0/5.0*C+32 = %.2f\n",F);
    printf("\nC+273.15   = %.2f\n",K);
    return 0;
    }
    And now only the value in Fahrenheit is incorrect.I'm getting57.00 while the value should be 77.00.





    I'm not quite sure what you're asking here. Can you clarify?
    Okay. My problem was "make a mark sheet for three students which shows their first name, raw marks & converted marks. The exam is based on 70 marks but it only contributes 20% fir the course. The names of the students are Peter, Daniel & Andy. The got 56, 48 & 64 respectively. Which is 16%, 13.71& & 18.29% respectively. [Hint: Create three arrays each for name, raw marks & converted marks]"

    And I don't know how I did this before but I don't seem to be able to post neatly formatted code. Sorry for that. I'm completely new here.
    Last edited by philia; 10-14-2014 at 08:22 AM.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The value I'm getting is -.1#J
    It sounds like you might be feeding the "sqrt()" function an argument that is less than zero (I mentioned this in my previous post). But without providing your input (as I've asked), there's no way I can confirm. Check this out for yourself. You need to handle the special case when the value to "sqrt()" is less than zero.

    And now only the value in Fahrenheit is incorrect.I'm getting57.00 while the value should be 77.00.
    Based on your code, you changed the equation in the print statement instead of the actual equation itself. It's the actual equation that generates the result. Whatever is in the print statement is simply for display to the user (i.e. has no effect on the result).

    Okay. My problem was "make a mark sheet for three students which shows their first name, raw marks & converted marks. The exam is based on 70 marks but it only contributes 20% fir the course. The names of the students are Peter, Daniel & Andy. The got 56, 48 & 64 respectively. Which is 16%, 13.71& & 18.29% respectively. [Hint: Create three arrays each for name, raw marks & converted marks]"
    The hint provides all you should need to start the program. Are you familiar with arrays? If not, start reading up and practicing with them until you get comfortable using them. Otherwise, start with a simple program that declares the necessary arrays, initializes them to the given value, and prints to the screen (this is to test they are initialized properly). You can develop the program from there.

  6. #6
    Registered User
    Join Date
    Oct 2014
    Posts
    33
    Quote Originally Posted by Matticus View Post
    It sounds like you might be feeding the "sqrt()" function an argument that is less than zero (I mentioned this in my previous post). But without providing your input (as I've asked), there's no way I can confirm. Check this out for yourself. You need to handle the special case when the value to "sqrt()" is less than zero.
    The input I gave was always positive integer for all a,b &c.



    Quote Originally Posted by Matticus View Post
    Based on your code, you changed the equation in the print statement instead of the actual equation itself. It's the actual equation that generates the result. Whatever is in the print statement is simply for display to the user (i.e. has no effect on the result).
    Got it. It's correct now. Thanks a lot.



    Quote Originally Posted by Matticus View Post
    The hint provides all you should need to start the program. Are you familiar with arrays? If not, start reading up and practicing with them until you get comfortable using them. Otherwise, start with a simple program that declares the necessary arrays, initializes them to the given value, and prints to the screen (this is to test they are initialized properly). You can develop the program from there.
    I'm not really familiar with arrays. That's why I was asking if someone could show me a sample. I really suck at coding.
    Thanks for the help, man. You're amazing.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The input I gave was always positive integer for all a,b &c.
    It doesn't matter that you entered all positive values. There is subtraction taking place under the square root, so you need to make sure that:

    b2 > 4*a*c

    Otherwise, you'll be passing a negative value to the "sqrt()" function, which is a no-no.

    Got it. It's correct now. Thanks a lot.
    You're welcome.

    I'm not really familiar with arrays. That's why I was asking if someone could show me a sample. I really suck at coding.
    If you have a text book or course material, go over arrays. You can also search for tutorials (we have one here), but those aren't as comprehensive in my opinion.

    Read about them, type out a few examples (do not just copy and paste, but hand-type each character - this will help you get familiar with the syntax and see the details), and run them. Practice practice practice!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need a help in solving c program
    By CHERYL123 in forum C Programming
    Replies: 16
    Last Post: 12-16-2013, 02:01 PM
  2. Simplifying Numerical Solving before actual solving
    By Vespasian in forum Tech Board
    Replies: 3
    Last Post: 10-14-2012, 11:39 AM
  3. Can anyone help me solving that Q?
    By Amjad Salman in forum C Programming
    Replies: 1
    Last Post: 03-04-2012, 11:50 AM
  4. Solving the exercise
    By skiabox in forum C Programming
    Replies: 7
    Last Post: 10-29-2010, 02:01 PM
  5. Help solving this problem
    By marcelomdsc in forum C++ Programming
    Replies: 6
    Last Post: 05-13-2008, 08:32 AM