CS2311-Lec01-intro
CS2311-Lec01-intro
LT01: Introduction
• Labs (practices)
• LI 4109, Tue 15:00-15:50, DONG Wentao ([email protected])
• LI 4208, Tue 16:00-16:50, CHEN Yufei ([email protected])
• LI 4400, Tue 17:00-17:50, HUANG Heqing ([email protected])
2
Resources
• On Canvas: lecture & lab ppt + example programs
• Example programs will be labelled with difficulty
levels including S (most difficult, for A+), A, B, to C
(easiest, for pass)
• Reference books
• Computer Systems: A Programmer’s Perspective
• Microsoft Visual Studio 2022 (Windows)
• Develop environment for compiling & debugging
• PASS (program assignment assessment system)
• Labs and practices
3
Assessment
• Assignments (30%)
• Start as early as possible and submit on time
• Debugging takes much longer time than you expect, make a good plan
4
Polices
• Plagiarism
• Can be automatically detected by the PASS system
• Both the giver and copier get punishments
• Punishment ranges from warning to course failure
• May cause you be forced out of CityU
5
Polices
• Students are NOT allowed to use GenAI for programming tasks
• Reason 1: Students need to learn how to write programs without aids first, so
that they obtain good fundamental programming, debugging, and analysis
skills. Relying on GenAI too early will hinder the learning of these necessary
skills.
• Reason 2: 75% of organizations worldwide have implemented or are
considering implementing bans on using GenAI apps in the workplace. This
includes companies like Apple, JPMorgan Chase, Deutsche Bank, Samsung,
Amazon, CityGroup, Bank of America, Accenture. Therefore, students must
be trained without using GenAI aids to be successful in the workplace.
6
Outline for Today
• What’s a computer
• Programming languages
• Basics of a program
• Basics of programming
• Hello World
7
Computers in 1940’s
• A large collection of arithmetic machines
8
From Wiring to Programming
Wiring
Task Diagram
9
From Wiring to Programming
Wiring
Task Diagram
Instructions Command
Task
Memory Controller
10
Stored Program Computer
• A.k.a Von Neumann machine (proposed in 1945)
• How does it work?
11
Logic Gate
• Logic gate to a computer is like a brick to a skyscraper
• It’s a circuit that implements a Boolean function
• Typically implemented using diodes or transistors
12
Arithmetic Circuits
• You can build a simple arithmetic circuits using logic gates
• E.g., a 1-bit adder
0 + 0 = 00
1 + 0 = 01
0 + 1 = 01
1 + 1 = 10
13
Arithmetic Circuits
• You can build a simple arithmetic circuits using logic gates
• E.g., a 1-bit adder
0 + 0 = 00 A
Input
B
Output
C S
XOR
1 + 0 = 01 0 0 0 0
0 + 1 = 01 1 0 0 1
0 1 0 1
1 + 1 = 10 1 1 1 0 AND
14
Arithmetic Circuits
• You can build a simple arithmetic circuits using logic gates
• E.g., a 1-bit adder
0 + 0 = 00 A
Input
B
Output
C S
1 + 0 = 01 0 0 0 0
0 + 1 = 01 1 0 0 1
0 1 0 1
1 + 1 = 10 1 1 1 0
15
Arithmetic Circuits
• You can build a simple arithmetic circuits using logic gates
• E.g., a 1-bit adder
0 + 0 = 00 A
Input
B
Output
C S
1 + 0 = 01 0 0 0 0
0 + 1 = 01 1 0 0 1
0 1 0 1
1 + 1 = 10 1 1 1 0
17
Stored Program Computer
• Main Memory: stores both data and program, CPU
i.e., a list of instructions Control Unit
Output
Input
• CPU (Central Processing Unit): Arithmetic/Logic
• ALU: performs arithmetic and bitwise Unit (ALU)
operations
• Control Unit: read instructions from memory,
direct ALU to execute instructions
Mian Memory
• External storage: (slow) mass storage
• Input/output: keyboard, microphone, display, External Storage
speaker…
18
Instruction
• A sequence of bits that defines a single operation of a CPU
• E.g., addition, subtraction, read, write
• Instruction size can be fixed (e.g., 16-bit, 32-bit, 64-bit) or variable
19
Outline for Today
• What’s a computer
• Programming languages
• Basics of a program
• Basics of programming
• Hello World
20
Why is Software Programming Important?
• It is almost impossible to run an electronic
device without software
• Printers, MRI machines, disk drives, remote
controls, vehicle control system, etc.
• More cost effective to implement functionality
in software than hardware
• Software bugs easy to fix
• Systems increasingly complex, bugs unavoidable
• Allows new features to be added later
21
Programming Languages
• Computer program
• A sequence of instructions for a computer to execute
• Programming language
• Notation for writing computer programs
22
Different Programming Languages
23
Programming Languages
Machine Language
Language directly
understood by the
computer.
Defined by ISA
x86, RISC …
24
PROGRAM 1-1 The Multiplication Program in Machine Language
25
Programming Languages
26
Programming Languages
• Symbolic language uses symbols to represent the various
machine language instructions
Example: a 32-bit Add Immediate instruction
27
PROGRAM 1-2 The Multiplication Program in Symbolic Language
28
Programming Languages
30
Compiler
• Computers only understand machine language (binary code)
• Human write programs using high-level programming language
?
31
Compiler
• Computers only understand machine language (binary code)
• Human write programs using high-level programming language
• Need a compiler to translate programs written in high-level
programming language to binary code
32
Compiled Languages
High-level
printf (“hello!”); programming
• Programs are compiled to binary machine language
code (for a specific hardware), which is then
directly executed by the hardware
Compiler Compiler
• E.g., C/C++, Rust, Pascal … for machine A for machine B
36
Elements of a Program (External View)
• Input
• Process
• Output
37
Elements of a Program (Internal View)
Instructions Data
38
Logic Flow
• The logic of problem solving
• You may implement the same logic flow using different languages
39
Logic Flow Examples
no
Temperature < 25oC ? Cooling
yes
Heating
40
Logic Flow Examples
• Find the maximum number in 2, 1, 3, Ø
max = 2
max = x Output
max
41
Logic Flow Examples
• Sort 8, 5, 2, 9, 4 in descending order
8, 5, 2, 9, 4
42
Logic Flow Examples
• Sort 8, 5, 2, 9, 4 in descending order
8, 5, 2, 9, 4
9,
43
Logic Flow Examples
• Sort 8, 5, 2, 9, 4 in descending order
8, 5, 2, 4
9, 8,
44
Logic Flow Examples
• Sort 8, 5, 2, 9, 4 in descending order
5, 2, 4
9, 8, 5,
45
Logic Flow Examples
• Sort 8, 5, 2, 9, 4 in descending order
2, 4
9, 8, 5, 4,
46
Logic Flow Examples
• Sort 8, 5, 2, 9, 4 in descending order
2,
9, 8, 5, 4, 2
47
Properties of a Program
• Reliability: how often the results of a program are correct
48
Properties of a Program
• Efficiency: Measure of system resources a program consumes (e.g.,
processor time, memory space)
49
Properties of a Program
• Readability: the ease with which a human reader can comprehend the
purpose, control flow, and operation of source code.
50
Outline for Today
• What’s a computer
• Programming languages
• Basics of a program
• Basics of programming
• Hello World
51
Steps of C++ Programming
1. Coding
• Write your source code into a file
• e.g., “hello.cpp”
• You can use different editors or IDEs
52
Editors and IDEs
• Editor: simply where you write your codes
• Vim, Emacs, nano, vi, or even notepad …
53
Steps of C++ Programming
1. Coding
• Write your source code into a file
• e.g., “hello.cpp”
• You can use different editors or IDEs
2. Compile your source code
• Check grammatical rules (syntax)
• Source code is converted to object code in machine language
• e.g., “hello.obj”
54
C++ Compilers
• MSVC (Microsoft Visual C++)
• Microsoft’s compiler for their custom implementation of the C++ standard,
known as Visual C++
• GCC/g++
• Mainly targets Unix-like platforms
• Windows support is provided through the Cygwin or MinGW runtime libraries
• Clang
• Strict adherence to C++ standards
• Minimal modification to source code’s structure during compilation
• GCC-compatible or MSVC-compatible through compiler drivers
Steps of C++ Programming
1. Coding
• Write your source code into a file
• e.g., “hello.cpp”
• You can use different editors or IDEs
2. Compile your source code
• Check grammatical rules (syntax)
• Source code is converted to object code in machine language
• e.g., “hello.obj”
3. Link
• Combines objects and libraries to create a binary executable
• e.g., “hello.exe”
56
Outline for Today
• What’s a computer
• Programming languages
• Basics of a program
• Basics of programming
• Hello World
57
An Example Program
/* The traditional first program in honor of Dennis Ritchie who
invented C at Bell Labs in 1972 */
#include <iostream>
using namespace std;
void main()
{
cout << "Hello world!” << endl;
}
58
#include /* The traditional first program in
• Include the libraries you want to use honor of Dennis Ritchie who
Syntax: #include <library name> invented C at Bell Labs in 1972 */
• A library is a collection of prewritten code that #include <iostream>
programmers can use for their tasks using namespace std;
• Libraries typically include definitions for
• Commonly used algorithms (e.g., sorting) void main()
• Data structures (e.g., lists, trees, hash tables) {
• Commonly used constants and functions cout << "Hello world!” << endl;
• e.g., M_PI, cos, sin in the cmath library }
• Input/output
• For example, iostream is the library for commonly
used i/o functions, including cout
59
Function /* The traditional first program in
honor of Dennis Ritchie who
• A sequence of instructions grouped
invented C at Bell Labs in 1972 */
together (contained within braces { and }),
which implement a specific task #include <iostream>
using namespace std;
Syntax:
void main()
ReturnType FunctionName (input parameters) {
{ cout << "Hello world!” << endl;
instructions within function body }
}
60
void main() /* The traditional first program in
honor of Dennis Ritchie who
• main function is the entry point of a C++
invented C at Bell Labs in 1972 */
program
• Every C++ program must have a main #include <iostream>
• Note: C/C++ is case sensitive using namespace std;
• E.g., Main() or MAIN() is incorrect
void main()
{
• void means there is NO return value cout << "Hello world!” << endl;
}
61
Statement /* The traditional first program in
honor of Dennis Ritchie who
• A syntactic unit that expresses some action invented C at Bell Labs in 1972 */
to be carried out
#include <iostream>
using namespace std;
• Ended with a semicolon “;”
void main()
{
cout << "Hello world!” << endl;
}
62
cout /* The traditional first program in
• cout: “Console OUTput” allows our honor of Dennis Ritchie who
invented C at Bell Labs in 1972 */
program to output values to the standard
output stream (the screen) #include <iostream>
• <<: output operator, which output values to using namespace std;
an output device
• The right-hand side of the << (i.e., Hello void main()
{
world! between a pair of double quotes) is cout << "Hello world!” << endl;
the string to output }
• endl: end of the line. advance the cursor on
the screen to the beginning of the next line
63
using namespace /* The traditional first program in
• namespace: a declarative region that honor of Dennis Ritchie who
provides a scope to the identifiers invented C at Bell Labs in 1972 */
(the names of types, functions, #include <iostream>
variables, etc) inside it
using namespace std;
• Declare namespace to avoid writing
the full name
void main()
Syntax: using namespace xxx {
• For example cout << "Hello world!” << endl;
}
Standard (std) namespace is used
such that we can write cout instead of
std::cout
64
Comments /* The traditional first program in
honor of Dennis Ritchie who
• Enclosed by “/*” and “*/” invented C at Bell Labs in 1972 */
• Or begin with “//” #include <iostream>
• Single line comments using namespace std;
65
The texts to
Syntax errors output should be
placed in a pair
/* The traditional first program in
of double quotes
honor of Dennis Ritchie who
invented C at Bell Labs in 1972 */ “ texts”.
#include <iostream>
using namespace std;
void main()
{
cout < Hello world! < endl
}
66
The texts to
Syntax errors output should be
placed in a pair
/* The traditional first program in
of double quotes
honor of Dennis Ritchie who
invented C at Bell Labs in 1972 */ “ texts”.
#include <iostream>
using namespace std; < is not an
void main() operator of
{ cout. We need
to use <<
cout < Hello world! < endl
}
67
The texts to
Syntax errors output should be
placed in a pair
/* The traditional first program in
of double quotes
honor of Dennis Ritchie who
invented C at Bell Labs in 1972 */ “ texts”.
#include <iostream>
using namespace std; < is not an
void main() operator of
{ cout. We need
to use <<
cout < Hello world! < endl
} We need ; at the
end of each
statement 68
Summary
• Stored program computer (Von Neumann machine)
69