Shiranai
Shiranai

Reputation: 23

Return multiple values in one function in C with pointers

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.

Upvotes: 1

Views: 1881

Answers (3)

Sujay
Sujay

Reputation: 606

#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.

Upvotes: 0

Alyssa Haroldsen
Alyssa Haroldsen

Reputation: 3731

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.

Upvotes: 5

Devin Zhu
Devin Zhu

Reputation: 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.

Upvotes: -1

Related Questions