Share
Explore BrainMass

# exponentiation

Declare x as float
Declare y as integer
Declare i as integer
Declare result as float
Input x // can be any float
Input y // has to be a positive integer
Set result = 1 // initial value
For i = 1 to y
result = result * x
End for
Output result
Convert this pseudocode into a C++ program. Note the restrictions on x and y: x can be any kind of float (including negatives), whereas y should only be an integer that is greater than or equal to 0 (0 is allowed).
Use the following template for your program and remember to maintain all the blank lines, spaces, and general alignment. Then replace the areas that have been highlighted in yellow with your code. Do not change any of the other code.
Code Template for Exercise 3
/******************************************************/
/* File: Programming Project */
/* */
/* Created by: Mireille Bikim */
/* Date: April 13, 2006 */
/* */
/* flow control statements and functions */
/* */
/* Inputs: (keyboard) */
/* 1. One integer exponent (y), y >= 0 */
/* 2. One float number (x) */
/* */
/* Output: */
/* Print x^y as a float, on the screen */
/* */
/* Algorithm: repeatedly multiply x by itself, y */
/* times */
/* */
/******************************************************/
#include <iostream>
using namespace std ;
int main()
{
Write code to declare all the variables.

// read in the two numbers
cout << endl ;
cout << "Enter number to be raised to power(float) : ";
cin >> y ;
cout << "Enter the exponent (positive integer) : " ;
cin >> x ;
Initialize any variables that need to be initialized.
for ( )
{
Write the code for the for-loop initializations
and the for-loop body. Carefully study and compare
the pseudocode and C++ for loops in the commentary.

}
// display the result

cout << endl ;
cout << "The value of" << x << " to power " << y
<< " is : " << result << endl ;

return (0); // terminate with success
}
Test Plan for Exercise 3
Test your code on the following. See if you get the expected answer.
2 3 8
2 0 1
2 1 2
2 8 256
8 2 64
3 3 27
5 5 3,125
Now test your code with the following. You may need to use a calculator to check your answers.
2 12
-3.5 4
63.5 0
5.6 4

#### Solution Preview

The following is verified using the executable file.
when x=2, y=12 the output is ...

#### Solution Summary

Help with exponentiation is achieved.

\$2.19