Int i j k i 2 j 3 i k 3 i i k j i k j 16 cpts 121

This preview shows 15 out of 22 pages.

int i, j, k; i = 2; j = 3 + i++; k = 3 + ++i; i *= ++k + j--; i /= k-- + ++j;
Image of page 15

Subscribe to view the full document.

16 CptS 121 - 9/30/09 Prof. Chris Hundhausen Counter Loops (cont.) Notice that we can rewrite our previous while loop with these new operators: int count; count = 0; /* Initialize counter */ while (count < 3) { axles = read_num_axles(infile); weight = read_weight(infile); toll = compute_toll(axles,weight); display_toll(axles, weight, toll); count++; /* increment counter */ }
Image of page 16
17 CptS 121 - 9/30/09 Prof. Chris Hundhausen Counter Loops (cont.) Notice that we can also rewrite our previous for loop with these new operators: int count; for (count = 0; count < 3; count++ ) { axles = read_num_axles(infile); weight = read_weight(infile); toll = compute_toll(axles,weight); display_toll(axles, weight, toll); }
Image of page 17

Subscribe to view the full document.

18 CptS 121 - 9/30/09 Prof. Chris Hundhausen Counter Loops (cont.) Accumulating a value Suppose that we want to keep track of the total number of tolls collected by the tollbooth. We could accumulate this value from within the loop body: int count, toll_total; for (count = 0, toll_total = 0 ; count < 3; count++) { axles = read_num_axles(infile); weight = read_weight(infile); toll = compute_toll(axles,weight); display_toll(axles, weight, toll); toll_total += toll; /* add toll to toll_total */ }
Image of page 18
19 CptS 121 - 9/30/09 Prof. Chris Hundhausen Counter Loops (cont.) Loop increments other than 1
Image of page 19

Subscribe to view the full document.

20 CptS 121 - 9/30/09 Prof. Chris Hundhausen You Try It With a partner, complete the following function: /* fib -- returns the nth number in the Fibonacci sequence. Note that the Fibonacci sequence is defined as follows: Fib(0) = 0; fib(1) = 1; fib(n) = fib(n-1) + fib(n-2) (for n>1) */ int fib(int n) { /* Fill this in */ }
Image of page 20
21 CptS 121 - 9/30/09 Prof. Chris Hundhausen You Try It (cont.) Solution int fib(int n) { /* one student group fills in their solution here. */ }
Image of page 21

Subscribe to view the full document.

22 CptS 121 - 9/30/09 Prof. Chris Hundhausen Next Lecture… We'll discuss several additional loop patterns: Conditional loops Sentinel-controlled loops Endfile-controlled loops Flag-controlled loops
Image of page 22
You've reached the end of this preview.
  • Spring '09
  • hundhaus
  • Prof. Chris Hundhausen

{[ 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