272,962 views
39 votes
39 votes
Objective

Make a function that solves the quadratic equation, outputting both values of X. You do not have to worry about imaginary numbers. That is, I will only use input that creates a real output (such as the example screenshots).
NOTE: A function that uses the C version of pass by reference is required to get full credit on this assignment. The C++ form of pass by reference will receive no credit.
Details
STEP 1
#include at the top of your file so that you can use the sqrt function. The sqrt function calculates the square root. For example:
int x = sqrt(9.0)
Will leave x with the value 3, which is the square root of 9.
STEP 2
Define a function THAT USES PASS BY REFERENCE to take in three double inputs and provides two double outputs. In the equations below I use a, b, and c as the double input names and x1 and x2 as the output names.
Inside this function solve the quadratic equation for both possible values of x:
X 1 = − b + b 2 − 4 a c 2 a
That is, X1 is equal to -b + sqrt(b * b - 4 * a * c) then divide all of that by 2 * a.
X 2 = − b − b 2 − 4 a c 2 a
That is, X2 is equal to -b - sqrt(b * b - 4 * a * c) then divide all of that by 2 * a.
Note the difference. One adds sqrt(b * b - 4 * a * c) and the other subtracts it.
This can be done in several lines of code if you want or you can attempt to perform the entire calculation in one line of code.
STEP 3
In the main function read in 3 inputs from the user. Pass these inputs to the quadratic equation function along with the pass by reference variables for the output. Print the results to the screen.
Advice
Your quadratic equation function does NOT need to account for imaginary numbers. Stick with the example inputs to test your code.
If you get NAN that means your equation has an imaginary solution. Do not worry about it. Try different inputs.
The calculation itself is a minor portion of this grade. The majority of the grade comes from implementing pass by reference so make sure you have that part correct.
b2 is simple enough that you do not need to call the pow() function to calculate the power. Instead multiply b * b.
You may want to calculate the numerator into one variable and the denominator into another. You don't have to solve the entire equation on one line (though that is possible).
Since you are using pass by reference you can make the QuadraticEquation function into a void function. It does not need to return anything since it is returning calculations via pointers.
Use scanf on one variable at a time. Don't forget to use double data types and %lf with scanf.

User Emilien Brigand
by
2.4k points

1 Answer

22 votes
22 votes

Answer:

In C

#include <stdio.h>

#include<math.h>

void myfunc(double *a, double *b, double *c) {

double x1, x2;

x1 = (-(*b) + sqrt((*b)*(*b)- 4*(*a)*(*c)))/(2*(*a));

x2 = (-(*b) - sqrt((*b)*(*b) - 4*(*a)*(*c)))/(2*(*a));

printf("x1 = %f\\",x1);

printf("x2 = %f\\",x2);}

int main () {

double a,b,c;

printf("a: "); scanf("%lf", &a);

printf("b: "); scanf("%lf", &b);

printf("c: "); scanf("%lf", &c);

myfunc(&a, &b, &c);

return 0;}

Step-by-step explanation:

#include <stdio.h>

#include<math.h> --- This represents step 1

Step 2 begins here

This gets the values of a, b and c from main by reference

void myfunc(double *a, double *b, double *c) {

This declares x1 and x2

double x1, x2;

Calculate x1

x1 = (-(*b) + sqrt((*b)*(*b)- 4*(*a)*(*c)))/(2*(*a));

Calculate x2

x2 = (-(*b) - sqrt((*b)*(*b) - 4*(*a)*(*c)))/(2*(*a));

Print x1

printf("x1 = %f\\",x1);

Print x2

printf("x2 = %f\\",x2);}

Step 3 begins here

int main () {

Declare a, b and c as double

double a,b,c;

Get input for a, b and c

printf("a: "); scanf("%lf", &a);

printf("b: "); scanf("%lf", &b);

printf("c: "); scanf("%lf", &c);

Call the function to calculate and print x1 and x2

myfunc(&a, &b, &c);

return 0;}

User Blindmeis
by
3.0k points