5
CSC425 – INTRODUCTION TO COMPUTER PROGRAMMING SEMESTER SEPT’11 – DEC’11 TUTORIAL 5 - FUNCTION 1. Given the following source code. Labeled the bold text. #include <iostream.h> void calculate(int,int,int); void main() { int x, y, z; cin >> x >> y >> z; calculate(x,y,z); } void calculate(int a, int b, int c) { int total = a + b + c; cout << total; return; } 2. Write a function named OuterRectangle to calculate the circumference of an outer rectangle. Write another function named InnerRectangle to calculate the circumference of inner rectangle. Display the difference of circumference of both rectangles in the void main. 3. Write a function named Grade to determine the grade of a student whereby the total marks of coursework and final exam have been calculated in other function called TotalMarks. Refer the following table to determine the grade. Range of marks Grade 1 | Page

Tut5CSC425 (1)

Embed Size (px)

DESCRIPTION

SCience computer

Citation preview

Page 1: Tut5CSC425 (1)

CSC425 – INTRODUCTION TO COMPUTER PROGRAMMINGSEMESTER SEPT’11 – DEC’11

TUTORIAL 5 - FUNCTION

1. Given the following source code. Labeled the bold text.

#include <iostream.h>

void calculate(int,int,int);

void main(){

int x, y, z;

cin >> x >> y >> z;calculate(x,y,z);

}

void calculate(int a, int b, int c){

int total = a + b + c;cout << total;return;

}

2. Write a function named OuterRectangle to calculate the circumference of an outer rectangle. Write another function named InnerRectangle to calculate the circumference of inner rectangle. Display the difference of circumference of both rectangles in the void main.

3. Write a function named Grade to determine the grade of a student whereby the total marks of coursework and final exam have been calculated in other function called TotalMarks. Refer the following table to determine the grade.

Range of marks Grade0 – 49 F50 – 59 D60 – 69 C70 – 79 B80 – 100 A

1 | P a g e

Page 2: Tut5CSC425 (1)

4. Write two functions named CalculateBMI and BMILevel. Your program must enter the weight and height in the main program and send both parameters to a function named CalculateBMI. The second function named BMILevel will determine the BMI level of the person based on the calculated BMI from the first function. The following is the formula to calculate the BMI.

BMI = weight / (height * height)

The following is the table of BMI Level.

BMI Range Level< 15 15.01 – 25.00 Normal> 25.01 Obese

5. Explain the difference of passing parameter by reference and by value. Given example for each concept.

6. Explain the following terminologies.a) Global variable and local variableb) Actual parameter and formal parameterc) Function prototype and function definition

7. Write the function definition for each of the following:a) Function ConvertToinches () that receives a measurement in feet and inches as

parameters. Convert it to the equivalent inches, and return the value in inches.( HINT : 1 FOOT = 12 INCHES )

b) Function printEven () that will print all the even numbers within the two integer parameters in ascending order. For example, if the parameters are 2 and 9 respectively, the function prints:

The even numbers are: 4 6 8If the parameters are 12 and 1 respectively, the function prints:The even numbers are: 2 4 6 8 10

8. Write a function sumOfNumbers () that calculates and returns the sum of the numbers from 1 to n (where n is a parameter). For example, if the value of n is 5, the function will return 15 since 1 + 2 + 3 + 4 + 5=15.

2 | P a g e

Page 3: Tut5CSC425 (1)

9. Trace the following program and determine the output:

void functionX(int& x, int y, int z){

int temp;cout << x << " " « y << " " << zx = y + z;temp = x + y;y = y + temp + z;

}

int raain(){ int first = 5;

int second = 3;functionX(second, first, first – second);cout « first « " " « second « endl;int third = 3;int fourth = 4;functionX(third, fourth, 5);cout « third « " " « fourth « endl;return 0;

}

10. Write a complete C++ program for WorldParking Sdn Bhd. to perform the following:a) Write a return-value function named calccharges () to calculate and return the

parking charges for the customers. The company charges a RM1.00 minimum fee to park for up to one hour. An additional RM0.50 will be charged for each hour exceeding the first one hour. The maximum charge for any given 24-hour period is RM10.00. Assume that no car parks for longer than 24 hours at a time. Use the appropriate parameters to pass values in and out of the function.

b) Write a void function named calcTotal () to calculate the total charges for all the customers. Use the appropriate parameters to pass values in and out of the function.

c) Write the main program that allows the user to input number of customers and the ours parked for the customers. The program should use the function calccharges() above to calculate and print the parking charges for each customer and function calcTotal() above to calculate and print the total charges for all the customers.

3 | P a g e