Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ General description This assignment is all about designing functions to be as broadly useful as possible: providing them with the information they need to

C++

General description

This assignment is all about designing functions to be as broadly useful as possible: providing them with the information they need to do their job (via parameters, either by value or reference); and having them hand back their results (either via the reference parameters or the return statement).

Your task

Write a program that allows the user to select a mathematical operation to solve. Each operation will be implemented as a function; a single "helper" function will gather the operands needed for the selected operation. Some of these operations can "break" under certain conditions (e.g. divide by 0), so the functions must be able to flag any errors by returning an error code or OK if the operation was successful.

Use incremental development

Develop this program one function at a time.

Implement everything in main prior to the invocation of acquireOperands.

Next turn all function prototypes into function stubs. After this, you should be able to compile your program without any errors.

Now you can begin implementing the functions, start with the "helper" function for operand input.

For each function, use your main function to test it locally or on Cloud 9 with various values.

After you believe your function works, test it on your own and then submit your file to have zyBooks further test the function.

Once you have received full credit on the unit tests for that function, you can begin to layout and implement the next function.

Once all the functions are completed and tested, complete the main function to properly drive the program.

The functions

acquireOperands: acquires the values for the specified operation and places them into reference parameters

1 const string reference parameter (requested operation)

3 integer reference parameters (for up to 3 operands)

returns number of user inputs acquired by this function

e.g. the "division" operation has 2 inputs and thus the function would return the value 2.

In many large programs, lots of developers work on various things. We return the quantity of inputs here because this allows others to check our work and know exactly which operand variables have valid values.

division: calculates floating point result of a / b

2 integer value parameters (for operands)

1 double reference parameter (for result)

returns the constant for "Divide by 0" or the constant for "OK"

Pythagorean equation: solve for hypotenuse c such that c = sqrt(a^2 + b^2)

2 integer value parameters (for operands)

1 double reference parameter (for result)

returns the constant for "OK"

quadratic equation: solves the equation ax^2 + bx + c = 0 for x image text in transcribed

3 integer value parameters (for operands)

2 double reference parameters (for two roots)

returns the constant for "OK", or "Divide by 0", or "Negative Discriminant"

Do not simplify the quadratic equation in any way based on input values such as a = 0.

Notes

the operation is acquired in main and then provided to acquireOperands

acquireOperands must request exactly the number of inputs required for the provided operation

the operation functions provide multiple "return" values by using the return statement and reference parameters

results are communicated via the reference parameters

the return statement is used for the error code/constant

no input or output statements in any of the operation functions - all i/o is in either main or acquireOperands

main function

Once you have your functions written and tested, you can write main:

Repeatedly acquire the user operation choice if it is anything other than "division" or "pythagorean" or "quadratic", output a message "Operation not supported", and continue to prompt until a proper choice is made.

acquireOperands is invoked for you, when it completes the proper operands should be filled with values

now you can invoke the appropriate operation function, and display the output similar to the examples:

Equation:

either a Result: or an Error:Output strings for potential errors

Operation not supported.

Cannot divide by zero.

Cannot take square root of a negative number.

Quadratic roots should be output in ascending order when outputting the results in main.

Double roots (two roots of the same value) should only be output once when outputting results in main.

Starter code

Use the starter code provided below by copy and pasting it into a development environment, such as Cloud 9. After pasting it, go through the code and read all the comments to help familiarize yourself with the starter code. Do not delete the comments, they are there to help guide you and provide insight as to what purpose certain code blocks have. When implementing a code block, it will most often go immediately below the comment, this allows a second viewer to read your comment and then your code, allowing the newcomer to read and interpret the code with the background knowledge or overview given in the comment.

#include

#include

#include

using namespace std;

const int OK = 0;

const int DIV_ZERO = 1;

const int SQRT_ERR = 2;

/// @brief acquire the proper number of numeric inputs based on operation string

/// /// The numeric inputs are set into x, y, and z in that order;

/// the number of numeric inputs acquired is returned to the caller.

/// Not all operations require all three values:

/// do not set the variables of operands that are not needed

. /// /// @param op the operation to be performed such as division

/// @param x the first numeric input /// @param y the second numeric input

/// @param z the third numeric input

/// @return the number of numeric inputs that were acquired int acquireOperands(const string &op, int &x, int &y, int &z);

/// @brief calculate quotient for provided values

/// /// @param a the dividend of the equation

/// @param b the divisor of the equation

/// @param result reference to place the quotient in

/// @return returns the integer representing the state of the calculation

/// using constants for OK and DIV_ZERO int division(int a, int b, double &result);

/// @brief calculate c for the pythagorean theorem a^2 + b^2 = c^2

/// /// @param a the value of a in the equation

/// @param b the value of b in the equation

/// @param c reference for the c calculation

/// @return returns the integer representing the state of the calculation

/// using constant for OK int pythagorean(int a, int b, double &c);

/// @brief calculate the roots to the quadratic formula for a polynomial of

/// the form a*x^2 + b*x + c = 0. Returns errant state when necessary.

/// Do not simplify equation in any way based on inputs such as a = 0.

/// /// @param a the coefficient of x^2 in the polynomial equation

/// @param b the coefficient of x in the polynomial equation

/// @param c the last value of the polynomial equation

/// @param root1 reference for the first root of the quadratic formula

/// @param root2 reference for the second root of the quadratic formula

/// @return returns the integer representing the state of the calculation

/// using constants for OK, DIV_ZERO, and SQRT_ERR int quadratic(int a, int b, int c, double &root1, double &root2);

/// @brief main driver for the mathematics program int main() { string operation; int number1; int number2; int number3; int numOperands; int result;

// Acquire the operation the user wishes to perform cout > operation; cout

// TODO: keep acquiring an operation from the user as long as the

// user has typed an invalid operation.

// Output the invalid operation error with every iteration.

// Acquire user numerical inputs

// All remaining cin statements will exist in acquireOperands numOperands = acquireOperands(operation, number1, number2, number3);

// TODO: test your functions as you develop them. Once

// you are sure the function is working correctly, move to the

// next function. Once you have completed all the functions

// comment out or eliminate your "testing" code and

// continue writing main

// TODO: Perform the operation specified by the user

// For the specified operation, output the equation followed

// by either the error or the result (but never both).

return 0;

}

b2 b-t 2a a, C

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions