1

I've searched the internet for my problem but it won't get solved. I have the following function:

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

int solve(double a, double b, double c, double *x1, double *x2){
    *x1=(-b+sqrt(pow(b,2)-(4*a*c)))/(2*a);
    *x2=(-b-sqrt(pow(b,2)-(4*a*c)))/(2*a);
}

int main(void){
    double a, b, c;
    scanf("%lf", &a);
    scanf("%lf", &b);
    scanf("%lf", &c);
    double x1, x2;
    int count= solve(a, b, c, &x1, &x2);
    if(!count){
        printf("no solution");}
    else if(count==1){
        printf("one solution x: %lf", x1);}
    else if(count>1){
        printf("two solutions x1: %lf x2: %lf", x1, x2);}
}

the program should return both values from the function solve but everytime I start the program I have a warning that says "missing return value". Where is my fault? By the way struct is not an option cause of restrictions from my professor and it need to be all done in one function.

3
  • 3
    You declared your function as returning an int but you're not returning anything (there's no return statement). Commented Dec 19, 2017 at 0:41
  • 2
    1. Your professor’s restrictions are a terrible idea in a real environment, but might theoretically serve some educational purpose. 2. You declare solve as returning an int using the regular return value (in addition to whatever it does with the output parameters), but you never actually do that. You can do that with main because it’s special-cased to mean 0, but for any other function that’s undefined behavior. Commented Dec 19, 2017 at 0:42
  • How does the function communicate to the caller that there is only one solution if b² = 4*a*c? How does it communicate that there is no solution if b² < 4*a*c? The main function is expecting the regular return value to be the number of solutions, but the solve function doesn’t calculate that information, and just leaves it as NaN for no solution or equal values for one solution. Commented Dec 19, 2017 at 0:50

3 Answers 3

5

So, there's some misunderstanding here. From what I can understand from the template code, your solve function is supposed to be returning the number of (real) solutions for a quadratic equation, and there's currently no logic for that.

You should implement logic that returns the number of solutions based on the input. For a quadratic equation, you look at the discriminant. This is b^2 - 4*a*c. If it is less than 0, there are no solutions, one solution if it's equal to 0, and two solutions otherwise.

Additionally, since there is no (real) square root of a negative number, you should not even run the calculations if the discriminant is less than 0. If you run your function with a*c > b^2, x1 and x2 would be Not a Number.

You are performing the "return" of values using pointers correctly. However, since your function is defined as int solve, it is expecting you use return <some value>; inside the function. In fact, theoretically int main should be breaking it as well because you don't return anything (usually just return 0), but that's a special exception in the language to drop it for main.

Sign up to request clarification or add additional context in comments.

2 Comments

On almost all platforms (those that support NaN), there’s no reason other than performance to avoid performing the calculation if there’s no solution. In fact, it might be good to do it either way so the outputs are NaN instead of unchanged or unspecified.
@DanielH Makes sense, although I'd argue handling NaN would probably confuse someone new to programming.
0
#include <stdio.h>
#include <math.h>

int solve(double a, double b, double c, double *x1, double *x2){
    if((pow(b,2)-(4*a*c)>=0)){
        *x1=(-b+sqrt(pow(b,2)-(4*a*c)))/(2*a);
        if(sqrt(pow(b,2)-(4*a*c)>0)){
            *x2=(-b-sqrt(pow(b,2)-(4*a*c)))/(2*a);
            return 2;
        }    
        return 1;   
    } 
    else{
        printf("it is imaginary term, ");
    }
    return 0;
}

int main(void){
    double a, b, c;
    scanf("%lf", &a);
    scanf("%lf", &b);
    scanf("%lf", &c);
    double x1, x2;
    int count= solve(a, b, c, &x1, &x2);
    if(!count){
        printf("no real solution");}
    else if(count==1){
        printf("one solution x: %lf", x1);}
    else if(count>1){
        printf("two solutions x1: %lf x2: %lf", x1, x2);}
}

You have to design if condition in solve function so it will calculate the x2 value. And if you want to reduce size you can also use char data type for count variable.

Comments

-1

You should return an int value in solve function, as your functions signature is: int solve(double a, double b, double c, double *x1, double *x2),so just add "return 0;" at the end of solve function.

2 Comments

If you're returning a value that does nothing, you should just be using void solve.
Or at least return 1 if nothing went wrong, 0 for an error (for example)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.