{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

cpts121-6-3 - CptS 121 Fall 09 Lecture 6-3 HK Chapter 5...

Info icon This preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
1 C ptS  121 L6-3 – 10/2/09 Pro f. C hris  Hundhaus e n CptS 121 Fall ‘09 Lecture 6-3 HK Chapter 5: Iteration Lecture Outline I. Conditional loops II. Sentinel-controlled loops III. Endfile-controlled loops IV. Flag-controlled loops
Image of page 1

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

View Full Document Right Arrow Icon
2 C ptS  121 L6-3 – 10/2/09 Pro f. C hris  Hundhaus e n Conditional Loops In the previous lecture, we considered loops whose number of iterations was known at the time the loop started In practice, we don’t always know in advance how many times a loop will execute! Often, the loop body itself determines whether another execution is necessary
Image of page 2
3 C ptS  121 L6-3 – 10/2/09 Pro f. C hris  Hundhaus e n Conditional Loops (cont.) Consider, for example, the following extension to the Tollbooth application: Suppose that, for safety reasons, the tollbooth limits the number of vehicles that can cross the bridge on a given day to a certain tonnage. Suppose that you want to read in vehicle data from a file to simulate cars crossing the bridge. When the maximum tonnage has been reached, not more data are read from file.The program prints out a message reporting - the revenues collected for the day - the number and total weight of all cars that crossed for the day See next slide for a possible solution
Image of page 3

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

View Full Document Right Arrow Icon
4 C ptS  121 L6-3 – 10/2/09 Pro f. C hris  Hundhaus e n Conditional Loops (cont.) #include <stdio.h> #define MAX_TONS_PER_DAY 20 #define POUNDS_PER_TON 2000 void main() { int vehicle_count, axles; double weight, tons, toll, total_tolls; FILE *infile; infile = get_and_open_file(); count = tons = total_tolls = 0; while (tons < MAX_TONS_PER_DAY) { axles = read_num_axles(infile); weight = read_weight(infile); toll = compute_toll(axles,weight); count++; /* Another car has crossed. */ tolls += toll; /* Another toll has been collected */ tons += weight/2000.0; /* Add weight of this car */ /* to running total*/ } printf("%d cars, weighing %.2f tons, crossed the bridge today.", count, tons); printf("$%.2f in tolls was collected today.",total_tolls); }
Image of page 4
5 C ptS  121 L6-3 – 10/2/09 Pro f. C hris  Hundhaus e n Conditional Loops (cont.) A possible sequence of questions that can guide loop design, applied to previous example Question Answer Implications for design 1. What are the inputs? Weight of vehicle, # of  axles Input vars: weight axles 2. What are the outputs? # and weight of vehicles  that cross bridge; tolls  collected Output vars:  count, tons, total_tolls
Image of page 5

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

View Full Document Right Arrow Icon
6 CptS 121 L6-3 – 10/2/09 Prof. Chris Hundhausen Conditional Loops (cont.) A possible sequence of questions that can guide loop design, applied to previous example (cont.) Question Answer Implications for design 3. Is there repetition? Yes! We repeatedly Get # axles and weight Calculate toll Add weight to total  weight Add toll to total tolls Add 1 to vehicle count Program variable needed:  MAX_TONS_ PER_DAY
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern