Purchase Solution

exponentiation

Not what you're looking for?

Ask Custom Question

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.
x y 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.
x y Expected answer
2 12
-3.5 4
63.5 0
5.6 4

Purchase this Solution

Solution Summary

Help with exponentiation is achieved.

Solution Preview

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

Purchase this Solution


Free BrainMass Quizzes
Java loops

This quiz checks your knowledge of for and while loops in Java. For and while loops are essential building blocks for all Java programs. Having a solid understanding of these constructs is critical for success in programming Java.

C++ Operators

This quiz tests a student's knowledge about C++ operators.

Basic UNIX commands

Use this quiz to check your knowledge of a few common UNIX commands. The quiz covers some of the most essential UNIX commands and their basic usage. If you can pass this quiz then you are clearly on your way to becoming an effective UNIX command line user.

Inserting and deleting in a linked list

This quiz tests your understanding of how to insert and delete elements in a linked list. Understanding of the use of linked lists, and the related performance aspects, is an important fundamental skill of computer science data structures.

Basic Computer Terms

We use many basic terms like bit, pixel in our usual conversations about computers. Are we aware of what these mean? This little quiz is an attempt towards discovering that.