Download presentation
Presentation is loading. Please wait.
1
Assignment #2, 12- month Calendar CS-2301, B-Term 20091 Programming Assignment #2 12-Month Calendar 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)
2
Assignment #2, 12- month Calendar CS-2301, B-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
Assignment #2, 12- month Calendar CS-2301, B-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
Assignment #2, 12- month Calendar CS-2301, B-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 May also need to use condition expression I.e., expr 1 ? expr 2 : expr 3
5
Assignment #2, 12- month Calendar CS-2301, B-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
Assignment #2, 12- month Calendar CS-2301, B-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
Assignment #2, 12- month Calendar CS-2301, B-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) %g, G – make intelligent decision between %e, %f
8
Assignment #2, 12- month Calendar CS-2301, B-Term 20098 printf() Optional Controls %6.4f ^ – minimum field width Padded on left %6.4f ^ – precision of number or minimum number of digits for decimal number %.12s ^ – width of string %-6.4f ^ – indicates left justify Padded on right
9
Assignment #2, 12- month Calendar CS-2301, B-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
Assignment #2, 12- month Calendar CS-2301, B-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
Assignment #2, 12- month Calendar CS-2301, B-Term 200911 scanf() Examples int i; double x; char c; scanf("%d%lf%c", &i, &x, &c); Looks first for an integer Skips white space Looks next for a double floating point Skips white space Looks next for a single character Does not skip white space; returns the first character it finds
12
Assignment #2, 12- month Calendar CS-2301, B-Term 200912 scanf() Formats %d — any decimal number (positive or negative) %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
Assignment #2, 12- month Calendar CS-2301, B-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 Note discrepancy in book about these specifiers
14
Assignment #2, 12- month Calendar CS-2301, B-Term 200914 Questions?
15
Assignment #2, 12- month Calendar CS-2301, B-Term 200915 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
16
Assignment #2, 12- month Calendar CS-2301, B-Term 200916 Use of Loop Invariant You must think about what your loop does You must think about what the variables in your loop mean You must think about the relationships among your variables from one loop iteration to the next Write these down in your README file! This information is the loop invariant
17
Assignment #2, 12- month Calendar CS-2301, B-Term 200917 Grading Rubric – 20 Points Total Correct compilation without warnings – 2 points Correct execution with graders’ test cases – 5 points Correct usage of scanf() to get input from user – 1 point Correct usage of print() to print the various lines of the calendar – 3 points Correct usage of conditional and loop statements – 5 points Satisfactory README file – 2 points Loop invariant(s) in README document – 2 points
18
Assignment #2, 12- month Calendar CS-2301, B-Term 200918 Questions About Homework?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.