Sei sulla pagina 1di 4

SIE1003/TLM1004

Tutorial 4

SIE1003/TLM1004
Tutorial 4: C Functions
1. For the following program, state the scope (function scope, file scope, block scope, or functionprototype scope) of each of the following elements.
(a) The variable a in main.
(b) The function square.
(c) The variable b in square.
(d) The function main.
(e) The function prototype for square.
(f)

The identifier b in the function prototype for


square.

#include <stdio.h>
int square(int b);
int main(void)
{
int a = 1;
while (a <= 5) {
printf(%d\n, square(a));
a++;
}
int square(int b)
{
return b * b;
}

2. Find the error(s) in each of the following program segments and explain how the error can be
corrected.
(a) int sum(int x, int y)
{
int result;
result = x + y;
}

(b) void f(float a);


{
float a;
printf(%f, a);
}

(c) int sum(int n)


{
if (0 == n)
return 0;
else
n + sum(n-1);
}

SIE1003/TLM1004

Tutorial 4

(d) void product(void)


{
int a, b, result;
printf(%s, Enter two integers: );
scanf(%d%d, &a, &b);
result = a * b;
printf(Result is %d, result);
return result;
}

(e) double cube(float);

// function prototype
...
cube(float number)
// function definition
{
return number*number;
}

3. Write (using pen and paper) a C program that reads two radiuses, r1 and r2, of type double and
passes them to a function areaOfEllipse that calculates the area of an ellipse (*r1*r2) and
returns the area of the ellipse of type double. Print the area of the ellipse in one decimal place.
Define as 3.14159.

4. Write a function that takes two integer distances (one in kilometres and one in meters) travelled
by a car and returns the total distance travelled by the car in meters. Write a C program that uses
this function to calculate the distance travelled (in meters) by three cars. A sample run of the
program is given below.
Enter distances (in kilometres
Distance travelled: 1050 m
Enter distances (in kilometres
Distance travelled: 830 m
Enter distances (in kilometres
Distance travelled: 2467 m
Total distance travelled: 4347

and meters): 1 50
and meters): 0 830
and meters): 2 467
m

SIE1003/TLM1004

Tutorial 4

5. Write a C program that prompts the user to enter two points (x1, y1) and (x2, y2), and displays (in
two decimal places) the distance between the two points, which is computed by a function
distance using the equation below. All variables are of type double, and the result is displayed
in two decimal places. A sample run of the program is shown below.

x2 x1

y2 y1

Enter first point (x,y): 3 4


Enter second point (x,y): 0 0
Distance between (3.00,4.00) and (0.00,0.00): 5.00

6. A baggage counter charges a $0.50 minimum fee to deposit bag for up to three hours and an
additional $0.25 per hour for each hour or part thereof over three hours. Write a C program that
calculates and prints the baggage counter charges for each of five customers and the total of all
five charges. The program should use the function calculateCharges to determine the charge
for each customer. A sample run of the program is given below.
Enter the number of hours for 5 bags deposited: 1 1.5 3 3.4 5.6
Customers
Hours
Charge ($)
1
1.00
0.50
2
1.50
0.50
3
3.00
0.50
4
3.40
0.75
5
5.60
1.25
TOTAL
14.50
3.50

7. Write a recursive function power(base,exponent) that when invoked returns baseexponent. The
recursive step uses the relationship baseexponent = base*baseexponent-1, and the terminating condition
occurs when exponent = 1 because base1 = base. For example, power(3,4) = 3*3*3*3. In your C
program, exponent is an integer greater than or equal to 1. Both base and exponent are unsigned
long integers, while the return power value is an unsigned long long integer. A sample run of the
program is shown below.
Enter a base and an exponent: 2 5
2 raised to the 5 is 32

SIE1003/TLM1004

Tutorial 4

8. Write a C function guessGame to play the game of guess the alphabet. The function first
randomly selects a letter in the range A to Z, and then prompts the player to guess the letter. The
player types a first guess, and the function responds with one of the following: Too low! Please
try again, Too high! Please try again or Great! Your guess is right. The function
should loop until the player gets the letter right, and should prompt the player to play the game
again. The program only terminates when the player does not want to play the game again. A
sample run of the program is illustrated below.

What is the selected letter? P


Too low! Please try again: S
Too low! Please try again: W
Too high! Please try again: U
Great! Your guess is right
Would you like to play again?
Enter 1 for Yes or 2 for No: 1
What is the selected letter? J
Too high! Please try again: E
Too low! Please try again: G
Great! Your guess is right
Would you like to play again?
Enter 1 for Yes or 2 for No: 2

ASCII Character Set

0
1
2
3
4
5
6
7
8
9
10
11
12

nul

soh

stx

etx

eot

enq

ack

bel

bs

ht

lf

vt

ff

cr

so

si

dle

dcl

dc2

dc3

dc4

nak

syn

etb

can

em

sub

esc

fs

gs

rs

us

sp

"

&

'

<

>

del

The American Standard Code for Information Interchange ASCII Character Set The digits at the left
of the table are the left digits of the decimal equivalent (0-127) of the character code, and the digits
at the top of the table are the right digits of the character code. For example, the character code for
F is 70, and the character code for & is 38. Reference from C: How to Program (Paul Deitel and
Harvey Deitel, 2013).

Potrebbero piacerti anche