Title: CSC 1520 Computer Principles
1CSC 1520 Computer Principles C Programming
2Outlines
- Example Program
- Assignment 2
- Some Practices
3How to write a program to determine the Day of
Week?
An Example Program
This is useful to finish Assignment 2!
4Day of Week
- Given a date (e.g. 23/3/2008)
- Calculate the day number, jdn, using the
following formulas.
Reference http//en.wikipedia.org/wiki/Julian_day
Calculation
5Day of Week
- Decode the day number (jdn mod 7) to obtain the
Day of Week.
6Day of Week
- Example Program
- Input
- A date in between 2000 and 2999.
- E.g. 23 9 2008
- Output
- Which day of the week the specified date is.
- E.g. Tuesday
day month year
7(No Transcript)
8Day of Week
- We need these in order to use functions like
cin, cout, and to declare strings.
9Day of Week
- Declare the variables which are used in the
program.
10Day of Week
- Prompt the user to input a date.Note The date
is composed of three numbers day, month, year. - Read the date and store it into three variables
(day, month, and year).
11Day of Week
- Code the calculation part
- Convert the formulas into statements.
12Day of Week
- ?x? (floor)
- Return the largest integer number that is not
greater than x. - E.g.
- ?2.3? 2
- ?3.8? 3
- ?3 / 2? ?1.5? 1
When handling division, taking floor is the same
as having integer division, which drops the
remainder. E.g. 3 / 2 1 (remainder 1)
13Day of Week
- Obtain the day of week by decoding the value of
the variable dow.
14Day of Week
- Print out the answer
- FormatDay of week for 23/9/2008 is Tuesday
- End the main function
15Run it!
16Its your turn now!
17Assignment 2
Deadline 115959pm, 6 Oct (Monday)
18Easter
- We have Easter holiday every year.
- Easter is the most important festival in the
Christian year. - Christians celebrate this day in observance of
Jesus Christ's resurrection two days after his
death by crucifixion. - Easter Sunday, refers to as the "third day"
including the day of crucifixion.
Do you know how to determine the Easter holiday?
19Easter
- Computus (Latin for computation)
- The calculation of the date of Easter in the
Christian calendar. - Canonical rule
- Easter day is the first Sunday after the 14th day
of the lunar month (the nominal full moon ???)
that falls on or after 21 March (nominally the
day of the vernal equinox ??).
20Easter
- Meeus/Jones/ButcherGregorian Algorithm
- Calculates the date of Easter
- Input year Y
- Output month day
21- mod (modulo) ?
- Used to divide two integer numbers and return
only the remainder. - E.g.
- 10 3 1
- 20 10 0
- ?x? (floor)
- Return the largest integer number that is not
greater than x. - E.g.
- ?2.3? 2
- ?3.8? 3
22Example
- Suppose Y 2009
- a 2009 mod 19 14
- b ?2009 / 100? 20
- c 2009 mod 100 9
- d ?20 / 4? ?5? 5
- e 20 mod 4 0
- f ?(20 8) / 25? ?1.12? 1
- g ?(20 1 1) / 3? ?6.667? 6
- h (19 14 20 5 6 15) mod 30 20
- i ?9 / 4? ?2.25? 2
- k 9 mod 4 1
- L (32 2 0 2 2 20 1) mod 7 1
- m ?(14 11 20 22 1) / 451? ?0.568? 0
- month ?(20 1 7 0 114) / 31? ?4.355?
4 - day (20 1 7 0 114) mod 31 1 12
- Date of Easter in year 2009 is 12 April 2009
23How to write a program to determine the Date of
Easter?
Assignment 2
24Example Program
- Go to the directory where the .exe program
locates
25Input
- When the program starts to run, it will
- Prompt user to enter a year, and
- Wait for users input.
26Input
- When user types a year (e.g. 2009) and presses
, the program will read in the year and
start the calculation.
Here, you only need to read in ONE integer value!
27Output
- At the end, the program will
- Calculate the date of Easter of the specified
year, and - Print the date out in a suitable format.
28Output Format
Day
Year
Month (full name, in English)
29Points To Note
- In this assignment, you have to know how to
- Handle the input
- Q Do you know how to declare variables, use
cin, and store the things read in? - Convert the formulas into statements
- Q Do you know how to write expressions?
- Output the date of Easter
- Q Do you know how to use cout?
30More Reminders
- The required file format
- Please add the following to each of your source
file
/ CSC1520 Assignment 2 Student
ID 01234567 Name Woo Hiu Chun
e-mail adr hcwoo_at_cse.cuhk.edu.hk /
31More Reminders
- Step 1 Create an empty project named s0XXXXXX
where s0XXXXXX is your computing ID. - Step 2 Create a source file named s0XXXXXX.cpp
to the project. - Step 3 Type the codes into the source file
editor. Save it! - Step 4 Build your project to generate the .exe
file. - Step 5 Go to the command line interface and
enter the directory where your program (the .exe
file) locates. - Step 6 Run it! Type the .exe file name.
s0XXXXXX.exe or, simply s0XXXXXX
32More Reminders
- Check carefully if your program gives correct
answers in the correct format. - Submit your .cpp source file to moodle under
"Assignment 2" - The moodle address
- http//moodle.cuhk.edu.hk/course/view.php?id988
33Some Practices
34Operators
- Assignment Operators Short Form
- Example
- a a 3 ? a 3
- b b / 3 ? b / 3
- /
35Operators
- Exercise
- k a 3 is equivalent to
- k k (a 3)
- k (k a) 3
- m / 4 s is equivalent to
- m (m / 4) s
- m m / (4 s)
- n a - 5 is equivalent to
- n n (a - 5)
- n (n a) - 5
36Operators
- Exercise
- k a 3 is equivalent to
- k k (a 3)
- k (k a) 3
- m / 4 s is equivalent to
- m (m / 4) s
- m m / (4 s)
- n a - 5 is equivalent to
- n n (a - 5)
- n (n a) - 5
?
?
?
?
?
?
37Operators
- Increment Operator
- Increase the value of a variable by one.
- E.g.
- a (same as a a 1)
- Decrement Operator --
- Decrease the value of a variable by one.
- E.g. b-- (same as b b - 1)
38Operators
- Exercise
- Are they correct?
- i
- --j
- (b 12)
- 9--
- a--
- k a - --b
39Operators
- Exercise
- Are they correct?
- i
- --j
- (b 12)
- 9--
- a--
- k a - --b
?
?
?
?
?
k a - --b is better written as a --b k
a b
?
40- Thank you!
- Feel free to email me if you have any problems.