CSC 1520 Computer Principles - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

CSC 1520 Computer Principles

Description:

Easter is the most important festival in the Christian year. ... Easter Sunday, refers to as the 'third day' including the day of crucifixion. ... – PowerPoint PPT presentation

Number of Views:158
Avg rating:3.0/5.0
Slides: 41
Provided by: hcw9
Category:

less

Transcript and Presenter's Notes

Title: CSC 1520 Computer Principles


1
CSC 1520 Computer Principles C Programming
  • Tutorial 3
  • - May

2
Outlines
  • Example Program
  • Assignment 2
  • Some Practices

3
How to write a program to determine the Day of
Week?
An Example Program
This is useful to finish Assignment 2!
4
Day 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
5
Day of Week
  • Decode the day number (jdn mod 7) to obtain the
    Day of Week.

6
Day 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)
8
Day of Week
  • We need these in order to use functions like
    cin, cout, and to declare strings.

9
Day of Week
  • Declare the variables which are used in the
    program.

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

11
Day of Week
  • Code the calculation part
  • Convert the formulas into statements.

12
Day 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)
13
Day of Week
  • Obtain the day of week by decoding the value of
    the variable dow.

14
Day of Week
  • Print out the answer
  • FormatDay of week for 23/9/2008 is Tuesday
  • End the main function

15
Run it!
16
Its your turn now!
17
Assignment 2
Deadline 115959pm, 6 Oct (Monday)
18
Easter
  • 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?
19
Easter
  • 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 ??).

20
Easter
  • 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

22
Example
  • 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

23
How to write a program to determine the Date of
Easter?
Assignment 2
24
Example Program
  • Go to the directory where the .exe program
    locates

25
Input
  • When the program starts to run, it will
  • Prompt user to enter a year, and
  • Wait for users input.

26
Input
  • 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!
27
Output
  • At the end, the program will
  • Calculate the date of Easter of the specified
    year, and
  • Print the date out in a suitable format.

28
Output Format
Day
Year
Month (full name, in English)
29
Points 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?

30
More 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 /
31
More 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
32
More 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

33
Some Practices
34
Operators
  • Assignment Operators Short Form
  • Example
  • a a 3 ? a 3
  • b b / 3 ? b / 3

- /
35
Operators
  • 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

36
Operators
  • 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

?
?
?
?
?
?
37
Operators
  • 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)

38
Operators
  • Exercise
  • Are they correct?
  • i
  • --j
  • (b 12)
  • 9--
  • a--
  • k a - --b

39
Operators
  • 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.
Write a Comment
User Comments (0)
About PowerShow.com