Write a program to solve quadratic equations for real roots. No need to use imaginary numbers.  ax^2 +bx +c= 0 Requirements: -Define an array of type double with size 3 in main() to store the 3 coefficients. -Read the 3 coefficients from user input. -Create the function prototype and definition for the function    getRootCount(discriminant, coefficients)     Parameters:           discriminant: output to be calculated by the function           coefficients: input array of 3 coefficients     Return: number of roots: 0, 1, or 2

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Write a program to solve quadratic equations for real roots. No need to use imaginary numbers.

 ax^2 +bx +c= 0

Requirements:
-Define an array of type double with size 3 in main() to store the 3 coefficients.
-Read the 3 coefficients from user input.
-Create the function prototype and definition for the function 
  getRootCount(discriminant, coefficients)
    Parameters:
          discriminant: output to be calculated by the function
          coefficients: input array of 3 coefficients
    Return: number of roots: 0, 1, or 2

-Create the function prototype and definition for a function to determine the numbers of roots:
  getRootCount(discriminant, coefficients);
    Parameters:
          discriminant: output value to be calculated by the function (reference parameter)
          coefficients: input array of 3 coefficients
    Return: number of roots: 0, 1, or 2 (int)

-Create the function prototype and definition for a function to solve equations and get the root(s):

  solveEquation(discriminant, coefficients,  roots);
   Parameters:
          discriminant: input value to the function. 
          The discriminant is calculated by getRootCount(), so main() gets it and passes to this function.
          coefficients: input array of 3 coefficients
          roots: output array of roots. 
             Size of roots array is either 1 or 2, as determined by main() after calling getRootCount()
   Return: none (void)

-If the equation is valid, the main() function calls getRootCount() to determine the number of roots and also to get the discriminant calculated by this function.

-If there is at least one root, then main() defines an array of roots with the exact size (based on count of roots), then it calls solveEquation().

-Finally main() displays the one or two roots in the roots array, or displays a message if there is no root.

-You must use the following given main() code to test your 2 functions first before adding a loop to main():

int main()
{
    int const SIZE = 3;
    double coefficients[SIZE];
    double discriminant;
    cout << "This program solves quadratic formulas\n";
    cout << "Enter 3 coefficients separate by spaces: ";
    cin >> coefficients[0] >> coefficients[1]  >> coefficients[2] ;

    if (coefficients[0]  == 0)
        cout << "First coefficient cannot be 0.";
    else
    {
        int rootCount = getRootCount(discriminant, coefficients);
        if (rootCount == 0)
            cout << "There is no root" << endl;
        else
        {
            double roots[rootCount];
            solveEquation(discriminant, coefficients,  roots);
            if (rootCount == 1)
                cout << "There is one root: " << roots[0] << endl;
            else
                cout << "There are 2 roots: " << roots[0] << "   " << roots[1] << endl;
        }
    }
}

 

Sample outputs with given main code and different coefficient inputs:

This program solves quadratic formulas
Enter 3 coefficients separate by spaces: 1 -8 5
There are 2 roots: 7.31662   0.683375

This program solves quadratic formulas
Enter 3 coefficients separate by spaces: 1 2 1
There is one root: -1

This program solves quadratic formulas
Enter 3 coefficients separate by spaces: 5 20 32
There is no root

This program solves quadratic formulas
Enter 3 coefficients separate by spaces: 0 4 5
First coefficient cannot be 0.

If your 2 functions do not work with the given main code, do not bother to add more code to main() in the next step.

-After checking that your functions works with the given main() code, then add the most efficient loop in main() to let the program accept multiple equations and stop only when all 3 coefficients are 0.

This program solves quadratic formulas
Enter 3 coefficients separate by spaces: 123
There is no root
Enter 3 coefficients separate by spaces: -2 4 5
There are 2 roots: -0.870829
Enter 3 coefficients separate by spaces: 175
There are 2 roots: -0.807418
Enter 3 coefficients separate by spaces: 121
There is one rool: -1
Enter 3 coefficients separate by spaces: 1 -6 9
There is one root: 3
Enter 3 coefficients separate by spaces: 000
2.87083
-6.19258
Process returned e (ex8)
Press any key to continue.
execution time : 315.272 s
Transcribed Image Text:This program solves quadratic formulas Enter 3 coefficients separate by spaces: 123 There is no root Enter 3 coefficients separate by spaces: -2 4 5 There are 2 roots: -0.870829 Enter 3 coefficients separate by spaces: 175 There are 2 roots: -0.807418 Enter 3 coefficients separate by spaces: 121 There is one rool: -1 Enter 3 coefficients separate by spaces: 1 -6 9 There is one root: 3 Enter 3 coefficients separate by spaces: 000 2.87083 -6.19258 Process returned e (ex8) Press any key to continue. execution time : 315.272 s
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY