Returns a value as an output parameter - c

I have a question in C where I need to insert coefficients of a quadratic equation into a function and return the number of solutions and result.
Write a program that accepts a series of 3 real numbers, which are the
coefficients of a quadratic equation, and the program will print out
some solutions to the equation and the solutions themselves.
Guidelines:
Functions must be worked with one of the functions that
returns the number of solutions as a returned value, and returns the
solutions themselves through output parameters.
3 numbers must be
received each time. The input will be from a file (will end in EOF)
In the meantime I built the function without reading from a file just to see that it works for me, I built the function that returns the number of solutions but I got entangled in how to return the result as output parameter
here is my code for now:
int main ()
{
double a, b, c, root1,root2,rootnum;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf",&a, &b, &c);
rootnum=(rootnumber(a,b,c);
printf("the number of roots for this equation is %d ",rootnum);
}
int rootnumber (double a,double b, double c)
{
formula=b*b - 4*a*c;
if (formula<0)
return 0;
if (formula==0)
return 1;
else
return 2;
}

In C, providing an "output parameter" usually amounts to providing an argument that is a pointer. The function dereferences that pointer and writes the result. For example;
int some_func(double x, double *y)
{
*y = 2*x;
return 1;
}
The caller must generally provide an address (e.g. of a variable) that will receive the result. For example;
int main()
{
double result;
if (some_func(2.0, &result) == 1)
printf("%lf\n", result);
else
printf("Uh oh!\n");
return 0;
}
I've deliberately provided an example that illustrates what an "output parameter" is, but has not relationship to the code you actually need to write. For your problem, you will need to provide two (i.e. a total of five arguments, three that you are providing already, and another two pointers that are used to return values to the caller).
Since this is a homework exercise, I won't explain WHAT values your function needs to return via output parameters. After all, that is part of the exercise, and the purpose is for you to learn by working that out.

Apart from a wayward parenthesis in the call and some other syntax errors, what you have so far looks fine. To print out the number of roots, you need to put a format specifier and an argument in your printf statement:
printf("the number of roots for this equation is %d\n", rootNum);
The %d is the format specifier for an int.

Here is your working code:
#include <stdio.h>
int rootnumber (double a,double b, double c)
{
double formula = (b*b) - (4*(a)*(c));
if (formula > 0) {
return 2;
}
else if (formula < 0) {
return 0;
}
else {
return 1;
}
}
int main (void)
{
double a, b, c;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf",&a, &b, &c);
printf("The number of roots for this equation is %d ", rootnumber(a,b,c));
return 0;
}

It just need some sanity checking, its working now:
#include<stdio.h>
int rootnumber(double a, double b, double c);
int main ()
{
double a, b, c, root1,root2;
int rootnum;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf",&a, &b, &c);
rootnum=rootnumber(a,b,c);
printf("the number of roots for this equation is %d", rootnum);
return 0;
}
int rootnumber(double a, double b, double c)
{
int formula= (b*b) - (4*a*c);
if (formula<0)
return 0;
if (formula==0)
return 1;
else
return 2;
}

Related

Can anyone explain why this code prints garbage values?

Can anyone explain why this code prints garbage values?
int main(){
int a, b, sum;
sum = a+b;
scanf("%d%d",&a,&b);
printf("addition is %d", sum);
return 0;
}
But if I write code like this it prints the correct value?
int main(){
int a, b, sum;
scanf("%d%d",&a,&b);
sum = a+b;
printf("addition is %d", sum);
return 0;
}
That's because C is an imperative language and each line is like a command, executed from top to bottom. So in this case:
int main(){
int a, b, sum; // create 3 variables: a, b and sum. Garbage on start
sum = a+b; // add 'a' and 'b' and save it in 'sum'. Still garbage
scanf("%d%d",&a,&b); // read 2 numbers and save them in 'a' and 'b'. Nowe they're valid, but 'sum' is still garbage
printf("addition is %d", sum); // print 'sum'. That's garbage
return 0;
}
By simply reordering the operations (adding a and b after reading their values) we get valid result
sum = a+b;
is not a definition or a formula, it's a statement - it's executed as soon as it is encountered, with whatever values are in a and b (which are indeterminate).
C just isn't that high-level.

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

Scan multiple variables depending on test cases?

I want to calculate sum of arithmetic progression in which we have to take 3 variables from user. a=first number, b= step size/increment, c=length of sequence.
If there are more than 1 test case , say three, then I have to scan a,b,c three time. How to do this?
E.g scanf (" %d %d %d", a,b,c); 3 times without affect initial values in first test case.
If you know no of test cases read it first and store it in a variable.
int calculate_ap(int a, int b, int c)
{
//Implement function to calculate Arithmetic progression and return the result
}
int main()
{
int test_cases = 0;
int a, b, c;
scanf("%d", &test_cases); //Reads no of test cases
while(test_cases--)
{
scanf("%d, %d, %d", &a, &b, &c); //read A, B, C
printf("%d\n", calculate_ap(a, b, c));
}
}
Hope this helps.

Squared Equation calculator not working in C

Hello guys I started learning C few weeks ago and I am trying to make my first useful program, I am trying to create a squared equation calculator but no matter what I type in the input it always outputs "no solution2" can anyone take a look and help me ?
examples for input :
0 0 0=
1 4 1=
#include <stdio.h>
#include <math.h>
int main()
{
printf("enter a\n");
double a; scanf("%1f", &a);
printf("\nenter b\n");
double b; scanf("%1f", &b);
printf("\nenter c\n");
double c; scanf("%1f", &c);
if (a==0)
{
if (b==0)
{
if (c==0)
printf("x can be every number\n");
else
printf("no solution1\n");
}
else
{
printf("x equals %.2f\n", ((-1)*c) / b);
}
}
else
{
double delta = (b*b)-(4*(a*c));
if (delta>0)
{
double sqrtDlt = sqrt(delta);
printf("x1 = %4.2f\n", (((-1)*b) + sqrtDlt) / 2 * a);
printf("x2 = %4.2f\n", (((-1)*b) - sqrtDlt) / 2 * a);
}
else
printf("no solution2\n");
}
return 0;
}
For double use specifier %l(ell)f not %1(one)f in scanf.
Note that this is one place that printf format strings differ substantially from scanf (and fscanf, etc.) format strings. For output, you're passing a value, which will be promoted from float to double when passed as a variadic parameter. For input you're passing a pointer, which is not promoted, so you have to tell scanf whether you want to read a float or a double, so for scanf, %f means you want to read a float and %lf means you want to read a double.

How do you return a value from a called function to the main?

I use the return command then try to print the value from the main. It returns a value of zero (0).
This program is about temperature conversion from Celsius to Fahrenheit.
Also how can you use a rounding function to round the answer to an integer so it is not a floating point number with decimals.
#include <stdio.h>
int Cel_To_Fah(int a, int b); // function declaration
int main (void)
{
int a;
int b;
printf(" Enter temperatrure: "); scanf("%d", &a);
Cel_To_Fah(a,b); // function call
printf("The temperature is: %d\n", b);
return 0;
} // main
int Cel_To_Fah(a,b)
{
b=1.8*a+32;
return b;
} // Cel_To_Fah
You just have to use the assignment operator:
b = Cel_To_Fah(a);
Your program has a lot of problems, though, including your Cel_To_Fah function not having a correct signature. You probably want something like:
int Cel_To_Fah(int a)
{
return 1.8 * a + 32;
}
You should probably get a good beginner C book.
No need of second argument to function(b).
You can do this by...
#include<stdio.h>
int Cel_To_Fah(int a); // function declaration, as it returns a values;
int main (void)
{
int a; int b;
printf(" Enter temperatrure: ");
scanf("%d", &a);
b = Cel_To_Fah(a); /* the returned value is stored into b, and as b is an integer so it is automatically rounded. no fraction point value can be stored into an integer*/
printf("The temperature is: %d\n", b);
return 0;
} // main
int Cel_To_Fah(int a)
{
return 1.8 * a + 32;
}
there are several issues. First you need to use float, not int, so that you can have values with a decimal point. otherwise your calculations will come out wrong. Also use 32.0 instead of 32 for the same reason.
Second, you need to understand that the a and b in your function are NOT the same as the a and b in main. They have the same name but are not in the same "scope". So changing the one in your function doesn't affect the one in main. That's why in main you have to say b=Cel... so that b in main will get the returned value.
finally, in c, you're supposed to put your functions above/before main. Otherwise it's technically not defined "yet", though some modern compilers will fix that for you. Read about function prototypes.
Since your function Cel_To_Fah(a,b); is returning a value (int type), you must have to assign it to a variable of its return type (int type).
int a;
int b;
printf(" Enter temperatrure: "); scanf("%d", &a);
b = Cel_To_Fah(a); // function call
printf("The temperature is: %d\n", b);
and your function should be
int Cel_To_Fah(a)
{
int b = 1.8*a+32;
return b;
} // Cel_To_Fah
And do not forget to change your function prototype to
int Cel_To_Fah(int a);
I saw two issues in your code. Firstly, it is variable type. I assume that you want Celsius as integer; but Fahrenheit = 1.8*Celsius+32 should be float. Therefore b should be float.
Secondly, you should not return a value from a function via its input parameters (unless you learn pointer or call by ref). I rewrite your code as following:
include<stdio.h>
float Cel_To_Fah(int a); // function declaration
int main (void)
{
int a;
float b;
printf(" Enter temperatrure: "); scanf("%d", &a);
b=Cel_To_Fah(a); // function call
printf("The temperature is: %.2f\n", b); //showing 2 decimal places
return 0;
} // main
float Cel_To_Fah(int a)
{
float b;
b=1.8*(float)a+32; //cast a from int to float
return b;
} // Cel_To_Fah

Resources