Presentation is loading. Please wait.

Presentation is loading. Please wait.

Display a 12-Month Calendar CS-2301 D-term 20091 Programming Assignment #2 12-Month Calendar CS-2301 System Programming C-term 2009 (Slides include materials.

Similar presentations


Presentation on theme: "Display a 12-Month Calendar CS-2301 D-term 20091 Programming Assignment #2 12-Month Calendar CS-2301 System Programming C-term 2009 (Slides include materials."— Presentation transcript:

1 Display a 12-Month Calendar CS-2301 D-term 20091 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)

2 Display a 12-Month Calendar CS-2301 D-term 20092 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

3 Display a 12-Month Calendar CS-2301 D-term 20093 Sample Output (fragment) January Sun 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 February Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 … Note justification – right justified under day of week Two spaces between days

4 Display a 12-Month Calendar CS-2301 D-term 20094 Implementation Need to use loop statements for loop while loop Nested loops Need to use conditional statements if-else statement switch and break statements

5 Display a 12-Month Calendar CS-2301 D-term 20095 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!

6 Display a 12-Month Calendar CS-2301 D-term 20096 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!

7 Display a 12-Month Calendar CS-2301 D-term 20097 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)

8 Display a 12-Month Calendar CS-2301 D-term 20098 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

9 Display a 12-Month Calendar CS-2301 D-term 20099 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......

10 Display a 12-Month Calendar CS-2301 D-term 200910 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

11 Display a 12-Month Calendar CS-2301 D-term 200911 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

12 Display a 12-Month Calendar CS-2301 D-term 200912 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

13 Display a 12-Month Calendar CS-2301 D-term 200913 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

14 Display a 12-Month Calendar CS-2301 D-term 200914 Questions on Assignment?


Download ppt "Display a 12-Month Calendar CS-2301 D-term 20091 Programming Assignment #2 12-Month Calendar CS-2301 System Programming C-term 2009 (Slides include materials."

Similar presentations


Ads by Google