SlideServe
Menu
Préférences
© SlideServe · v2026

Coin Change Problem Solving in Programming

cicada avatar
11 diapositives · 234 Views · Aug 14, 2014

Learn how to determine the minimum coins needed for a given amount of change using functions in programming. Practice problems & solutions provided.

Coin Change Problem Solving in Programming - slide 1
01 / 11Coin Change Problem Solving in Programming
1 / 17

Télécharger la présentation

Coin Change Problem Solving in Programming
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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