0% found this document useful (0 votes)
7 views38 pages

Fundamentals of Programming Overview

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)
7 views38 pages

Fundamentals of Programming Overview

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

CSC10012 –

FUNDAMENTAL OF
PROGRAMMING
Week 1 – Programming
Overview

Lecturer: Nguyễn Hải Minh - 10/2024


Contents
1. Computer programming
2. Computer algorithm

2 Fundamentals of Programming - NMHuy, NHMinh


1. COMPUTER PROGRAMMING
• Concepts
• Programmer
• Instruction Code
• Programming Cycle
• Programming Language
• Programming Environment 3
Computer programming
 Programming concepts:
◼ Problem:
 Teach a kid to find sum of max and min among 7, 1, 9.
 Rules:
◼ The kid knows addition and comparison between two
numbers.
◼ Write solution in step-by-step.
◼ Programming ~ teaching:
 From what computer knows.
 Step-by-step.

Computer Programming Program


4 Fundamentals of Programming - NMHuy, NHMinh
Computer programming
 Programming concepts:
◼ Computer has a set of hard-wired instructions.
◼ Programming: write instructions for computer.
◼ Computer program:
 Written organized instructions.
 Follow step-by-step.
 To solve problem.

5 Fundamentals of Programming - NMHuy, NHMinh


Computer programming
 Programmer:
◼ Person who writes the program.
◼ Programmer, coder, developer.
◼ To become a good programmer:
 Logical thinking.
 Analytical and careful.
 Hands-on and explore.
◼ The first programmer?

Ada Lovelace
6 Fundamentals of Programming - NMHuy, NHMinh
Programming is a unique mix of
creativity and logic. It’s not just about
writing code – it’s about solving
problems and exploring new
possibilities.

7
Computer programming
 Instruction code:
◼ Machine code:
 Binary sequence of ‘0’ and ‘1’.
 Computer can execute directly.
◼ Pseudo-code:
 Natural language. Step 1: add a, b.
Step 2: compare a, c.
 Human can understand. Step 3: output a.

◼ Source code:
 Intermediate language.
 Easy to read, not too vague.
 Can be translated to machine code.
◼ Compiler vs. Interpreter.
8
◼ Who wrote the first compiler?
Computer programming
 Programming cycle:
[Link]: write source code.
[Link]: source code → machine code.
[Link]: execute machine code to perform tasks.
[Link]: review source code to fix errors.
➔ Debug takes the most time!

Edit / Debug Compile Run

9 Fundamentals of Programming - NMHuy, NHMinh


Computer programming
 Programming language:
◼ Intermediate language between human and
computer.
◼ Concise and precise.
◼ There are thousands of programming languages!
◼ Learning programming thinking is more important
than learning programming language.

10 Fundamentals of Programming - NMHuy, NHMinh


Computer programming
 Programming language:
◼ Low-level language:
 1st generation: machine code.
 2nd generation: assembly language.
◼ High-level language:
 Procedural: Fortran, Pascal, C.
 Functional: Lisp, Haskell, F#.
 Object oriented: C++, Java, C#.
◼ Trends:
 Scripting languages: JavaScript, Python.
 AI prompts: ask AI to generate source code.
11 Fundamentals of Programming - NMHuy, NHMinh
Computer programming
 Programming environment:
◼ A platform of tools supporting programming tasks.
◼ Source code editor:
 Edit text, syntax highlight, code completion.
◼ Compiler:
 Translate source code to machine code.
◼ Terminal:
 Command-line tool to run program and shell
commands.
◼ Debugger:
 Run program step-by-step.
12
 Monitor computer memory.
Computer programming
 Programming environment:
◼ IDE – Integrated Development Environment:
 A single application combining programming tools all-
in-one.
 User friendly, configuration-free, add-in features.
 Cons:
◼ Heavy memory and storage usage.
◼ Limit configuration skill.
◼ Require license.
◼ Track user data.

13 Fundamentals of Programming - NMHuy, NHMinh


Computer programming
 C/C++ programming environment:
Tools Windows MacOS Linux
Editor Notepad++ TextMate Vim, Emacs
VS Code (*) VS Code (*) VS Code (*)
Sublime Text (*) Sublime Text (*) Sublime Text (*)
Compiler MSVC (*)
GCC (+) Clang GCC
Clang (+) GCC Clang
Debugger MSVC (*) LLDB GDB
GDB GDB LLDB
IDE Visual Studio (*) Xcode (*)
Code::Blocks Code::Blocks Code::Blocks
CodeLite CodeLite CodeLite
(*): Proprietary software and track user data.
(+): Run on Linux simulated environment like MinGW64, WSL.

