Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Assignment #1 12-Month Calendar—

Similar presentations


Presentation on theme: "Programming Assignment #1 12-Month Calendar—"— Presentation transcript:

1 Programming Assignment #1 12-Month Calendar—
Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie, Absolute C++, by Walter Savitch, The C++ Programming Language, Special Edition, by Bjarne Stroustrup, and from C: How to Program, 5th and 6th editions, by Deitel and Deitel) CS-2303, A-Term 2012 PA1 — Twelve Month Calendar

2 PA1 — Twelve Month Calendar
Goals and Objectives Develop a simple C program on a Linux platform based on your Java skills Become familiar with C idiosyncrasies pertaining to loops, switches, and conditional statements Learn to use formatted input and output in C CS-2303, A-Term 2012 PA1 — Twelve Month Calendar

3 Display a 12-Month Calendar
Prompt for a non-negative year number (e.g., 2012) Determine starting day-of-the-week and whether or not it is a leap year Print calendar for twelve months for that year CS-2303, A-Term 2012 PA1 — Twelve Month Calendar

4 Sample Output (fragment)
January Sun Mon Tue Wed Thu Fri Sat February Sun Mon Tue Wed Thu Fri Sat … Two spaces between days Note – right justified under day of week CS-2303, A-Term 2012 PA1 — Twelve Month Calendar

5 PA1 — Twelve Month Calendar
Implementation Loop statements — similar to Java for loop while loop Nested loops Conditional statements — also similar to Java if-else statement switch and break statements Function declarations — similar to Java, but not part of any class Calling functions is similar to Java Formatted input and output — very different from Java CS-2303, A-Term 2012 PA1 — Twelve Month Calendar

6 PA1 — Twelve 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! CS-2303, A-Term 2012 PA1 — Twelve Month Calendar

7 PA1 — Twelve Month Calendar
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:– Part of the "string in double quotes" 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! Note – the "... " means that the number of arguments is open-ended CS-2303, A-Term 2012 PA1 — Twelve Month Calendar

8 PA1 — Twelve Month Calendar
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 K&R textbook for full list (pp 154, 244) CS-2303, A-Term 2012 PA1 — Twelve Month Calendar

9 printf() Optional Controls
^ – 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 CS-2303, A-Term 2012 PA1 — Twelve Month Calendar

10 PA1 — Twelve Month Calendar
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); CS-2303, A-Term 2012 PA1 — Twelve Month Calendar

11 PA1 — Twelve 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 argument string or when no match is found CS-2303, A-Term 2012 PA1 — Twelve Month Calendar

12 PA1 — Twelve 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, including newline characters Looks next for a floating point Looks next for a single character Does not skip white space; returns the first character it finds CS-2303, A-Term 2012 PA1 — Twelve Month Calendar

13 PA1 — Twelve 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 CS-2303, A-Term 2012 PA1 — Twelve Month Calendar

14 PA1 — Twelve 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 CS-2303, A-Term 2012 PA1 — Twelve Month Calendar

15 PA1 — Twelve Month Calendar
Summary of Assignment Prompt user for a non-negative year number (e.g., 2012) Determine starting day-of-the-week and whether or not it is a leap year Print calendar for twelve months for that year CS-2303, A-Term 2012 PA1 — Twelve Month Calendar

16 PA1 — Twelve Month Calendar
Submission Submit on Turnin as PA1 Program name PA1.c Project value:– 20 points Rules:– Program must compile without warnings On CCC systems Program must be named PA1.c CS-2303, A-Term 2012 PA1 — Twelve Month Calendar

17 Questions on Assignment?
Digression on Loop Invariants CS-2303, A-Term 2012 PA1 — Twelve Month Calendar


Download ppt "Programming Assignment #1 12-Month Calendar—"

Similar presentations


Ads by Google