CSCI 1120
Introduction to Computing Using C++
Tutorial 1
14th Sep. 2020
About
• SHEN, Tiancheng ( 沈天成 ) [Class A tutor]
Email: tcshen@[Link]
Office: TBA
Consultation hour : Tue 9:00AM-10:00AM Thu 9:00AM-
10:00AM
Zoom ID: 916 0707 8857 Password: 239972
• DAI Xinyan ( 戴新顏 ) [Class B tutor]
Email: xydai@[Link]
Office: SHB 116
Consultation hour : Tue 10-12am
Zoom ID: 987 9198 0067 Password: 804570
2
Outline
• Visual Studio Community
Install Visual Studio Community 2019
Create our first project
Write and run our first C++ code
• 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)
[Link]
4
Install Visual Studio Community 2019
Step 2 Install
Select Desktop development with C++
5
Use Visual Studio Community 2019
Start Visual Studio Community
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
The main VS interface
8
Use Visual Studio Community 2019
Create a new project
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”
You may need the
location later on
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”
You may need the
location later on
14
Use Visual Studio Community 2019
Solution Explorer
15
Use Visual Studio Community 2019
Add new item
Right-click Source Files>Add>New Item…
16
Use Visual Studio Community 2019
Add new item
Step 1:
Select “Code” Step 2:
Select “C++
Files”
Step 3:
Step 4:
Type your file name
Add
17
Use Visual Studio Community 2019
The source code editor interface
Type your code here
18
Use Visual Studio Community 2019
Write our first program
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!\n";
return 0;
}
19
Use Visual Studio Community 2019
Build our first program
Build>Build Solution
20
Use Visual Studio Community 2019
Build our first program
Check building result in output window
21
Use Visual Studio Community 2019
Run our first program
Debug>Start Without Debugging (Ctrl+F5)
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
Enter commands to run your program
Step 1: Enter Debug folder
> D: <Enter>
> cd VSProjects\HelloWorld\Debug <Enter>
(Use the location you input in page11 or page 14)
Step 2: Run our program
> [Link] <Enter>
(Type the actual name of your program)
26
visual studio c++ online
[Link]
27
Assignment 1:
Mid Autumn Festival
Introduction
• Mid Autumn Festival date changes every year. How to determine the
exact Mid Autumn Festival date?
• The calculation is complex. But there are also easy methods to
estimate the rough 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.
[Link]
[2] [Link] 31
Methodology Formulation
𝑦,𝑚,𝑑,𝑎,𝑏,𝑐,𝑗,𝑛,p are integers,
using “int” variable to
represent.
• 𝑘 is a real number, using
“double” 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.
⌊𝑥⌋ and ⌈𝑥⌉ mean the floor and ceiling of 𝑥 respectively.
32
Used Operations
Floor
⌊𝑥⌋ means the floor of 𝑥, that is, the largest integer not
greater than 𝑥. For example, ⌊3.2⌋ = ⌊3.98⌋ = 3.
Hint: In C/C++, integer division actually returns the floor of
the result rather than the original mathematical result.
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.
Hint: In C/C++, the ceiling operation can be written as
ceil(…). You have to add #include <cmath> at the beginning
of your program.
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)
Hint: In C++ the operator for modulo operation is “%”
For example;
int a = 7 % 2; // a = 1
int b = 124 % 6; // b = 4
int c = 2018 % 50; // c = 18
36
Example
For example, Let 𝑦=2020, then:
Therefore, Mid-Autumn Festival of year 2020 is
around 38 days after 24 Aug. That is, 1 Oct. Also,
((𝑗 + 𝑝) mod 7) + 1 = ((2459086 + 38) mod 7) + 1 =
(2459124 mod 7) + 1 = 3 + 1 = 4. So, 1
Oct 2020 is Thursday.
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 [Link]. Do not use other
file names. Submit the file in Blackboard ([Link]
• 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
Don’t hesitate to ask if you have any question
42