I need to make a variation of the pow() function for a homework assignment.
These are the requirements:
"Using a for loop, write code that will compute the result of an int raised to the power of another int. For example, your for loop should use two variables, one for the base and one for the exponent. It should then calculate the base raised to the exponent. 2 raised to the power of 2 should output 4, 2 raised to 8 should output 256, etc. Ensure your code meets these requirements and then paste it in the response section below:"
"Contains a variable for the base
Contains a variable for the exponent
Uses a for loop to perform the power function
Outputs the result to the console window"
This is the code I have right now:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
// Osman Zakir
// 12 / 28 / 2016
// raise_to_power.cpp
// edx.org Introduction to C++ - Control Flow Peer Review
// Requirements:
/**
* Contains a variable for the base
* Contains a variable for the exponent
* Uses a for loop to perform the power function
* Outputs the result to the console window
*/
#include <iostream>
#include <stdexcept>
int raise_to_power(int number, int exp);
void print_result(const int result, const int number, const int exp);
int main()
{
int number = 0, exp = 0, result = 0;
using namespace std;
try
{
number = 2, exp = 2;
result = raise_to_power(number, exp);
print_result(result, number, exp);
}
catch (const runtime_error &e)
{
cerr << "Error: " << e.what() << endl;
if (number <= 0)
{
cout << "Provide a positive value for number to raise to power: ";
cin >> number;
cin.ignore();
result = raise_to_power(number, exp);
print_result(result, number, exp);
}
if (exp <= 0)
{
cout << "Provide a positive value for exponent: ";
cin >> exp;
cin.ignore();
result = raise_to_power(number, exp);
print_result(result, number, exp);
}
}
}
int raise_to_power(int number, int exp)
// pre-condition: both the number and the exponent should be positive
{
using namespace std;
if (number <= 0 || exp <= 0)
{
throw runtime_error("bad argument to raise_to_power()");
}
else if (number <= 0 && exp <= 0)
{
throw runtime_error("bad argument to raise_to_power()");
}
int result = 0;
for (int i = 1; i <= number; ++i)
{
result = i;
result *= exp * exp;
}
return result;
// post-condition: the result should be positive
}
void print_result(const int result, const int number, const int exp)
{
using namespace std;
cout << number << " raised to the power of " << exp << " is " << result << "\n";
}
|
I need helping fixing the bug and also in making it less complicated if possible. Thanks in advance.
Edit: Okay, never mind on the bug. I fixed it from looking other code on this site.
It works like this:
1 2 3 4 5 6
|
int result = 1;
for (int i = 0; i < exp; ++i)
{
result *= number;
}
return result;
|
Everything else is the same as before, though, so I'd like to know of a good way to make the code in main() more simple (in the catch block, I mean). It has to still handle the exception like it does now, but the code should just be less complicated.