Lecture 01

advertisement
C201 Schedule of First Two
Weeks: Review of C101
 Week 1
 Functions
 Array and sorting
 Week 2
 C-String
 An application
5-1
Program 1: Determine the day of a
week
This is formula to determine the day of a week given the date
information
W = (k + floor(2.6*m - 0.2) – 2*C + Y + floor(Y/4.0) + floor(C/4.0)) % 7
Where
 floor() denotes the integer floor function,
 k is day (1 to 31),
 m is month (1 = March, ..., 10 = December, 11 = Jan, 12 = Feb), treat
Jan & Feb as months of the preceding year,
 C is century (1987 has C = 19, 2000 has C=20 except C=19 for Jan &
Feb),
 Y is year (1987 has Y = 87 except Y = 86 for Jan & Feb),
 W is week day (0 = Sunday, ..., 6 = Saturday), if W is less than 0, add 7.
5-2
Program 2
Any even number larger than 2 can be
expressed as the sum of two prime
numbers (Goldbach's conjecture). Write
a program that takes input of an even
integer number n (larger than or equal to
4) and find two prime numbers p1 and p2,
such that n = p1 + p2, and print p1 and p2.
For example, 10 = 3 + 7, or 8 = 3 + 5, or
20 = 13 + 7.
5-3
Program 3
Any integer number larger than 5 can be
expressed as the sum of three prime
numbers (Goldbach's conjecture). Write
a program that takes input of an integer
number n (larger than or equal to 6) and
find three prime numbers p1, p2, and p3
such that n = p1 + p2 +p3, and print p1,
p2, and p3. For example, 6 = 2 + 2 + 2, or
8 = 3 + 2 + 3, 11 = 3 + 3 + 5
5-4
Array in C++
 Size and index
 Search an array
 Sort an array
5-5
C-String
 The ending mark: the ‘\0’character
 C-String functions
 strlen()
 strcmp()
 Strcpy()
 Strcat()
 Implementations
5-6
An Application
 Write a program that asks a user to enter
his/her first name, year of birth, and weight (in
pounds) in one line separated with spaces.
The program should show if the user is
overweight using the following formula
Age
≤ than 5
>5
Weight
≥ 98.5
< 98.5
≥ 198.5
Result
Overweight
Can not tell
Overweight
<198.5
Can not tell
5-7
Download