Course: Programming Fundamentals

In Lab Assignment 8

Date: 8th April, 2013 Lab Instructors: Hafsah Qaiser

Question 1:

Write an entire C++ program that reads a positive integer entered by an interactive user and then prints out all the positive divisors of that integer in a column and in decreasing order. The program should allow the user to repeat this process as many times as the user likes. Initially, the program should inform the user about how the program will behave.

Then the program should prompt the user for each integer that the user wishes to enter. Make sure the program does not accept any negative or zero input. When the user accidentally enters a zero or negative integer to have its divisors calculated, the program should inform the user that the input is unacceptable and should allow the user to try again (and again!).

Sample Run:

Please enter a positive integer: 36

36

18

12

9

6

4

3

2

1

Would you like to see the divisors of another integer (Y/N)? y

Please enter a positive integer: -44

-44 is not a positive integer.

Please enter a positive integer: 0

0 is not a positive integer.

Please enter a positive integer: 109

109

1

Would you like to see the divisors of another integer (Y/N)? m

Please respond with Y (or y) for yes and N (or n) for no.

Would you like to see the divisors of another integer (Y/N)? n

Note:

You should write separate functions for:

·  Printing the initial information

·  Getting the number input from the user and returning it to main

·  Printing the divisors

·  Getting the character input to determine user wishes to continue or not and returning integer value i.e. 1 for yes and 0 for no.

Question 2:

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 100 times and count the number of times each side of the coin appears. Print the results. The program should call a separate function ‘flip’ that takes no arguments and returns 0 for tails and 1 for heads.

Question 3:

Write a C++ program to compute Sin(x) where

sinx= x1!- x33!+ x55!- x77!+x99!- … xnn!

Your program should accept two values from the user (the angle x and the value of n) and then should compute and print the value of sin(x). To make the program, do following tasks.

·  Write two functions, i.e. function to calculate factorial and function to calculate power having following prototypes.

double Factorial (int n); //Factorial function prototype

double Power(double x, int y); //Power function prototype

·  Use these functions in your main function to compute the series.

Question 4:

Part A

Write a program that plays game of “guess the number” as follows: your program chooses the number to be guessed by selecting an integer at random in the range 1 to 1000. The program then types:

The player then types a first guess. The program responds with one of the following:

If the player’s guess is incorrect your program should loop until the player finally gets the number right. Your program should keep telling the prayer High or Low to help the player guess the correct answer.

(Part B)

Modify the above program to count the number of guesses the player makes. If the number is fewer than 10, print Either you know the secret or you got lucky! If the player guesses the number in 10 tries, then print Ahah! You know the secret! If the player makes more than 10 guesses, then print You should be able to do better!

Note:

·  All the tasks must be implemented using functions.

·  If not specified the count or the signature of function, then must decide it by your own.

“Success is not achieved by contended people”. […]