CSCI 1120: Introduction To Computing Using C++ Tutorial 1
CSCI 1120: Introduction To Computing Using C++ Tutorial 1
• Assignment 1
Introduction to Mid Autumn Festival
Methodology
Programming tips
3
Install Visual Studio Community 2019
Step 1 Download
Visual Studio Community 2019 for Windows Desktop (free)
https://www.visualstudio.com
4
Install Visual Studio Community 2019
Step 2 Install
Select Desktop development with C++
5
Use Visual Studio Community 2019
Search:
Press “Win + Q”, then input “Visual Studio 2019”
6
Use Visual Studio Community 2019
Sign in
Sign in using your CUHK Office365 account or your
personal Microsoft account.
7
Use Visual Studio Community 2019
8
Use Visual Studio Community 2019
9
Use Visual Studio Community 2019
New Project Dialog
Step 1:
Select “Windows
Desktop Wizard”
Step 2:
Press “Next”
10
Use Visual Studio Community 2019
New Project Dialog
Step 3:
Input project name
and location, then
press “Create”
11
Use Visual Studio Community 2019
New Project Dialog
Step 3:
Input project name
and location, then
press “Create”
Step 4:
Select “Console
Application (.exe)”
for Application type
Tick “Empty Project”
12
Another way to create a new project (self study)
Step 1:
Select “Empty
Project”
Step 2:
Press “Next”
13
Another way to create a new project (self study)
Step 3:
Input project name
and location, then
press “Create”
14
Use Visual Studio Community 2019
Solution Explorer
15
Use Visual Studio Community 2019
16
Use Visual Studio Community 2019
Step 1:
Select “Code” Step 2:
Select “C++
Files”
Step 3:
Step 4:
Type your file name
Add
17
Use Visual Studio Community 2019
18
Use Visual Studio Community 2019
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!\n";
return 0;
}
19
Use Visual Studio Community 2019
20
Use Visual Studio Community 2019
21
Use Visual Studio Community 2019
22
Use Visual Studio Community 2019
Execution result
23
Use Command Line to Run Our Program
Open CMD
Press hot key “win + R”
24
Use Command Line to Run Our Program
Open CMD
25
Use Command Line to Run Our Program
26
visual studio c++ online
https://rextester.com/l/cpp_online_compiler_visual
27
Assignment 1:
Mid Autumn Festival
Introduction
• Mid Autumn Festival date changes every year. How to determine the
exact Mid Autumn Festival date?
29
Methodology
• Known facts:
• Mid Autumn Festival lies on the 15th day of the 8th month in the Chinese Lunar Calendar.
• New moon repeats every 29.53 days.
• 24 Aug is the earliest possible date for the 1st day of the 8th month in the Lunar Calendar.
• HKT 10:42, 19 Aug 2020 is a full moon.
• Assumption:
• Mid Autumn Festival usually appears between 7 Sep and 8 Oct.
• Solution:
• Find the first new moon on or after HKT 0:00, 24 Aug of a year.
• The accuracy is ±1 day in general (although there can be huge error in rare cases).
30
Methodology Formulation
• Let
𝑦 be the year which we want to estimate the Mid-Autumn Festival date
• 𝑚=8, and 𝑑=24, meaning 24 Aug
• The are shown as follows:
After computing the integer 𝑛, which means the number of days until the next new moon on or
after 24 Aug, and then the Mid-Autumn Festival is estimated as 𝑝=𝑛+14 days after 24 Aug.
https://en.wikipedia.org/wiki/Julian_day
[2] https://zh.wikipedia.org/wiki/%E5%84%92%E7%95%A5%E6%97%A5 31
Methodology Formulation
𝑦,𝑚,𝑑,𝑎,𝑏,𝑐,𝑗,𝑛,p are integers,
using “int” variable to
represent.
The day of the week (Mon–Sun) can be determined using ((𝑗+𝑛) mod 7)+1. The result is 1–7,
meaning Mon–Sun respectively.
32
Used Operations
Floor
⌊𝑥⌋ means the floor of 𝑥, that is, the largest integer not
greater than 𝑥. For example, ⌊3.2⌋ = ⌊3.98⌋ = 3.
For example,
int a = 7, b = 2;
cout << a / b; // Output 3 rather than 3.5
33
Used Operations
Ceil
⌈x⌉ means the ceil of 𝑥, that is, the least integer greater than
or equal to 𝑥. For example, ⌈3.2⌉ = ⌈3.98⌉ = 4.
For example,
double a = 3.2;
cout << ceil(a); // Output 3
34
Used Operations
Modulo
In computing, the modulo operation finds
the remainder after division of one number by another
(sometimes called modulus)
For example,
7 mod 2 = 1
124 mod 6 = 4
2018 mod 50 = 18
35
Used Operations
Modulo
In computing, the modulo operation finds
the remainder after division of one number by another
(sometimes called modulus)
For example;
int a = 7 % 2; // a = 1
int b = 124 % 6; // b = 4
int c = 2018 % 50; // c = 18
36
Example
37
Sample Run
Blue text is user input and the other text is the program printout.
2020
38
Sample Run
39
How to print the weekday as text?
• A simple way:
#include <string>
string name;
if (w == 1)
name = "Mon";
else if (w == 2)
name = "Tue";
else if (w == 3)
name = "Wed";
else if ...
...
• Other ways …
40
Submission and Marking
• Your program source file name should be midautumn.cpp. Do not use other
file names. Submit the file in Blackboard (https://blackboard.cuhk.edu.hk/).
• Insert your name, student ID, and e-mail as comments at the beginning of
your source file.
• You can submit your assignment multiple times. Only the latest submission
counts.
• Your program should be free of compilation errors and warnings.
• Your program should include suitable comments as documentation.
• Do NOT plagiarize. Sending your work to others is subjected to the same
penalty as the copier.
41
Thanks
42