Assignment 4 for CSCI 201

Description:

In this assignment, you are asked to write two programs. Both programs will consist of more than one function.

Program 1:

(worth 15 points)

Write a function that will compute x raise to the power n. The prototype for this function should be:


double power(double x, int n);
The function should correctly handle all integer powers, including negative powers. Also build into the function that 0 raised to any power is 0, and that any number raised to the 0 power is 1. Write a program that tests this function by prompting the user for a real number and an integer power. The program should display the answer returned by the function. Do not use the pow() function in the math library to write this program.

Program 2:

(worth 10 points)

The greatest common divisor (GCD) of two integers is the largest integer that evenly divides each of these two numbers. For example, the GCD of 6 and 4 is 2 while the GCD of 5 and 10 is 5. Write a function called gcd that returns the greatest common divisor of two integers, and then write a program to test this function with input from the user.

What to turn in:

Both programming assignments must be turned in by transferring the respective C++ source code files to your ftp directory for this class. For each program, only the source code file (i.e., the file with the .cpp extension) should be turned in. The source code for program 1 should be named prog1.cpp and the source code for program 2 should be named prog2.cpp.