Display a 12-Month Calendar CS-2301 D-term 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 CS-2301 D-term 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 CS-2301 D-term Sample Output (fragment) January Sun Mon Tue Wed Thu Fri Sat February Sun Mon Tue Wed Thu Fri Sat … Note justification – right justified under day of week Two spaces between days
Display a 12-Month Calendar CS-2301 D-term 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 CS-2301 D-term 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 CS-2301 D-term 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 arg i Resulting string printed on stdout Returns number of characters printed!
Display a 12-Month Calendar CS-2301 D-term printf() (continued) %d, %i — decimal number %u — unsigned decimal number %c — character %s — string %f — floating point number e.g., %e, E — floating point number e.g., e+8 or E+8 % — a single ‘%’ character See textbook for full list (pp 154, 244)
Display a 12-Month Calendar CS-2301 D-term 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 CS-2301 D-term printf() Examples int j = 24; float twoPi = 2 * pi; printf("j=%d, k=%f\n", j, twoPi ); Output j=24, k= Return from printf() = 16 printf("%4d %4d %4d %6d", 1, 10, 100, 1000); Output
Display a 12-Month Calendar CS-2301 D-term 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 CS-2301 D-term 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 CS-2301 D-term 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 CS-2301 D-term 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 Must specify “ h ” or “ l ” indicating short or long integer, float vs. double
Display a 12-Month Calendar CS-2301 D-term Questions on Assignment?