CS260

MP5


Please see program documentation requirement.


Problem 1

Write a program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. Let the program toss the coin a number of times as specified by the user, and count the number of times each side of the coin appears. Print the results of each toss. The program should call a separate function flip that takes no arguments and returns 0 for tails and 1 for heads. Note: If the program realistically simulates the coin tossing, then each side of the coin should appear approximately half the time.

The program interaction should look as follows (user inputs are in bold):

Welcome to the Coin Tossing program. 

Enter the number of tosses (-1 to exit): 5 
Flip 1 is a: HEAD
Flip 2 is a: TAIL
Flip 3 is a: HEAD
Flip 4 is a: HEAD
Flip 5 is a: TAIL 

There were 3 heads and 2 tails in 5 tosses.

Enter the number of tosses (-1 to exit): -1

Thank you for using the Coin Flipping program and have a wonderful day.


Save your program as mp05a.cpp.


Problem 2

There's a well known function called the Ackermann function. This function usually appears in computer science courses and texts when the topic of recursion is discussed. The function is defined as:

Use the following function prototype to implement the Ackermann function as a recursive function:

long ack(long,long);

Some values for the Ackermann function are as follows:

ackerman(0,0)= 0
ackerman(1,0)= 0
ackerman(1,1)= 2
ackerman(1,2)= 4
ackerman(1,3)= 8
ackerman(2,1)= 2
ackerman(2,2)= 4
ackerman(2,3)= 16
ackerman(3,1)= 2
ackerman(3,2)= 4

The program interaction should look as follows (user inputs are in bold):

Welcome to the Ackermann program.

Enter values for x and y(-1 -1 to exit): 0 0

value is: 0

Enter values for x and y(-1 -1 to exit): 2 3

value is: 16

Enter values for x and y(-1 -1 to exit): -1 -1

Thank you for using the Ackermann program and have a wonderful day.


Save your program as mp05b.cpp.


Problem 3

Write a program implementing a C function called daPower. The function prototype should be:

float daPower(float, int);

The function should calculate the integer power of a floating point value using recursion (no loops allowed).


The program interaction should look as follows (user inputs are in bold):

Welcome to daPower program.

Enter values for x and power(-1 -1 to exit): 12.1 3

value is: 1771.561

Enter values for x and power(-1 -1 to exit): 2 3

value is: 8

Enter values for x and power(-1 -1 to exit): -1 -1

Thank you for using daPower program and have a wonderful day.

Save your program as mp05c.cpp


What You'll Turn In:

A diskette labeled with your name, course # and containing the files mp05a.cpp, mp05b.cpp and mp05c.cpp.