0% found this document useful (0 votes)
21 views2 pages

Sheet 2

The document outlines a series of programming tasks that involve creating functions for various calculations and simulations, including temperature conversion, coin tossing, finding the smallest number, calculating parking charges, computing integer powers, and playing a guessing game. Each task provides specific requirements and examples of expected outputs. The document emphasizes the use of functions and loops to achieve the desired results.

Uploaded by

Hosam Mohammed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views2 pages

Sheet 2

The document outlines a series of programming tasks that involve creating functions for various calculations and simulations, including temperature conversion, coin tossing, finding the smallest number, calculating parking charges, computing integer powers, and playing a guessing game. Each task provides specific requirements and examples of expected outputs. The document emphasizes the use of functions and loops to achieve the desired results.

Uploaded by

Hosam Mohammed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Sheet 2

1) The formula for converting a temperature from Fahrenheit to Celsius is


C = (5/9) * (F – 32)
where F is the Fahrenheit temperature and C is the Celsius temperature. Write a function
named Celsius that accepts a Fahrenheit temperature as an argument. The function
should return the temperature, converted to Celsius. Demonstrate the function by
calling it in a loop that displays a table of the Fahrenheit temperatures 0 through 20 and
their Celsius equivalents.

2) Write a function named coinToss that simulates the tossing of a coin. When you call
the function, it should generate a random number in the range of 1 through 2. If the
random number is 1, the function should display "heads." If the random number is 2,
the function should display "tails." Demonstrate the function in a program that asks the
user how many times the coin should be tossed, and then simulates the tossing of the
coin that number of times.

3) Write a program that inputs three double-precision, floating-point numbers and


passes them to a function that returns the smallest number.

4) (Parking Charges) A parking garage charges a $2.00 minimum fee to park for up to
three hours. The garage charges an additional $0.50 per hour for each hour or part
thereof in excess of three hours. The maximum charge for any given 24-hour period is
$10.00. Assume that no car parks for longer than 24 hours at a time. Write a program
that calculates and prints the parking charges for each of three customers who parked
their cars in this garage yesterday. You should enter the hours parked for each customer.
Your program should print the results in a neat tabular format and should calculate and
print the total of yesterday’s receipts. The program should use the function
calculateCharges to determine the charge for each customer. Your outputs should
appear in the following format:

5) Write a function integerPower(base, exponent) that returns the value of baseexponent


For example, integerPower(3, 4) = 3 * 3 * 3 * 3. Assume that exponent is a positive,
nonzero integer and that base is an integer. Do not use any math library functions.
6) Write a program that plays the 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 displays the following:

I have a number between 1 and 1000.


Can you guess my number?
Please type your first guess.

The player then types a first guess. The program responds with one of the following:
1. Excellent! You guessed the number!
Would you like to play again (y or n)?
2. Too low. Try again.
3. Too high. Try again.

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 player Too high or Too low to
help the player “zero in” on the correct answer.

7) Write a recursive function power( base, exponent ) that, when invoked, returns
base exponent
For example, power( 3, 4 ) = 3 * 3 * 3 * 3. Assume that exponent is an integer greater
than or equal to 1.

You might also like