1 / 14

Programming Assignment #2 12-Month Calendar

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.

chapmank
Download Presentation

Programming Assignment #2 12-Month Calendar

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. Questions on Assignment? Display a 12-Month Calendar

More Related