Instructions: Complete the following problems in a programming environment (visu
ID: 3671678 • Letter: I
Question
Instructions: Complete the following problems in a programming environment (visual studios). Upload your.cpp files to d2l. Add comments to your files that include your name and dawn tag. Due:02/25/16(Thursday) Write a program to calculate the result of a number raised to a power. The user will input the base and exponent Prompt: Enter the value for base: 2 Enter the value for exponent: 3 Output: 2 raised to 3 is: 8 Write a program which asks user to input two numbers in order to perform arithmetic operation. Prompt the user to select which arithmetic operation they wish to perform (+, *). Using the switch statement perform the specified operation and print the result. Prompt: Enter two numbers: 5 3 Enter the arithmetic operation you wish to perform: * Output: Multiplication of 5 and 3 is: 15Explanation / Answer
1.
main.cpp
#include <iostream>
#include <cmath>
using namespace std;
// Function prototypes
void computePower(double base, double exponent);
int main(int argc, char* argv[])
{
double base, exponent;
bool isValid = false;
// Keep asking the user for a base until a valid input is given
do
{
cout << "Please enter a number for the base: ";
cin >> base;
// If the input is invalid, let the use know.
if(!cin)
{
cout << "Invalid input. Please enter a number. ";
cin.clear();
cin.ignore();
}
else
isValid = true;
} while (!isValid);
// Reassign the value of isValid
isValid = false;
// Keep asking the user for an exponent until a valid input is given
do
{
cout << "Please enter a number for the exponent: ";
cin >> exponent;
// If the input is invalid, let the use know.
if(!cin)
{
cout << "Invalid input. Please enter a number. ";
cin.clear();
cin.ignore();
}
else
isValid = true;
} while (!isValid);
computePower(base, exponent);
// Successfully end the program
return 0;
} // End of main()
// Function definitions
/* Purpose: This function will raise a base by a power and print the result.
* Parameters:
* int base: The base that will be raised.
* int exponent: The power that the base will be raised by.
* Return: This function does not return anything.
*/
void computePower(double base, double exponent)
{
// Calculate the power
double power = pow(base, exponent);
// Print the result
cout << base << " to the power of " << exponent << " equals " << power << ". ";
return;
} // End of computePower function
2.
InArith.cpp
#include <iostream>
#include <cmath>
using namespace std;
// Function prototypes
void computePower(double base, double exponent);
int main(int argc, char* argv[])
{
double base, exponent;
bool isValid = false;
// Keep asking the user for a base until a valid input is given
do
{
cout << "Please enter a number for the base: ";
cin >> base;
// If the input is invalid, let the use know.
if(!cin)
{
cout << "Invalid input. Please enter a number. ";
cin.clear();
cin.ignore();
}
else
isValid = true;
} while (!isValid);
// Reassign the value of isValid
isValid = false;
// Keep asking the user for an exponent until a valid input is given
do
{
cout << "Please enter a number for the exponent: ";
cin >> exponent;
// If the input is invalid, let the use know.
if(!cin)
{
cout << "Invalid input. Please enter a number. ";
cin.clear();
cin.ignore();
}
else
isValid = true;
} while (!isValid);
computePower(base, exponent);
// Successfully end the program
return 0;
} // End of main()
// Function definitions
/* Purpose: This function will raise a base by a power and print the result.
* Parameters:
* int base: The base that will be raised.
* int exponent: The power that the base will be raised by.
* Return: This function does not return anything.
*/
void computePower(double base, double exponent)
{
// Calculate the power
double power = pow(base, exponent);
// Print the result
cout << base << " to the power of " << exponent << " equals " << power << ". ";
return;
} // End of computePower function