{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# tutorial03 - CS1010E Programming Methodology Semester 1...

This preview shows pages 1–3. Sign up to view the full content.

CS1010E Programming Methodology Semester 1 2011/2012 Week of 5 – 9 September 2011 Tutorial 3 Functions 1. In tutorial #2, we performed a trace of a program containing only the main function. We now extend our mental model to include traces of program execution involving more than one function. (a) #include <stdio.h> void s(int x, int y); // or void s(int, int); int main(void) { int x, y; x = 3; y = 4; printf("x = %d; y = %d.\n", x, y); s(x,y); printf("x = %d; y = %d.\n", x, y); s(y,x); printf("x = %d; y = %d.\n", x, y); return 0; } void s(int x, int y) { int temp; temp = x; x = y; y = temp; return; } 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
(b) #include <stdio.h> int f(int x, int y); int main(void) { int x = 3, y = 4; x = f(x,y); y = f(x, f(y,x)); printf("x = %d; y = %d\n", x, y); return 0; } int f(int x, int y) { return x*10 + y; } 2. To solve a complex problem, we can adopt a top-down approach during algorithm design, followed by a bottom-up incremental approach during implementation and testing. We use a familiar example from Tutorial #2 Question #6 to illustrate this process. Given a square inscribed in a circle with all four corners touching the cir- cumference of the circle, we would like to Fnd out the area of the circle given only the sides of the square, a . The following is an example of a top-down algorithm with major steps formulated as functions. Recall that a function may take in arguments and may return a result. Step 1. Read in the side of the square,
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 5

tutorial03 - CS1010E Programming Methodology Semester 1...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online