1. Instead of using the variables: num1 and num2 create an array variable named
ID: 657954 • Letter: 1
Question
1. Instead of using the variables: num1 and num2 create an array variable namedNumberInput of size 2 to save input in from the keyboard... (10 points)
// includes all necessary headers.
// <iostream> for cin and cout. <iomanip> for setprecision(), fixed, showpoint etc.
// <cmath> for pow function.
#include<iostream>
#include<string>
#include<cmath>
#include <iomanip>
using namespace std;
// main function
int main ()
{
// declaring variables
double num1, num2,answer ;
char symbol;
// Prompts user for choice of arimetic expression to use.
cout << "Please input calculation operation eg. (1 + 2): ";
cin >> num1 >> symbol >> num2 ;
// result if addition is chosen
if(symbol=='+'){
answer = num1 + num2;
}
// result if subtraction is chosen
else if(symbol == '-'){
answer= num1 - num2;
}
// result if multiplication is chosen
else if(symbol == '*'){
answer = num1 * num2;
}
// result if division is chosen
else if (symbol == '/'){
answer = num1 / num2;
}
// result if modulus is chosen
else if (symbol == '%'){
answer = static_cast<double>(static_cast<int>(num1) % static_cast<int>(num2));
}
// result if exponent is chosen
else if(symbol == '^'){
answer = pow ( num1, num2);
}
// output from the arimetic expression
cout << fixed << showpoint << setprecision(2);
cout << "The answer is:" << endl;
cout << num1 << " " << symbol << " " << num2 << " = " << answer << endl;
system("pause");
return 0;
}
Explanation / Answer
// <cmath> for pow function.
#include<iostream>
#include<string>
#include<cmath>
#include <iomanip>
using namespace std;
// main function
int main ()
{
// declaring variables
double NumberInput[2],answer ;
char symbol;
// Prompts user for choice of arimetic expression to use.
cout << "Please input calculation operation eg. (1 + 2): ";
cin >> NumberInput[0] >> symbol >> NumberInput[1] ;
// result if addition is chosen
if(symbol=='+'){
answer = NumberInput[0] + NumberInput[1];
}
// result if subtraction is chosen
else if(symbol == '-'){
answer= NumberInput[0] - NumberInput[1];
}
// result if multiplication is chosen
else if(symbol == '*'){
answer = NumberInput[0] * NumberInput[1];
}
// result if division is chosen
else if (symbol == '/'){
answer = NumberInput[0] / NumberInput[1];
}
// result if modulus is chosen
else if (symbol == '%'){
answer = static_cast<double>(static_cast<int>(NumberInput[0]) % static_cast<int>(NumberInput[1]));
}
// result if exponent is chosen
else if(symbol == '^'){
answer = pow ( NumberInput[0], NumberInput[1]);
}
// output from the arimetic expression
cout << fixed << showpoint << setprecision(2);
cout << "The answer is:" << endl;
cout << NumberInput[0] << " " << symbol << " " << NumberInput[1] << " = " << answer << endl;
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.