220 likes | 466 Views
Programming Assignment #2 12-Month Calendar. CS-2301 System Programming C-term 2009 (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). Display a 12-Month Calendar.
E N D
Programming Assignment #212-Month Calendar CS-2301 System Programming C-term 2009 (Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie and from C: How to Program, 5th and 6th editions, by Deitel and Deitel) Display a 12-Month Calendar
Display a 12-Month Calendar • Prompt user for day of week of January 1 • 0 = Sunday • 1 = Monday • … • 6 = Saturday • Prompt user whether leap year or not • Print calendar for twelve months Display a 12-Month Calendar
Note justification – right justified under day of week Two spaces between days Sample Output (fragment) JanuarySun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 FebruarySun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 … Display a 12-Month Calendar
Implementation • Need to use loop statements • for loop • while loop • Nested loops • Need to use conditional statements • if-else statement • switch and break statements Display a 12-Month Calendar
Hints • Think through the data you need to keep track of • Which month and how many days per month • Which day of week and when to start new line • Think through how to organize loops to • Cycle through the months • Cycle through the days within a month • Communicate day of week from one month to next • Ask questions next time! Display a 12-Month Calendar
More on printf() & scanf() • Prints a string in which each conversion specifier is replaced with value of corresponding argument • int printf("string in double quotes", arg1, arg2, arg3, …); • Conversion specifier:– • Begins with '%' character • Describes how to print one argument • ith conversion specifier in string says how to print argi • Resulting string printed on stdout • Returns number of characters printed! Display a 12-Month Calendar
printf()(continued) • %d, %i — decimal number • %u — unsigned decimal number • %c — character • %s — string • %f — floating point number • e.g., 3.14159 • %e, E — floating point number • e.g., 2.9979e+8 or 2.9979E+8 • %% — a single ‘%’ character • See textbook for full list (pp 154, 244) Display a 12-Month Calendar
printf() Optional Controls • %6.4f • ^ – minimum field width • Padded on left • ^ – precision of number • or minimum number of digits for decimal number • %.12s • ^ – width of string • %-6.4f • ^ – indicates left justify • Padded on right Display a 12-Month Calendar
printf() Examples int j = 24; float twoPi = 2 * pi; printf("j=%d, k=%f\n", j, twoPi ); • Output j=24, k=6.28319 • Return from printf() = 16 printf("%4d %4d %4d %6d", 1, 10, 100, 1000); • Output 1 10 100 1000 .. .. .. .. .. .. Display a 12-Month Calendar
scanf() • Reads input, decomposes into individual variables • Opposite of printf() • scanf("string in double quotes", &arg1, &arg2, &arg3, …); • Arguments must be locations – use ‘&’ • Converts input string according to scan string, stores in variables • Returns number of matched and stored items • Or the value EOF if end of file is encountered • Stops at end of string or when no match is found Display a 12-Month Calendar
scanf() Examples int i; double x; char c; scanf("%d%f%c", &i, &x, &c); • Looks first for an integer • Skips white space • Looks next for a floating point • Skips white space • Looks next for a single character • Does not skip white space; returns the first character it finds Display a 12-Month Calendar
scanf() Formats • %d — any decimal number • %u — an unsigned integer • %c — character • White space not skipped • %e, f, g — floating point number • %s — string • Defer to later in the course • %% — matches a single ‘%’ character • Any other character • Matches that character Display a 12-Month Calendar
scanf() Formats Must specify “h” or “l” indicating short or long integer, float vs. double • %d — any decimal number • %u — an unsigned integer • %c — character • White space not skipped • %e, f, g — floating point number • %s — string • Defer to later in the course • %% — matches a single ‘%’ character • Any other character • Matches that character Display a 12-Month Calendar
Questions on Assignment? Display a 12-Month Calendar