0% found this document useful (0 votes)
17 views

CS2311-Lec01-intro

CS2311 Computer Programming at City University of Hong Kong covers fundamental concepts and techniques in computer programming, with lectures led by Dr. Huang and lab sessions by other instructors. The course includes assessments such as assignments, a midterm quiz, and a final exam, with strict policies against plagiarism and the use of GenAI for programming tasks. Students will learn about programming languages, logic flow, and properties of a program, with a focus on C/C++ as the primary language for the course.

Uploaded by

chuigary70866
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

CS2311-Lec01-intro

CS2311 Computer Programming at City University of Hong Kong covers fundamental concepts and techniques in computer programming, with lectures led by Dr. Huang and lab sessions by other instructors. The course includes assessments such as assignments, a midterm quiz, and a final exam, with strict policies against plagiarism and the use of GenAI for programming tasks. Students will learn about programming languages, logic flow, and properties of a program, with a focus on C/C++ as the primary language for the course.

Uploaded by

chuigary70866
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 69

CS2311 Computer Programming

LT01: Introduction

Computer Science, City University of Hong Kong


Semester A 2023-24
About the Course
• Lectures (concepts and techniques)
• Dr. HUANG Heqing ([email protected])
• LT 17, Monday 12:00-14:50
• Zoom link and record will be available on Canvas

• 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

• Midterm quiz (20%)


• 14 Oct. 2024 (week 7), covers lecture 1~6

• Final exam (50%)


• To pass the course you must obtain at least 30% of the final exam marks
(No. 1 reason to fail this course)

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

• A computer program in 1940’s is an


interconnection of arithmetic machines, which
require significant wiring

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

V_IN V_OUT V_IN V_OUT


A B Q A B Q
0 0 0 0 0 0
1 0 0 1 0 1
A AND B 0 1 0 A XOR B 0 1 1
1 1 1 1 1 0

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

• Implementing more complex arithmetic is similar


• e.g., multiplier …
16
Stored Program Computer
• Main Memory: stores both data and program, CPU
i.e., a list of instructions Control Unit
• 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

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

Example: a 32-bit Add Immediate instruction

001000 00001 00010 0000000101011110


OPCode Addr 1 Addr 2 Immediate value
addi $r1 $r2 350

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

Machine Language Symbolic Language


Language directly English-like abbreviations
understood by the representing elementary
computer computer operations
Defined by ISA

x86, RISC … Assembly language

26
Programming Languages
• Symbolic language uses symbols to represent the various
machine language instructions
Example: a 32-bit Add Immediate instruction

001000 00001 00010 0000000101011110


OPCode Addr 1 Addr 2 Immediate value

addi $r1 $r2 350

27
PROGRAM 1-2 The Multiplication Program in Symbolic Language

28
Programming Languages

Machine Language Symbolic Language High-level Language


Language directly English-like abbreviations Close to human language.
understood by the representing elementary Example: a = a + b
computer computer operations [add vales of a and b, and
Defined by ISA store the result in a,
replacing the previous
value]

x86, RISC … Assembly language C/C++, Java, Python


29
PROGRAM 1-3 The Multiplication Program in C

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

• Cons: need to re-compile a program before


you execute it on a different hardware
0010111010 0010111010
1010001011 1010001011
• Pros: fast
Machine code Machine code
for A for B
33
High-level
Interpreted Languages print (“hello!”); programming
language

• Programs are first compiled to an Compiler


intermediate representation (IR), which is
Intermediate
then converted by a Virtual Machine (VM) to 00101110101010001 representation
machine code and executed on the hardware
VM for VM for
• E.g., Java, Python… machine A machine B

• Pros: better portability


0010111010 0010111010
1010001011 1010001011
• Cons: slow
Machine code Machine code
for A for B
34
For CS2311: C/C++
• Created in 1970s by Dennis Ritchie in Bell Labs
• Found lasting use in
• Supercomputers, microcontrollers, embedded systems
• Operating systems, device drivers, network stacks
• Many programming languages are based on/influenced by C/C++
• Go, Java, JavaScript …
• Easy to move from C++ to other languages
• But often not in the other direction
• Cons: requires explicit low-level manipulation
• Pros: very efficient
35
Outline for Today
• What’s a computer
• Programming languages
• Basics of a program
• Basics of programming
• Hello World

36
Elements of a Program (External View)
• Input
• Process
• Output

Input Process Output

37
Elements of a Program (Internal View)

• A list of instructions that implement Logic Flow


a logic flow

• Usually involve data access Computer


Program

Instructions Data

38
Logic Flow
• The logic of problem solving

• E.g. Calculate BMI (Body Mass Index)


1. Read weight from keyboard
2. Read height from keyboard
3. Weight x weight/height
4. Write BMI to screen

• You may implement the same logic flow using different languages

39
Logic Flow Examples

Read temperature sensor

no
Temperature < 25oC ? Cooling

yes
Heating

40
Logic Flow Examples
• Find the maximum number in 2, 1, 3, Ø
max = 2

read the next number in the sequence,


and write the value to x

x > max ? x = Ø? x <= max?

max = x Output
max
41
Logic Flow Examples
• Sort 8, 5, 2, 9, 4 in descending order

8, 5, 2, 9, 4

Find the maximum number

Output that number

42
Logic Flow Examples
• Sort 8, 5, 2, 9, 4 in descending order

8, 5, 2, 9, 4

Find the maximum number

Output that number

9,
43
Logic Flow Examples
• Sort 8, 5, 2, 9, 4 in descending order

8, 5, 2, 4

Find the maximum number

Output that number

9, 8,
44
Logic Flow Examples
• Sort 8, 5, 2, 9, 4 in descending order

5, 2, 4

Find the maximum number

Output that number

9, 8, 5,
45
Logic Flow Examples
• Sort 8, 5, 2, 9, 4 in descending order

2, 4

Find the maximum number

Output that number

9, 8, 5, 4,
46
Logic Flow Examples
• Sort 8, 5, 2, 9, 4 in descending order

2,

Find the maximum number

Output that number

9, 8, 5, 4, 2
47
Properties of a Program
• Reliability: how often the results of a program are correct

• Robustness: how well a program anticipates problems (e.g., incorrect or


corrupted data, unavailability of resources)

48
Properties of a Program
• Efficiency: Measure of system resources a program consumes (e.g.,
processor time, memory space)

• Portability: the range of computer hardware and operating


system platforms on which the source code of a program can
be compiled/interpreted and run.

49
Properties of a Program
• Readability: the ease with which a human reader can comprehend the
purpose, control flow, and operation of source code.

• Maintainability: the ease with which a program can be modified by its


present or future developers in order to make improvements or to
customize, fix bugs and security holes, or adapt it to new environments.

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 …

• IDE: integrated development environment


• Integrate compiler, build tools, and debuggers
• With syntax highlighting
• Popular IDEs
• Visual Studio, Visual Studio Code, JetBrains
Clion, Apple Xcode …

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;

• Comments improves readability of void main()


source code {
cout << "Hello world!” << endl;
• Will NOT be compiled into machine }
code

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)

• Programming languages (machine, symbolic, high-level, compiler)

• Elements of a program (input/output + process, instr + logic flow + data)

• Properties of a program (reliability, robustness, efficiency, portability,


readability, maintainability)

• Steps of programming (coding + compile + link)

• The Hello World program

69

You might also like