cpts121-6-3

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

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

1 C ptS  121 L6-3 – 10/2/09 Pro f. C hris  Hundha us 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

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

View Full Document
2 C ptS  121 L6-3 – 10/2/09 Pro f. C hris  Hundha us 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
3 C ptS  121 L6-3 – 10/2/09 Pro f. C hris  Hundha us 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

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

View Full Document
4 C ptS  121 L6-3 – 10/2/09 Pro f. C hris  Hundha us 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); }
5 C ptS  121 L6-3 – 10/2/09 Pro f. C hris  Hundha us 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

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

View Full Document
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
7

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 01/25/2011 for the course CPTS 121 taught by Professor Hundhaus during the Spring '09 term at Washington State University .

### Page1 / 21

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

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

View Full Document
Ask a homework question - tutors are online