58.3k views
1 vote
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.

User Alex Lapa
by
4.1k points

1 Answer

0 votes

Answer:

#include <iostream>

#include <cmath>

using namespace std;

//initialize function quadratic

void quadratic(double, double, double);

int main() {

//declare double variables a, b and c

double a,b,c;

//take input from user

cin>>a>>b>>c;

//call function quadratic

quadratic(a,b,c);

return 0;

}

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

double root,n;

//check if variable a is equal to zero

if(a==0){

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

return;

}

//check if b squared - 4ac is less than zero

else

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

cout<<"no real solutions"<<endl;

return;

}

//print the largest root if the above conditions are not satisfied

else{

n=((b*b)-(4*a*c));

root=(-b + sqrt(n)) / (2*a);

cout<<"Largest root is:"<<root<<endl;

}

return ;

}

Step-by-step explanation:

Read three double variables a, b and c from the user and pass it to the function quadratic. Check if the value of variable a is equal to zero. If true, print "no solution for a=0". If this condition is false, check if b squared - 4ac is less than zero. If true, print "no real solutions". If this condition is also false, calculate the largest solution using the following formula:

largest root = (-b + square root of (b squared - 4ac)) / (2*a)

Input 1:

2 5 3

Output 2:

Largest root is:-1

Input 2:

5 6 1

Output 2:

Largest root is:-0.2

User DumTux
by
5.4k points