Presentation is loading. Please wait.

Presentation is loading. Please wait.

Discussion of Assignment #2 CS-2301, B-Term 20091 Discussion of Assignment #2 CS-2301, System Programming for Non-Majors (Slides include materials from.

Similar presentations


Presentation on theme: "Discussion of Assignment #2 CS-2301, B-Term 20091 Discussion of Assignment #2 CS-2301, System Programming for Non-Majors (Slides include materials from."— Presentation transcript:

1 Discussion of Assignment #2 CS-2301, B-Term 20091 Discussion of Assignment #2 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel)

2 Discussion of Assignment #2 CS-2301, B-Term 20092 Thinking Through the Problem int startingDay; /* init from user input*/ for (int month = 0; month < 12; month++) { }// for month

3 Discussion of Assignment #2 CS-2301, B-Term 20093 Calendar Assignment (continued) int startingDay; /* init from user input*/ for (int month = 0; month < 12; month++) { }// for month This variable is “global” to the loop. I.e., it is remembered from one iteration to the next.

4 Discussion of Assignment #2 CS-2301, B-Term 20094 Calendar Assignment (continued) int month, date; int startingDay; /* init from user input*/ for (month = 0; month < 12; month++) { }// for month At beginning of each iteration, startingDay indicates the day of the week on which that particular month starts. It is the responsibility of the loop to update startingDay for the next month. This is the beginning of a Loop Invariant!

5 Discussion of Assignment #2 CS-2301, B-Term 20095 Definition – Loop Invariant Something that is true at a certain point of each iteration of the loop Usually at the start E.g., a relationship of the variables Often expressed as a logical statement called an assertion Needs to be preserved from one iteration to the next Does not necessarily remain true within the loop, but only at the specific point each time through

6 Discussion of Assignment #2 CS-2301, B-Term 20096 Calendar Assignment (continued) int month, date; int startingDay; /* init from user input*/ for (month = 0; month < 12; month++) { const int daysInMonth = …; /* set # of days */ int dayOfWeek = 0; printf(…); //month name printf(…); //days of week }// for month

7 Discussion of Assignment #2 CS-2301, B-Term 20097 Calendar Assignment (continued) int month, date; int startingDay; /* init from user input*/ for (month = 0; month < 12; month++) { const int daysInMonth = …; /* set # of days */ int dayOfWeek; printf(…); //month name printf(…); //days of week for (dayOfWeek = 0; dayOfWeek<startingDay; dayOfWeek++) printf(/*blanks*/); }// for month Note that dayOfWeek is global to this loop. It is remembered outside this inner loop.

8 Discussion of Assignment #2 CS-2301, B-Term 20098 Calendar Assignment (continued) int month, date; int startingDay; /* init from user input*/ for (month = 0; month < 12; month++) { const int daysInMonth = …; /* set # of days */ int dayOfWeek; printf(…); //month name printf(…); //days of week for (dayOfWeek = 0; dayOfWeek<startingDay; dayOfWeek++) printf(/*blanks*/); }// for month What is the loop invariant associated with this loop?

9 Discussion of Assignment #2 CS-2301, B-Term 20099 Calendar Assignment (continued) int month, date; int startingDay; /* init from user input*/ for (month = 0; month < 12; month++) { const int daysInMonth = …; /* set # of days */ int dayOfWeek; printf(…); //month name printf(…); //days of week for (dayOfWeek = 0; dayOfWeek<startingDay; dayOfWeek++) printf(/*blanks*/); for (int date = 1; date <= daysInMonth; date++){ }// for date }// for month

10 Discussion of Assignment #2 CS-2301, B-Term 200910 Calendar Assignment (continued) int month, date; int startingDay; /* init from user input*/ for (month = 0; month < 12; month++) { const int daysInMonth = …; /* set # of days */ int dayOfWeek; printf(…); //month name printf(…); //days of week for (dayOfWeek = 0; dayOfWeek<startingDay; dayOfWeek++) printf(/*blanks*/); for (int date = 1; date <= daysInMonth; date++){ }// for date }// for month What should the loop invariant be for this loop? Why?

11 Discussion of Assignment #2 CS-2301, B-Term 200911 Calendar Assignment (continued) int month, date; int startingDay; /* init from user input*/ for (month = 0; month < 12; month++) { const int daysInMonth = …; /* set # of days */ int dayOfWeek; printf(…); //month name printf(…); //days of week for (dayOfWeek = 0; dayOfWeek<startingDay; dayOfWeek++) printf(/*blanks*/); for (int date = 1; date <= daysInMonth; date++){ printf("…", date); if (++dayOfWeek>6) { printf("\n"); dayOfWeek = 0; }; }// for date }// for month

12 Discussion of Assignment #2 CS-2301, B-Term 200912 Calendar Assignment (continued) int month, date; int startingDay; /* init from user input*/ for (month = 0; month < 12; month++) { const int daysInMonth = …; /* set # of days */ int dayOfWeek; printf(…); //month name printf(…); //days of week for (dayOfWeek = 0; dayOfWeek<startingDay; dayOfWeek++) printf(/*blanks*/); for (int date = 1; date <= daysInMonth; date++){ printf("…", date); if (++dayOfWeek>6) { printf("\n"); dayOfWeek = 0; }; }// for date }// for month Is loop invariant preserved by this loop? Why?

13 Discussion of Assignment #2 CS-2301, B-Term 200913 Calendar Assignment (continued) int month, date; int startingDay; /* init from user input*/ for (month = 0; month < 12; month++) { const int daysInMonth = …; /* set # of days */ int dayOfWeek; printf(…); //month name printf(…); //days of week for (dayOfWeek = 0; dayOfWeek<startingDay; dayOfWeek++) printf(/*blanks*/); for (int date = 1; date <= daysInMonth; date++){ printf("…", date); if (++dayOfWeek>6) { printf("\n"); dayOfWeek = 0; }; }// for date if (dayOfWeek != 0) printf(“\n”); }// for month What about the original loop invariant for this loop?

14 Discussion of Assignment #2 CS-2301, B-Term 200914 Calendar Assignment (continued) int month, date; int startingDay; /* init from user input*/ for (month = 0; month < 12; month++) { const int daysInMonth = …; /* set # of days */ int dayOfWeek; printf(…); //month name printf(…); //days of week for (dayOfWeek = 0; dayOfWeek<startingDay; dayOfWeek++) printf(/*blanks*/); for (int date = 1; date <= daysInMonth; date++) { printf("…", date); if (++dayOfWeek>6) { printf("\n"); dayOfWeek = 0; }; }// for date if (dayOfWeek !=0) printf(“\n”); startingDay = dayOfWeek; }// for month

15 Discussion of Assignment #2 CS-2301, B-Term 200915 Questions?


Download ppt "Discussion of Assignment #2 CS-2301, B-Term 20091 Discussion of Assignment #2 CS-2301, System Programming for Non-Majors (Slides include materials from."

Similar presentations


Ads by Google