1 / 11

16.216 ECE Application Programming

16.216 ECE Application Programming. Instructor: Dr. Michael Geiger Fall 2011 Lecture 24: Finishing PE4: Functions. Lecture outline. Announcements/reminders Removing project; programs now worth 60% Exam 2: Wednesday 11/9 Allowed 1 double-sided sheet of notes

cicada
Download Presentation

16.216 ECE Application Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Lecture outline • Announcements/reminders • Removing project; programsnow 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 ECE Application Programming: Lecture 24

  3. 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. ECE Application Programming: Lecture 24

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

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

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

  7. D printf nH,nQ,nD,nN,nP Finish ECE Application Programming: Lecture 24

  8. 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 … ? ECE Application Programming: Lecture 24

  9. 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 ECE Application Programming: Lecture 24 C

  10. 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 ECE Application Programming: Lecture 24 Finish

  11. Next time • Exam 1 Preview ECE Application Programming: Lecture 24

More Related