Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE Application Programming

Similar presentations


Presentation on theme: "ECE Application Programming"— Presentation transcript:

1 16.216 ECE Application Programming
Instructor: Dr. Michael Geiger Fall 2011 Lecture 24: Finishing PE4: Functions

2 ECE Application Programming: Lecture 24
Lecture outline Announcements/reminders Removing project; programs now worth 60% Exam 2: Wednesday 11/9 Allowed 1 double-sided sheet of notes Practice problems to be posted over weekend Prog. 6 to be posted this weekend; due Mon, 11/14 Prog. 5 grading done today; regrades due Mon, 11/7 No late penalties; will post solution 11/7 Today: Finishing PE4 (functions) Identify where function(s) can be used Write code Test using debugger 9/14/2018 ECE Application Programming: Lecture 24

3 ECE Application Programming: Lecture 24
PE4: Change problem Given any amount of change under $2.00, determine and print out the minimum number of coins required to make that amount of change. Available coins are Halves (half dollars), Quarters, Dimes, Nickels, and Pennies. 9/14/2018 ECE Application Programming: Lecture 24

4 ECE Application Programming: Lecture 24
Flowcharting - Sample Start scanf amount nHalves = 0 Given some amount of money, amount, how many half dollars would be returned? myAmt = amount myAmt < 0.50 myAmt = myAmt nHalves = nHalves + 1 printf nHalves Finish 9/14/2018 ECE Application Programming: Lecture 24

5 Expand to all coins - page 1
Start nH = 0 scanf amount myAmt < 0.50 myAmt = amount A myAmt = myAmt nH = nH+ 1 B 9/14/2018 ECE Application Programming: Lecture 24

6 ECE Application Programming: Lecture 24
B C nQ = 0 nN = 0 myAmt < 0.25 myAmt < 0.05 myAmt = myAmt myAmt = myAmt nQ = nQ + 1 nN = nN + 1 nD = 0 nP = 0 myAmt < 0.10 myAmt < 0.01 myAmt = myAmt myAmt = myAmt nD = nD + 1 nP = nP + 1 9/14/2018 ECE Application Programming: Lecture 24 C D

7 ECE Application Programming: Lecture 24
D printf nH,nQ,nD,nN,nP Finish 9/14/2018 ECE Application Programming: Lecture 24

8 ECE Application Programming: Lecture 24
When to use functions Find a particular step or series of steps being repeated in your code If code is exactly the same, you need no arguments If one or more values change, but the actual calculations that need them are the same, those values can be parameters of your function (arguments) Reassess change example Steps for creating change are extremely similar What changes each time? How should we use those values in a function? Inputs, outputs, variables … ? 9/14/2018 ECE Application Programming: Lecture 24

9 ECE Application Programming: Lecture 24
B General case: nC=number coins Output vC=value of coin Input myAmt=amt left Input/Output nQ = 0; vQ=0.25 myAmt < vQ myAmt = myAmt - vQ nQ = nQ + 1 function: change nC = 0; vC=input nD = 0; vD=0.10 myAmt < vC myAmt < vD myAmt = myAmt - vC myAmt = myAmt - vD nC = nC + 1 nD = nD + 1 return 9/14/2018 ECE Application Programming: Lecture 24 C

10 (addr nC, val vC, addr Amt) change(&nQ,0.25,&myAmt)
Start General case: nC=number coins Output vC=value of coin Input myAmt=amt left Input/Output scanf amount myAmt = amount change(&nH,0.5,&myAmt) function:change (addr nC, val vC, addr Amt) change(&nQ,0.25,&myAmt) change(&nD,0.1,&myAmt) *nC = 0; vC=input *myAmt < vC change(&nN,0.05,&myAmt) change(&nP,0.01,&myAmt) *myAmt = *myAmt - vC printf nH,nQ,nD,nN,nP *nC = *nC + 1 return 9/14/2018 ECE Application Programming: Lecture 24 Finish

11 ECE Application Programming: Lecture 24
Next time Exam 1 Preview 9/14/2018 ECE Application Programming: Lecture 24


Download ppt "ECE Application Programming"

Similar presentations


Ads by Google