14 Fundamentals of Programming - NMHuy, NHMinh


Computer programming
 C/C++ programming environment:
◼ Set up loosely-coupled environment:
 Windows: MinGW64, GCC, GDB.
◼ Project WinLibs: light-weight, not extensible,
[Link]
◼ Project MSYS2: get MSYS2 and toolchain,
[Link]
 Linux (Ubuntu/Debian): GCC, GDB.
◼ Install package build-essential.
 MacOS: Clang, LLDB.
◼ Terminal command: xcode-select --install.

15 Fundamentals of Programming - NMHuy, NHMinh


Computer programming
 C/C++ programming environment:
◼ Set up IDE:
 Visual Studio (Windows):
[Link]
 Xcode (MacOS): xcode-select --install, then Get Xcode.
 Code::Blocks: [Link]
 CodeLite: [Link]

16 Fundamentals of Programming - NMHuy, NHMinh


2. COMPUTER ALGORITHM
• Concepts
• Basic Instructions
• Representations
• Efficiency

Fundamentals of Programming - 17
NMHuy, NHMinh
Computer algorithm
 Algorithm concepts:
◼ Different machines have different instruction code.
◼ Turing machine: abstract machine and instructions.
◼ Algorithm ~ program on Turing machine:
 Finite steps to solve problem on Turing machine.
 Does not depend on specific machine.
 Is more abstract than program.

Alan Turing
18 Fundamentals of Programming - NMHuy, NHMinh
Computer algorithm
 Solving Problem with algorithm:
◼ Ignore the details of your programming language
(C/C++, Python, Java, …) when trying to solve a
problem.
◼ Instead, figure out the steps you need to go thru to
solve the problem
◼ Write these steps down in English
◼ These steps are called the algorithm!

Fundamentals of Programming - NMHuy, NHMinh 19


Computer algorithm
 Solving Problem with algorithm:

Fundamentals of Programming - NMHuy, NHMinh 20


Computer algorithm
 Example: Convert inches to millimeters
◼ First understand the problem
 where do the inches come from (the user)
 what is the math needed for the conversion
◼ mm = 25.4 times inches
 how do we want to display the results
◼ 2in convert to 50.8mm

Fundamentals of Programming - NMHuy, NHMinh 21


Computer algorithm
 Example: Convert inches to millimeters
◼ Next, write the algorithm
◼ Step 1: Welcome the user
 tell them what to expect
 tell them the purpose of the program
◼ Step 2:
 Get the number of inches from the user
◼ display a prompt asking the user to enter
◼ read in the number of inches
◼ display what was read (echo)
◼ ask the user if this is really correct (confirm)
◼ if not, repeat this step until the user is satisfied
Fundamentals of Programming - NMHuy, NHMinh 22
Computer algorithm
 Example: Convert inches to millimeters
◼ Next, write the algorithm
◼ Continuing with Steps 3 and 4:
 Convert the number of inches to mm
◼ mm = 25.4 times inches
 Display the results
 Provide a sign-off message

Fundamentals of Programming - NMHuy, NHMinh 23


Computer algorithm
 Algorithm basic instructions:
◼ Read input, write output.
◼ Arithmetic operations: +, -, *, /, =.
◼ Comparisons: >, <, >=, <=, ==, !=.
◼ Condition: if-else.
◼ Loop: while.
 Can machines think?
◼ The Imitation game - Turing test.

24 Fundamentals of Programming - NMHuy, NHMinh


Computer algorithm
 Algorithm representations:
◼ Pseudo-code:
 Use natural language.
 Notations:
Instructions Notations Example
Read input Input -Step 1: Input a, b, c.
Write output Output -Step 2: Output a + b + c.
-Step 1: a = 5.
Arithmetic operations +, -, *, /, %, =
-Step 2: b = (a + 5) / (a – 3).
-Step 1: Input a.
If <condition> …
Condition -Step 2: If a < 0 Output “Negative”
Else …
Else Output “Positive”.
-Step 1: a = 0.
-Step 2: While a < 10
Loop While <condition> ...
Output a.
a = a + 1.
25 Fundamentals of Programming - NMHuy, NHMinh
Computer algorithm
 Algorithm representations:
