146k views
4 votes
Write the definition of a function named quadratic that receives three double parameters a, b, c. If the value of a is 0 then the function prints the message "no solution for a=0" and returns. If the value of "b squared" – 4ac is negative, then the code prints out the message "no real solutions" and returns. Otherwise the function prints out the largest solution to the quadratic equation. The formula for the solutions to this equation can be found here: Quadratic Equation on Wikipedia.

User Joulss
by
5.0k points

1 Answer

5 votes

Answer:

Following are the program in the C++ Programming Language.

#include <iostream> //header file

#include <cmath> //header file

using namespace std; //namespace

//define function

void quadratic (double a, double b, double c)

{

double x,y; //set double type variables

//check a==0

if (a==0)

{ //then print message

cout << "no solution for a=0";

}

//check if solution is less than 0

else if ((b*b - 4*a*c) < 0)

{ //print message

cout << "no real solutions";

}

//otherwise

else

{//solve the following quadratic equation

x = (-b + sqrt( b*b - 4*a*c)) /(2*a);//in term of x

y = (-b - sqrt(b*b - 4*a*c)) / (2*a);//in term of y

//check x is greater than y

if (x > y){

cout<<x; //print the value of x

}

//otherwise

else{

cout<<y;//print the value of y

}

}

}

//define main method

int main() {

//set double type variables and assign their value

double x=10, y=50, z=-205;

//call the function

quadratic(x,y,z);

}

Output:

2.67204

Step-by-step explanation:

Here, we define a function "quadratic" and pass three double data type variables i.e., "a", "b" and, "c" inside the function.

  • Set two double type variable i.e., "x", "y".
  • Set the if conditional statement and check the conditions is the variable a is equal to the 0 then, print the message or check another condition is the solution of the equation "b*b - 4*a*c" is less than 0, then print the message.
  • Otherwise, solve the quadratic equation in terms of the variable x or the variable y.
  • Again check the condition inside the else part is the variable x is greater than the variable y then, print the value of x otherwise print the value of y.

Finally, we define the main function inside it we set three double type variables i.e., "x", "y" and, "z" and initialize value 10, 50 and, -250 then, call the function and pass the variables x y z in its parameter.

User James Hiew
by
5.2k points