KLMM
KLMM

Reputation: 93

C program to find roots error

I am writing a function in C with the below specifications:

float find_root(float a, float b, float c, float p, float q);

find_root takes the coefficients a,b,c of a quadratic equation and an interval (p, q). It will return the root of this equation in the given interval.

For example: find_root(1, -8, 15, 2, 4) should produce a root "close to" 3.0

I have written the below code, and I don't understand why it doesn't work:

#include<stdio.h>
#include<math.h>

main()
{
    printf("Hello World");
}

float find_root(float a, float b, float c, float p, float q) {

    float d,root1,root2;  
    d = b * b - 4 * a * c;
    root1 = ( -b + sqrt(d)) / (2* a);
    root2 = ( -b - sqrt(d)) / (2* a);

    if (root1<=q || root1>=p)
    {
        return root1;
    }
    return root2;
}

Please let me know what the error is.

Upvotes: 0

Views: 196

Answers (4)

Sourav Ghosh
Sourav Ghosh

Reputation: 134336

Your program doesn't work, because, you never called find_root() from your main().

find_root() is not suppossed to run all-by-itself. Your program statrs execution from main(). You need to call your sub-function from main() in order to make them execute.

Change your main to have a call to find_root(), something like below.

int main()                              //put proper signature
{

float anser = 0;

answer = find_root(1, -8, 15, 2, 4);   //taken from the question
printf("The anser is %f\n", answer);        //end with a \n, stdout is line buffered

return 0;                                 //return some value, good practice

}

Then, compile the program like

gcc -o output yourfilename.c -lm

Apart from this, for the logical issue(s) in find_root() function, please follow the way suggested by Mr. @paxdiablo.

Upvotes: 2

Your program starts at main by definition.

Your main function is not calling find_root but it should.

You need to compile with all warnings & debug info (gcc -Wall -Wextra -g) then use a debugger (gdb) to run your code step by step to understand the behavior of your program, so compile with

gcc -Wall -Wextra -g yoursource.c -lm -o yourbinary

or with

clang -Wall -Wextra -g yoursource.c -lm -o yourbinary

then learn how to use gdb (e.g. run gdb ./yourbinary ... and later ./yourbinary without a debugger)

Then you'll think and improve the source code and recompile it, and debug it again. And repeat that process till you are happy with your program.

BTW, you'll better end your printf format strings with \n or learn about fflush(3)

Don't forget to read the documentation of every function (like printf(3) ...) that you are calling.

You might want to give some arguments (thru your main(int argc, char**argv) ...) to your program. You could use atof(3) to convert them to a double

Read also about undefined behavior, which you should always avoid.

BTW, you can use any standard C compiler (and editor like emacs or gedit) for your homework, e.g. use gcc or clang on your Linux laptop (then use gdb ...). You don't need a specific seashell

Upvotes: 1

Arun A S
Arun A S

Reputation: 7006

Change this condition

if (root1<=q || root1>=p)

to

if (root1<=q && root1>=p)

otherwise if anyone of the conditions is satisfied, root1 will be returned and root2 will almost never be returned. Hope this fixes your problem.

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 881543

For that data, your two roots are 5 and 3. With p == 2 and q == 4:

if (root1<=q || root1>=p)

becomes:

if (5<=4 || 5>=2)

which is true, so you'll get 5.

The if condition you want is:

if ((p <= root1) && (root1 <= q))

as shown in the following program, that produces the correct 3:

#include<stdio.h>
#include<math.h>

float find_root (float a, float b, float c, float p, float q) {
    float d,root1,root2;

    d = b * b - 4 * a * c;
    root1 = ( -b + sqrt(d)) / (2* a);
    root2 = ( -b - sqrt(d)) / (2* a);

    if ((p <= root1) && (root1 <= q))
        return root1;

    return root2;
}

int main (void) {
    printf ("%f\n", find_root(1, -8, 15, 2, 4));
    return 0;
}

That's the logic errors with your calculations of the roots.

Just keep in mind there are other issues with your code.

You need to ensure you actually call the function itself, your main as it stands does not.

It also wont produce a value within the p/q bounds, instead it will give you the first root if it's within those bounds, otherwise it'll give you the second root regardless of its value.

You may want to catch the situation where d is negative, since you don't want to take the square root of it:

a = 1000, b = 0, c = 1000: d <- -4,000,000

And, lastly, if your compiler is complaining about not being able to link sqrt (as per one of your comments), you'll probably find you can fix that by specifying the math library, something like:

gcc -o myprog myprog.c -lm

Upvotes: 1

Related Questions