#include // Write a program that first asks the user for an integer base 'm' // and an integer exponent 'n'. If 'n' is non-negative, calculate // n // m // // If n < 0, explain that the result cannot, in general, be // represented as an integer. // // Next, ask the user for a real base 'x'and an integer // exponent 'n'. Calculate // n // x // allowing the exponent to be negative. // Function declarations int main(); // Function definitions int main() { return 0; } // Hint: // // n // To calculate x when n < 0, you could // divide 1.0 by x a total of '-n' times, // but this would require two separate // for loops. // // Instead, you can use the rule // // n / 1 \ -n // x = | --- | // \ x / // // Thus, if n < 0, first replace 'x' // with its reciprocal and 'n' with // '-n'. You then need only one for loop. // Here is a solution: /* int main() { int m{}; std::cout << "Enter an integer base 'm': "; std::cin >> m; int n{}; std::cout << "Enter an integer exponent 'n': "; std::cin >> n; if ( n >= 0 ) { int result{ 1 }; for ( int k{ 1 }; k <= n; ++k ) { result *= m; } std::cout << m << "^" << n << " = " << result << std::endl; } else { std::cout << "A negative exponent cannot, in general," << std::endl; std::cout << "be represented by an integer result." << std::endl; } std::cout << std::endl; double x{}; std::cout << "Enter a real base 'x': "; std::cin >> x; std::cout << "Enter an integer exponent 'n': "; std::cin >> n; // We want to print the original 'x' and // the original 'n' entered by the user. std::cout << x << "^" << n << " = "; if ( x == 0.0 && n < 0 ) { std::cout << "undefined" << std::endl; return 0; } if ( n < 0 ) { x = 1.0/x; n = -n; } // Now, 'n' is guaranteed to be non-negative. // We cannot use 'result' because it was // declared above to be of type 'int'. double real_result{ 1.0 }; for ( int k{ 1 }; k <= n; ++k ) { real_result *= x; } std::cout << real_result << std::endl; return 0; } */