Work Sheet On Function Defination

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

HiLCoE

School of Computer Science & Technology

CS222Computer programming II Autumn 2009


Worksheet 2 (Functions)
Objective:
• To work on function definition

1. Write the prototype for a function named Perimeter(), which returns an unsigned
long int and takes two parameters, both unsigned short int s.

2. Write the definition of the function Perimeter() as described in Exercise 1. The


two parameters represent the length and width of a rectangle. Have the function
return the perimeter (twice the length plus twice the width).

3. Write a function that takes two unsigned short integer arguments and returns the result
of dividing the first by the second. Do not do the division if the second number is zero,
but do return –1.

4. Write a program that asks the user for two numbers and calls the function you wrote
in Exercise 3. Print the answer, or print an error message if you get –1.
5.The following algorithm was attributed by Euclid at the time 300 B.C. to find the greatest
common divisor of two integer numbers, a and b.
S1: Start
S2: Input a and b
S3: If a > b then interchange a and b
(i.e. do c = a ; a = b ; b = c)
S4: Check if a <= 0 then go to S7
S5: b = b – a
S6: Go to S3
S7: gcd = b (gcd is the return value)
S8: End
Write a C++ function named int gcd (int a, int b) that returns greatest common
divisor of two integer parameters (a and b) using Euclid algorithm. Test the function in a
main program.
6. Hand trace the codes below

a) f();
}
#include <iostream.h>
void h() {
#include <string.h>
f();
g();
int counter = 0;
f();
}
void f() {
int main() {
++counter;
f();
}
cout << counter << endl;
void g() {
counter = 0;
f();
By Ermias Bekele 1
g(); cout << counter << endl;
cout << counter << endl; return 0;
counter = 0; }
h();

9. This assignment is pledged. You may discuss its content and your approach with anyone, but
you must do all your own work. You must create the logic of the solution, type your own code,
and compile, execute, and debug your own program. You may not use any code developed by
anyone else.

Objective

The purpose of this homework is to familiarize you with conditional loops from your last term(I)
and function calls that exist in all realistic programs.

Problem

You are the programmer at cyber soft in Addis Ababa City. You have been tasked to develop a
computer program that can play Ace-High, a card game similar to, but simpler than, blackjack.
The rules and procedures are as follows.

1. Cards have their standard values (a numerical card has its numerical value in the range of
two to ten and face cards all have a value of ten), except that aces always have a value of
eleven. To make the game simpler, the drawing of a card will be simulated by a function
that will return an integer value in the range of 2 through 11, inclusive.
2. The program begins by printing a suitable welcome message (see example).
3. The player always draws first and always draws two cards initially. If the player "busts,"
that is, if the combined value of the player's two cards exceeds 21, then the player loses,
the dealer does not draw, and the game is over.
4. The dealer draws second. The dealer draws two cards initially. If the total of the two
cards is less than 17, the dealer then "hits" (draws another card) until the total of her or
his cards is greater than or equal to 17. If the dealer busts at any time (from the first two
cards or as a result of drawing more cards), then the player wins and the game is over
5. If at this point the dealer and player are tied, this forces a playoff in which both the dealer
and player each draw a single card. If the dealer's card is higher, the dealer wins; if the
player's card is higher, the player wins. If the dealer's and player's cards are equal, then
the playoff is repeated (i.e., each draws another card) until either the dealer or player
wins.
6. The program displays as much of this information as is pertinent to the hand:
o The value of each of the player's initial two cards and their total value;
o The value of each of the dealer's initial two cards and their total value;
o The value of the player's card(s) during a playoff;
o The value of the dealer's card(s) during a playoff;
o A statement declaring whether the player or dealer is the winner.

By Ermias Bekele 2
7. After the winner has been determined, the program asks if another game should be
played. An answer of "y" or "Y" starts another game; any other answer terminates the
game.

Example run
WELCOME TO THE ACE-HIGH GAME
Player draws 11 and 11, total 22.
Dealer wins.
Play again (y or n)? y
WELCOME TO THE ACE-HIGH GAME
Player draws 8 and 10, total 18.
Dealer draws 9 and 11, total 20.
Dealer wins.
Play again (y or n)? y
WELCOME TO THE ACE-HIGH GAME
Player draws 7 and 4, total 11.
Dealer draws 5 and 9, total 16.
Dealer draws 2, total 18.
Dealer wins.
Play again (y or n)? y
WELCOME TO THE ACE-HIGH GAME
Player draws 10 and 10, total 20.
Dealer draws 5 and 6, total 11.
Dealer draws 5 and 9, total 25.
Player wins.
Play again (y or n)? y
WELCOME TO THE ACE-HIGH GAME
Player draws 8 and 9, total 17.
Dealer draws 10 and 7, total 17.
Player draws 9.
Dealer draws 7.
Player wins.
Play again (y or n)? y
WELCOME TO THE ACE-HIGH GAME
Player draws 9 and 10, total 19.
Dealer draws 11 and 2, total 13.
Dealer draws 2, total 15.
Dealer draws 4, total 19.
Player draws 5.
Dealer draws 10.
Dealer wins.
Play again (y or n)? x
Game ended at player's request.

Notes

Use function named get_card() that will return a value in the range of 2 to 11 each time it is
called. To use this function, store its value into an integer value like this:

int my_card;
// my_card will be assigned a value in the range 2 to 11
my_card = get_card();

Submissions

When your program is finished, name the source ass2.cpp (you must use exactly these names!)
and mail to the e-mail you are given.

By Ermias Bekele 3

You might also like