◼ Pseudo-code:
* Find max among 3 numbers: * Compute factorial of N:
-Step 1: Input a, b, c. -Step 1: Input N.
-Step 2: max = a. -Step 2: S = 1.
-Step 3: If b > max -Step 3: While N > 1
max = b. S = S * N.
-Step 4: If c > max N = N – 1.
max = c. -Step 4: Output S.
-Step 5: Output max.

26 Fundamentals of Programming - NMHuy, NHMinh


Computer algorithm
 Algorithm representations:
◼ Flowchart:
 Use diagram.
 Visualize flow of process.
 Notations:
Intrustions Notations Example
Read input
a, b, c x, y
Write output

Arithmetic operations a=a+b

Condition a<0

Transition
27 Fundamentals of Programming - NMHuy, NHMinh
Computer algorithm
 Algorithm representations:

a, b, c N

max = a S=1

true false
b > max max = b N>1
false true
true S=S*N
c > max max = c
false
N=N-1
max

28 Fundamentals of Programming - NMHuy, NHMinh


Computer algorithm
 Algorithm efficiency:
◼ Complexity:
 Algorithm performance varies with different inputs.
 Big-O notation: 𝑂(), growth rate.
 Time complexity: input ~ instructions.
 Space complexity: input ~ memory.
 Classes of complexity:
◼ Constant: 𝑂(1).
◼ Logarithmic: 𝑂(log(𝑛)).
◼ Linear: 𝑂(𝑛).
◼ Polynomial: 𝑂(𝑛2 ), 𝑂(𝑛3 ), …
◼ Exponential: 𝑂(2𝑛 ).
◼ Factorial: 𝑂(𝑛!).
29 Fundamentals of Programming - NMHuy, NHMinh
Computer algorithm
 Algorithm efficiency:
◼ Popular algorithm complexities:
Algorithm Complexity Form
Finding max (3 numbers) 𝑂(1) Constant
Binary Search(N elements) 𝑂(log(𝑛)) Logarithmic
Factorial N! 𝑂(𝑛) Linear
Sorting Bubblesort: 𝑂(𝑛2)
Polynomial
(N elements) Quicksort: 𝑂(𝑛log(𝑛))
Traveling Salesman 𝑂 (𝐾𝑛 ) Exponential
(N city) 𝑂(𝑛!) Factorial

◼ Simplicity:
 Easier to understand and implement.
 Reduce cost in debugging and maintenance.
30 Fundamentals of Programming - NMHuy, NHMinh
Fundamentals of Programming - NMHuy, NHMinh 31
Practice
Practice 1.1:
◼ Write an algorithm to tell between 2 input number a
and b, which one is bigger or they are equal.

32 Fundamentals of Programming - NMHuy, NHMinh


Practice
Practice 1.2:
◼ Input a and b, write an algorithm to find x which
satisfies ax + b = 0

33 Fundamentals of Programming - NMHuy, NHMinh


Practice
Practice 1.3:
◼ Input a≠0, b, and c, write an algorithm to find x
which satisfies ax2 + bx + c = 0

34 Fundamentals of Programming - NMHuy, NHMinh


Summary
 Computer programming:
◼ Write instructions for computer to solve problem.
◼ Programming cycle: edit, compile, run, debug.
◼ Programming language:
 Intermediate language to communicate with computer.
 Low-level vs high-level.
 Trends: scripting languages, AI prompts.
◼ Programming environment:
 Platform of tools supporting programming tasks.
 Editor, compiler, terminal, debugger.
 IDE: Integrated Development Environment.
35 Fundamentals of Programming - NMHuy, NHMinh
Summary
 Computer algorithm:
◼ Finite steps to solve a problem on Turing machine.
◼ Turing machine: abstract machine and instructions.
◼ Basic instructions:
 Input, output.
 Arithmetic operations, comparisons.
 Condition, loop.
◼ Representations: pseudo-code, flowchart.
◼ Efficiency:
 Complexity: Big-O notation, growth rate.
 Simplicity: easy to understand or implement.
36 Fundamentals of Programming - NMHuy, NHMinh
What’s Next?
 Input & Output:
◼ C/C++ Overview
◼ Basic units: variables, constants, data types,
expressions.
◼ I/O Stream
◼ Text file
 Homework:
◼ Submit your handwriting homework 1 next week.
The questions are available on Moodle.

Fundamentals of Programming - NMHuy, NHMinh 37


Fundamentals of Programming - NMHuy, NHMinh 38

You might also like