Embed
Email

tutorial2

Document Sample

Shared by: gegeshandong
Categories
Tags
Stats
views:
1
posted:
10/27/2011
language:
English
pages:
3
CS1010E Programming Methodology

Semester 1 2010/2011



Week 30th August of 2010

Tutorial 2

Basic Function



1. (Writing Function) Function is usually used to capture generally useful computations. For each of the

following function prototypes, give the complete function definition (i.e. full coding). You should

also write a full program to demonstrate the usage of the function.



a. int rounding( double input );

//Take input and perform rounding to integer (see tutorial 1 question 3e)

//example: 11.1  11, 11.5  12



b. double squareRoot( double x );

//Return the square root of x, using the “Babylonian Method” as in lab 1 task 3

// give the 4th approximation



2. (Function Design) A good function should:

i. Perform a single well defined task only

ii. Whenever applicable, rely only on the input parameter to produce output

iii. Reusable

Check whether the following function meets the above criteria.



a.

double calculateAreaOfCircle( )

{

double radius, area;



printf(“Please enter radius of circle:” );

scanf(“%lf”, &radius );



area = 3.14159 * radius * radius;

return area;

}



b.

double calPyramidBaseArea( double length, double width,

double height )



{

double baseArea;



baseArea = length * width;

return baseArea;

}



1|Page

If there is enough time during tutorial, you are encouraged to show your own function to the class

and ask for feedback on design.



3. (Problem Solving with Function)

Everyday, Mr.Lightyear will travel from Home to his office in the morning. After work, he will visit

NTUC for groceries before returning to Home.



Suppose we represent the locations: Home, office and NTUC as two dimensional Cartesian

coordinate (X, Y). Write a simple program to calculate the total distance travelled by Mr.Lightyear

everyday. You can use the squareRoot() function in question 1 if needed.







4. (Tracing and Scoping Rule)



Trace the execution of the following programs. This exercise is to highlight:

 Execution flow of function calls

 Relationship between formal parameter and actual argument

 Scoping rule of variables



a.



#include



void swap( int x, int y ); // Function Prototype

// attempt to exchange x and y



int main()

{



int x, y;



x = 3;

y = 4;



swap( x , y );



printf(“X is %d\n”, x );

printf(“Y is %d\n”, y );



return 0;

}



void swap( int x, int y )

{

int temp; //temporary storage



temp = x;

x = y;

y = temp;



return;

}





2|Page

b.



#include



int f( int x, int y ); // Function Prototype

// calculate x^2 + y^2



int main()

{



int x, y, result;



x = 3;

y = 4;



result = f( x , y );



x = f( y, result + x );



y = f( 1, f( result, 2 ) );



return 0;

}



int f( int x, int y)

{

return x*x + y*y;

}



5. (Using Function)

If the data type of the argument passed to a function does not match the data type of the formal

parameter, data type conversion (promotion) will occur automatically. The effect is similar to the

data type conversion in assignment statement.



Given the following functions:

int integerAdd( int x, int y)

{

return x + y;

}



double doubleAdd( double x, double y)

{

return x + y;

}



What is the value stored in the result variable in the following usages?



a. int result;

result = doubleAdd( 4.7, 5.5);



b. double result;

result = integerAdd( 4.75, 5.25);



3|Page



Related docs
Other docs by gegeshandong
Mar - Mr Hanson
Views: 0  |  Downloads: 0
WhatDoYouMeanHighest.Price
Views: 0  |  Downloads: 0
core data
Views: 0  |  Downloads: 0
jan-18-2009b
Views: 0  |  Downloads: 0
Status - California State University
Views: 0  |  Downloads: 0
PHASE ONE
Views: 0  |  Downloads: 0
By registering with docstoc.com you agree to our
privacy policy

You are almost ready to download!

You are almost ready to download!