lecture7

# lecture7 - Wednesday, October 27 Function Re w vie...

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

Wednesday, October 27 th Function Review Program Design and Decomposition Typecasting If we have some time, we’ll start ASCII

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

View Full Document
Function Review The foo function computes the average of two numbers and: 1. Returns the sum of the numbers. 1. Sends back the average of the numbers in the third reference parameter . Whats wrong with it? VOID foo(int n1, n2, int avg) ( int sum; sum += num1; sum += num2; return(sum); avg = sum / 2; ] int main(void) { int mean, sum; sum = func(5,9,mean); cout << sum << “ “ << mean; } { = 0; avg = sum / 2; return(sum); } int foo(int n1, n2, int avg) int foo(int n1, int n2, int avg) int foo(int n1, int n2, int & avg) int sum; sum += num1; sum += num2; return(sum); avg = sum / 2; { int mean, sum; sum = func(5,9,mean); cout << sum << “ “ << mean;
Function Review What does the following program print out? What is the value of credit after lines 1 , 2 and 3 have executed. float credit; void charge(float amount) { if (amount <= credit) { cout << “Approved.\n”; credit -= amount; } else cout << “Rejected!\n”; } int main(void) { credit = 100.00; // starting charge(52); // line 1 charge(10); // line 2 charge(40); // line 3 }

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

View Full Document
Function + Static Review float moving_avg(float cur) // avg of last 3 { static float s1,s2,s3; s1=s2; s2=s3; s3=cur; return((s1+s2+s3)/3); } int main(void) { float avg; avg=moving_avg(3); cout << avg << “\n”; avg=moving_avg(6); cout << avg << “\n”; avg=moving_avg(1); cout << avg << “\n”; avg=moving_avg(2); cout << avg << “\n”; } What does it print? s1 0 s2 0 s3 0 avg 3 cur 3 3 (0+0+3)/3 1 6 6 3 6 (0+3+6)/3 3 1 3 1 1 3 6 1 (3+6+1)/3 3.3 Etc…
Reference Review void Silly(int byValue, int &byReference) { byValue = 10; byReference = 20; } void main() { int a = 5, b = 6; Silly(a,b); } When we pass a variable without using it is called passing by value . When we pass a variable using a reference, it is called passing by reference . When we pass by value, the program makes a copy of the actual parameter and sends it to the function.

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

View Full Document
Reference Review void Silly(int byValue, int &byReference) { byValue = 10; byReference = 20; } void main() { int a = 5, b = 6; Silly(a,b); } When we pass a variable without using it is called passing by value . When we pass a variable using a reference, it is called passing by reference . When we pass by reference , the program references the actual parameter in the calling function.
void Silly(int byValue, int & byReference) { byValue = 10; byReference = 20; } void main() { int a = 5, b = 6; Silly(a,b); cout << a << “ “ << b; } a 5 b 6 byValue 5 byReference 10 20 Output: Reference Review What does it print?

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

View Full Document
Programming Challenge void main(void) { int eyes, ears; cin >> eyes >> ears; switch (eyes + ears) { case 0: cout << “I think “; case 1: cout << “you are a goblin”; break; case 2: case 3: cout << “You ghoul ”; case 4: cout << “or a human”; break; default: cout << “not a ghost”; break; } } Convert the switch statement to the left into the equivalent set of if statements .
Top Down Design Today we’re going to learn how to attack large programming problems By breaking a big problem down into smaller, more manageable components, we make it easier to solve.

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

View Full Document
Top Down Design Imagine if you were redecorating a bedroom You’d probably want to break up the task into a set of smaller steps : Remove all furniture. Remove carpet. Strip old wallpaper.
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 10/20/2009 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

### Page1 / 42

lecture7 - Wednesday, October 27 Function Re w vie...

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

View Full Document
Ask a homework question - tutors are online