Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ program Error: General description This assignment is all about designing fu

ID: 3842929 • Letter: C

Question

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"

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.

This is my code:

#include <iostream>
#include <cstdlib> // Enables use of rand()
#include <ctime> // Enables use of time()
#include <string>
#include <iomanip>
#include <cmath>

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 << "Enter your first number: " ;
cin >> x;
cout << endl;
cout << "Enter your second number: ";
cin >> y;
cout << endl;
return 2;
  
}
  
/// function of Pythagorean equation
else if (op == "pythagorean")
{
cout << "Enter your first number: " ;
cin >> x;
cout << endl;
cout << "Enter your second number: ";;
cin >> y;
cout << endl;
return 2;
}
  
/// quaratic function
else if (op == "quadratic")
{
cout << "Enter your first number: " ;
cin >> x;
cout << endl;
cout << "Enter your second number: ";
cin >> y;
cout << endl;
cout << "Enter your third number: ";
cin >> z;
cout << endl;
return 3;
  
}
  
else
{
cout << "Error: Operation not supported.";
}
return -1 ;
}

// definition of division
int division(double a, double b, double &result)
{
  
  
if (static_cast <int> (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 <int>(Discriminant) < 0)
{
return SQRT_ERR;
}
if (static_cast <int>(Discriminant) >= 0 && static_cast <int>(a)!= 0)
{
root1 = (-b + sqrt(Discriminant)) / (2 * a);
root2 = (-b - sqrt(Discriminant)) / (2 * a);
  
return OK;
  
}
if (static_cast <int>(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 << "Please choose an operation: ";
cin >> oper;
cout << endl;
  
while (oper != "division" && oper!= "pythagorean" && oper != "quadratic")
{
cout << "Error: Operation not supported." << endl;
cout << "Please choose an operation: ";
cin >> oper;
cout << endl;
  
}
acquireOperands( oper, num1, num2, num3);
if (oper == "division")
{
cout << "Equation: " << num1 << " / " << num2 << endl;
returnValue = division(num1, num2, resultDivision );
  
/// check returnvalue in division fucntion
if (returnValue == 0)
{
cout << "Result: " << resultDivision << endl;
  
}
else if (returnValue == 1)
{
cout << "Error: Cannot divide by zero." << endl;
  
}
  
}
else if (oper == "pythagorean")
{
cout <<"Equation: " <<"sqrt(" << num1;
cout << "^2 + " << num2 << "^2)" << endl;
returnValue = pythagorean(num1, num2, resultPythagorean);
  
if (returnValue ==0)
{
cout <<"Result: " << resultPythagorean << endl;
  
  
}
  
}
else if (oper == "quadratic")
{
cout <<"Equation: "<< num1 <<"x^2 + " ;
cout << num2 << "x + " << num3 << " = 0" << endl;
returnValue = quadratic(num1, num2, num3, rootNum1, rootNum2);
if (returnValue == 0 )
{
if (static_cast<int>(rootNum1) < static_cast<int>(rootNum2))
{
cout << "Result: " << rootNum1 <<", "<< rootNum2 << endl;
  
}
else if (static_cast<int>(rootNum1) > static_cast<int>(rootNum2))
{
cout << "Result: " << rootNum2 <<", "<< rootNum1 << endl;
  
}

else
{
cout << "Result: " << rootNum1 << endl;

}

}
else if (returnValue == 2)
{
cout << "Error: Cannot take square root of a negative number.";

  
}
else if (returnValue == 1)
{
cout << "Error: Cannot divide by zero.";

  
}
  
}
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

Explanation / Answer

#include <iostream>
using namespace std;
template<class T>
class numbers
{
private:
T num1;
T num2;
public:
numbers() : num1(0), num2(0) {}
void setvalues(T n1, T n2) {num1 = n1; num2 = n2;}
T add() {return num1 + num2;}
T subtract() {return num1 - num2;}
T multiply() {return num1 * num2;}
T divide() {return (num2 != 0) ? num1 / num2 : 0;}
};
void integer(numbers<int>& numo)
{
int number1;
int number2;
cout << "First number: " << endl;
cin >> number1;
cout << "Second number: " << endl;
cin >> number2;
numo.setvalues(number1, number2);
}
void doubl(numbers<double>& numo)
{double number1;
double number2;
cout << "First number: " << endl;
cin >> number1;
cout << "Second number: " << endl;
cin >> number2;
numo.setvalues(number1, number2);
}
int main()
{
cout << "Type to choose:" << endl;
cout << "1. int" << endl;
cout << "2. double" << endl;
int choosed;
cin >> choosed;
numbers<int> num_int;
numbers<double> num_dbl;
int typeOfVal = 0; // 1 for integer, 2 for double

switch(choosed) {
case 1:
integer(num_int);
typeOfVal = 1;
break;
case 2:
doubl(num_dbl);
typeOfVal = 2;
break;
default:
cout << "Error" << endl;
return 1;
}
int typeOfOp = 0;
cout << "What operation would like to do on this numbers?" << endl;
cout << "1. +" << endl;
cout << "2. -" << endl;
cout << "3. *" << endl;
cout << "4. /" << endl;
cin >> typeOfOp;
int resint;
double resdbl;
switch(typeOfOp){
case 1:
if (typeOfVal == 1) resint = num_int.add(); else resdbl = num_dbl.add();
break;
case 2:
if (typeOfVal == 1) resint = num_int.subtract(); else resdbl = num_dbl.subtract();
break;
case 3:
if (typeOfVal == 1) resint = num_int.multiply(); else resdbl = num_dbl.multiply();
break;
case 4:
if (typeOfVal == 1) resint = num_int.divide(); else resdbl = num_dbl.divide();
default:
cout << "Error" << endl;
return 1;
}
cout << "The answer:" << endl;
if (typeOfVal == 1) cout << resint << endl;
else cout << resdbl << endl;
cin.get();
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote