The following program is written in a very poor style that usesglobal variables
ID: 3610321 • Letter: T
Question
The following program is written in a very poor style that usesglobal variables instead of parameters and arguments, resulting inunwanted side effects.Rewrite it using good style.
#include <iostream> using namespace std; int Power ( ) ; int pow; int x; int result;
int main ( ) { cout << "Enter power: "; cin >> pow; cout << "Enter value to be raised to power: "; cin >> x; cout << Power(x, pow); } int Power ( ) { result =1;
while (pow > 0)
{ result = result * x; pow--; } return result; }
Rewrite it using good style.
#include <iostream> using namespace std; int Power ( ) ; int pow; int x; int result;
int main ( ) { cout << "Enter power: "; cin >> pow; cout << "Enter value to be raised to power: "; cin >> x; cout << Power(x, pow); } int Power ( ) { result =1;
while (pow > 0)
{ result = result * x; pow--; } return result; }
Explanation / Answer
#include using namespace std; int Power (int x, int pow ); int main ( ) { int pow=0; int x; cout > pow; cout > x; coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.