Discussion of Assignment 2 CS2301 System Programming for

  • Slides: 15
Download presentation
Discussion of Assignment #2 CS-2301, System Programming for Non-Majors (Slides include materials from The

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) CS-2301, B-Term 2009 Discussion of Assignment #2 1

Thinking Through the Problem int starting. Day; /* init from user input*/ for (int

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

Calendar Assignment (continued) int starting. Day; /* init from user input*/ the loop. to

Calendar Assignment (continued) int starting. Day; /* init from user input*/ the loop. to l” a b lo g “ is This variable from one d e r e b m e r I. e. , it is next. iteration to the for (int month = 0; month < 12; month++) { } // for month CS-2301, B-Term 2009 Discussion of Assignment #2 3

Calendar Assignment (continued) int month, date; int starting. Day; /* init from user input*/

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

Definition – Loop Invariant • Something that is true at a certain point of

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 CS-2301, B-Term 2009 Discussion of Assignment #2 5

Calendar Assignment (continued) int month, date; int starting. Day; /* init from user input*/

Calendar Assignment (continued) int month, date; int starting. Day; /* init from user input*/ for (month = 0; month < 12; month++) { const int days. In. Month = …; /* set # of days */ int day. Of. Week = 0; printf(…); //month name printf(…); //days of week } // for month CS-2301, B-Term 2009 Discussion of Assignment #2 6

Calendar Assignment (continued) int month, date; int starting. Day; /* init from user input*/

Calendar Assignment (continued) int month, date; int starting. Day; /* init from user input*/ for (month = 0; month < 12; month++) { const int days. In. Month = …; /* set # of days */ int day. Of. Week; printf(…); //month name printf(…); //days of week for (day. Of. Week = 0; day. Of. Week<starting. Day; day. Of. Week++) printf(/*blanks*/); } al to b o l g s ek i Of. We y a d hat Note t. op inner s i h this lo t side t u o d bere m e It is r loop. // for month CS-2301, B-Term 2009 Discussion of Assignment #2 7

Calendar Assignment (continued) int month, date; int starting. Day; /* init from user input*/

Calendar Assignment (continued) int month, date; int starting. Day; /* init from user input*/ for (month = 0; month < 12; month++) { const int days. In. Month = …; /* set # of days */ int day. Of. Week; printf(…); //month name printf(…); //days of week for (day. Of. Week = 0; day. Of. Week<starting. Day; day. Of. Week++) printf(/*blanks*/); } oop l e h t is What s loop? hi with t ated i c o s s ant a i r a v n i // for month CS-2301, B-Term 2009 Discussion of Assignment #2 8

Calendar Assignment (continued) int month, date; int starting. Day; /* init from user input*/

Calendar Assignment (continued) int month, date; int starting. Day; /* init from user input*/ for (int date = 1; date <= days. In. Month; date++){ for (month = 0; month < 12; month++) { const int days. In. Month = …; /* set # of days */ int day. Of. Week; printf(…); //month name printf(…); //days of week for (day. Of. Week = 0; day. Of. Week<starting. Day; day. Of. Week++) printf(/*blanks*/); CS-2301, B-Term 2009 } } Discussion of Assignment #2 // for date // for month 9

Calendar Assignment (continued) int. Wmonth, hat shouldate; d th lo/* intfostarting. Day; from op

Calendar Assignment (continued) int. Wmonth, hat shouldate; d th lo/* intfostarting. Day; from op ininit r this loop euser v a r i a n t be input*/ ? for (int date = 1; date <= days. In. Month; date++){ Wh(month y? for = 0; month < 12; month++) { const int days. In. Month = …; /* set # of days */ int day. Of. Week; printf(…); //month name printf(…); //days of week for (day. Of. Week = 0; day. Of. Week<starting. Day; day. Of. Week++) printf(/*blanks*/); CS-2301, B-Term 2009 } } Discussion of Assignment #2 // for date // for month 10

Calendar Assignment (continued) int month, date; int starting. Day; /* init from user input*/

Calendar Assignment (continued) int month, date; int starting. Day; /* init from user input*/ for (int date = 1; date <= days. In. Month; date++){ for (month = 0; month < 12; month++) { const int days. In. Month = …; /* set # of days */ int day. Of. Week; printf(…); //month name printf(…); //days of week for (day. Of. Week = 0; day. Of. Week<starting. Day; day. Of. Week++) printf(/*blanks*/); CS-2301, B-Term 2009 } } Discussion of Assignment #2 printf("…", date); if (++day. Of. Week>6) { printf("n"); day. Of. Week = 0; }; // for date // for month 11

Calendar Assignment (continued) int month, date; loop inva int. Isstarting. Day; init from riant

Calendar Assignment (continued) int month, date; loop inva int. Isstarting. Day; init from riant pr/* e serinput*/ loop? W user ved by hy? for (int date = 1; date <= days. In. Month; date++){ this for (month = 0; month < 12; month++) { const int days. In. Month = …; /* set # of days */ int day. Of. Week; printf(…); //month name printf(…); //days of week for (day. Of. Week = 0; day. Of. Week<starting. Day; day. Of. Week++) printf(/*blanks*/); CS-2301, B-Term 2009 } } Discussion of Assignment #2 printf("…", date); if (++day. Of. Week>6) { printf("n"); day. Of. Week = 0; }; // for date // for month 12

Calendar Assignment (continued) int month, date; int starting. Day; /* init from user input*/

Calendar Assignment (continued) int month, date; int starting. Day; /* init from user input*/ for (int date = 1; date <= days. In. Month; date++){ for (month = 0; month < 12; month++) { const int days. In. Month = …; /* set # of days */ int day. Of. Week; printf("…", date); if (++day. Of. Week>6) { printf("n"); day. Of. Week = 0; }; } // for date if (day. Of. Week != 0) printf(“n”); printf(…); //month name printf(…); //days of week W hat about the origin invariant a loop for (day. Of. Week for this lo = l 0; op? day. Of. Week<starting. Day; day. Of. Week++) printf(/*blanks*/); CS-2301, B-Term 2009 } Discussion of Assignment #2 // for month 13

Calendar Assignment (continued) int month, date; int starting. Day; /* init from user input*/

Calendar Assignment (continued) int month, date; int starting. Day; /* init from user input*/ for (int date = 1; date <= days. In. Month; date++) { printf("…", date); if (++day. Of. Week>6) { printf("n"); day. Of. Week = 0; }; } // for date if (day. Of. Week !=0) printf(“n”); for (month = 0; month < 12; month++) { const int days. In. Month = …; /* set # of days */ int day. Of. Week; printf(…); //month name printf(…); //days of week for (day. Of. Week = 0; day. Of. Week<starting. Day; day. Of. Week++) printf(/*blanks*/); CS-2301, B-Term 2009 starting. Day = day. Of. Week; } Discussion of Assignment #2 // for month 14

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

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