Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

C++ program Error:

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  

This is my code:

#include #include // Enables use of rand() #include // Enables use of time() #include #include #include

using namespace std;

//decalre global variables; const int OK = 0; const int DIV_ZERO = 1; const int SQRT_ERR = 2;

// definition for helper function

int acquireOperands(const string &op, double &x, double &y, double &z); int division(double a, double b, double &result); int quadratic(double a, double b, double c, double &root1, double &root2); int pythagorean(double a, double b, double &c);

// definition of acquireOperands function int acquireOperands(const string &op, double &x, double &y, double &z) { /// division operand if (op == "division") { cout > x; cout > y; cout > x; cout > y; cout > x; cout > y; cout > z; cout

// definition of division int division(double a, double b, double &result) { if (static_cast (b) == 0 ) { return DIV_ZERO; } else { result = a / b; return OK; } return -1; }

// definition of pythgorean equation

int pythagorean(double a, double b, double &c) { c = sqrt (a * a + b * b); return OK; }

int quadratic(double a, double b, double c, double &root1, double &root2) { double Discriminant;

Discriminant = pow(b, 2) - 4 * a * c; if (static_cast (Discriminant) (Discriminant) >= 0 && static_cast (a)!= 0) { root1 = (-b + sqrt(Discriminant)) / (2 * a); root2 = (-b - sqrt(Discriminant)) / (2 * a); return OK; } if (static_cast (a)==0) {

return DIV_ZERO; } return -1; }

int main () { string oper = " "; double resultDivision = 0.0; double resultPythagorean = 0.0; double rootNum1, rootNum2 ; int returnValue; double num1, num2, num3; // for cout > oper; cout > oper; cout (rootNum1) (rootNum2)) { cout (rootNum1) > static_cast(rootNum2)) { cout

else { cout

}

} else if (returnValue == 2) { cout

} else if (returnValue == 1) { cout

} } return 0; }

Basically got most of them right except the first part: this is my error:

Tests the acquireOperands function with all potential operations

Compilation failed

program7.cpp: In function 'bool testPassed()': program7.cpp:220:61: error: invalid initialization of non-const reference of type 'double&' from an rvalue of type 'double' returnVal = acquireOperands(ops.at(i), one, two, three); ^

program7.cpp:27:5: note: initializing argument 2 of 'int acquireOperands(const string&, double&, double&, double&)' int acquireOperands(const string &op, double &x, double &y, double &z) ^~~~~~~~~~~~~~~

Please help! Thank you!!

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

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2010 Barcelona Spain September 2010 Proceedings Part 1 Lnai 6321

Authors: Jose L. Balcazar ,Francesco Bonchi ,Aristides Gionis ,Michele Sebag

2010th Edition

364215879X, 978-3642158797

More Books

Students also viewed these Databases questions

Question

What is Ohm's law and also tell about Snell's law?

Answered: 1 week ago

Question

4. Identify cultural variations in communication style.

Answered: 1 week ago

Question

9. Understand the phenomenon of code switching and interlanguage.

Answered: 1 week ago

Question

8. Explain the difference between translation and interpretation.

Answered: 1 week ago