You are on page 1of 82

EE3490E – Programming

Techniques
Chapter 1: Introduction
Lecturer: Dr. Hoang Duc Chinh (Hoàng Đức Chính)
Department of Automation Engineering
School of Electrical and Electronics Engineering
Email: chinh.hoangduc@hust.edu.vn
© HĐC 2022.1
Content
1.1. Course Description
1.2. Introduction of programming technique
1.3. Programming Paradigms
1.4. Software development process
1.5. Fundamentals of C/C++ languages
1.6. Getting started

Chapter 1: Introduction © HĐC 2022.1 2


1.1. Course Description
 This course provides fundamentals of programming techniques,
implemented with C and C++ programming languages
 Structure programming
 Object oriented programming (OOP)
 Real-time programming and typical data structure in embedded systems
 Generic programming (optional)
 You will also learn the essential knowledge, including
 memory management, pointers, preprocessor macros,
 and how to find bugs when you inevitably use any of those incorrectly.
 Why C/C++:
 Strong and popular languages, which enables programmer to implement
most important techniques
 Essential in electrical and electronic systems

Chapter 1: Introduction © HĐC 2022.1 3


Targeted platforms

Chapter 1: Introduction © HĐC 2022.1 4


What to expect?

Chapter 1: Introduction © HĐC 2022.1 5


What to expect?

Chapter 1: Introduction © HĐC 2022.1 6


What to expect?

Chapter 1: Introduction © HĐC 2022.1 7


What to expect?
 Solid background is important, especially
 Thinking and Methodology
 Adaptation to different applications
 Adaptation to various programming languages: Java, Python, C#,
MATLAB, etc.
 Focus on professionality in programming: effectiveness and quality
 The following topics may be mentioned but not included in the
main scope of this course:
 Low-level system programming
 Graphics programing
 I/O interface
 Database
 Communication and Networking
 There will be weekly assignments and an individual mini-project
Chapter 1: Introduction © HĐC 2022.1 8
Syllabus
 Chapter 1 – Introduction 1 week
 Chapter 2 – Basic of C & C++ 3 weeks
Assignments
 Chapter 3 – Functions & Libraries 3.5 weeks
Mini project assigned
Mid-term exams tentative schedule (Thi giữa kỳ dự kiến) Week 10th
 Chapter 4 – Data structures & Algorithms 2.5 weeks
Assignments
 Chapter 5 – Program design & efficiency 1.5 weeks
Mini project submision
 Chapter 6 & 7 – Class & Objects 1 weeks
 Chapter 8 – Class relationship 1 weeks
Assignments
 Revision 0.5 weeks

Chapter 1: Introduction © HĐC 2022.1 9


Evaluations
 Progress assessment: 30%
 Weekly exercises/ Quizzes (Progress scores)
 Mini project
 Mid-term exams
 Final exams: 70%
 Mini project

Chapter 1: Introduction © HĐC 2022.1 10


Evaluations
 Mid-term and final exams are in written format with the
duration of an hour, they includes
 Theoretical questions
 Evaluate program results
 Write programs to solve specific problems
 Mini project:
 Good performance: bonus points
 Late submission: 0 bonus point
 NO submission: -2 in progress assessment

Chapter 1: Introduction © HĐC 2022.1 11


Learning methodologies
 Method 1: Attend lectures self-attempt read
material discussion practice
 Method 2: Read material self-attempt attend
lectures discussion practice
 Basic principle: Active learning frequently!
 Do not:
 Write too much in the class
 Learn by heart
 Rely on revision only
 Learn the examples in the books solely

Chapter 1: Introduction © HĐC 2022.1 12


The tools
 A computer: PC or Laptop or Single Board Computer
 Editors: Visual Studio Code (must use in this course),
Notepads++, Emacs, Sublime, etc.
 Compilers: gcc, g++
 Debugger: gdb
 Operating systems and Packages:
 Windows: Mingw-w64 (recommended), Cygwin
 Linux (Ubuntu) and MacOS: gcc, g++ and gdb are usually pre-
installed as default packages
 An Integrated Development Environment such as Visual
Studio, Code Blocks, Dev-C++, etc. can be used but not
recommended.
Chapter 1: Introduction © HĐC 2022.1 13
References
 English:
[1] Brian Kernighan B.W., Ritchie D.M., The C
Programming Language, 2nd edition, Prentice
Hall, 1998
[2] Bjarne Stroustrup, The C++ Programming
Language, 4th Edition
[3] Stanley B. Lippman et. al, C++ Primer, 5th
Edition
[4] Scott Meyers, Effective C++, 3rd Edition
[*] Many other books and resources
 Tiếng Việt:
[5] Phạm Văn Ất et. al., Giáo Trình Kỹ Thuật Lập
Trình C Căn Bản và Nâng Cao
[6] Phạm Văn Ất et. al., Giáo Trình C++ Và Lập
Trình Hướng Đối Tượng
[7] Hoàng Minh Sơn, Bài giảng Kĩ thuật lập trình,
ĐH Bách khoa Hà Nội
Chapter 1: Introduction © HĐC 2022.1 14
Contact
 Our team:
 Assoc. Prof. Nguyễn Hồng Quang
 Dr. Hoàng Đức Chính
 Dr. Nguyễn Trí Cường
 Department of Automation Engineering, School of
Electrical and Electronics Engineering
 Slides (bài giảng), assignments & discussion: Microsoft
Teams (Team code: xem QLĐT)

Chapter 1: Introduction © HĐC 2022.1 15


1.2. Introduction of programming
technique
 Programming technique: The technique is used to
implement a software solution (data structure and
algorithm) based on a methodology and one or a few
programming languages suitable with the requirements
of a specific application.

 Programming technique Analysis & Design

Chapter 1: Introduction © HĐC 2022.1 16


Some definitions
 A programming paradigm is a style, or “way,” of
programming. Some languages make it easy to write in some
paradigms but not others.
 Programming technique relates to algorithms/ideas to solve a
class of respected problems, e.g.: “Divide and conquer” or
“program development by stepwise refinement”
 Programming style is a set of guidelines used to format
programming instructions. It is useful to follow a style as it
makes it easier for programmers to understand the code, maintain
it, and assists in reducing the likelihood of introducing errors.
 Programming culture
 Programming activities, usually relates to programming languages
 A combination of programming model, style and techniques
 Programming ethics as well as the way software is used

Chapter 1: Introduction © HĐC 2022.1 17


History
1960s
21st Century
1940s Stronger 1980s C++ 1st
Blooming era of
1st computers Computational appeared
Internet
Languages
Machine Code Languages facilitate Internet
Cobol, Lisp, Algol 60, Basic, applications including Web,
PL/1 IoT, Big Data, etc.

Simplicity – Object oriented,


Functional Programming

Interchangeability
Structured programming,
Modular Programming &
Information Hiding
Pascal, Algo 68 Programs run faster
Assembler, Autocodes, 1st New languages support parallel
version of Fortran computing

1950s 1970s “Software 1990s


Early Programming Crisis” & 1st version Parallel & distributed
Languages of C computing

Chapter 1: Introduction © HĐC 2022.1 18


Software crisis
 Software crisis included various issues when developing
software in 1960s, especially with large software system based
on conventional techniques at that time.
 Consequences:
1. Time and price increased tremendously
2. Low productivity
3. Low quality
 It resulted in appearance of Software Engineering

Chapter 1: Introduction © HĐC 2022.1 19


What is programming?

Write a program to Write a function to


calculate factorial of calculate the
100! factorial!

Write a program to Write a program to


print the first 100 print the first N
prime numbers! prime numbers!

This is not proper This is programming!


programming!

Chapter 1: Introduction © HĐC 2022.1 20


A good program
 Provide the right solution accepted by customers
 Reliability
 Proper execution
 Less errors in quantity and frequency of occurrence
 Non-severe errors
 Efficiency
 Compact programs, low memory utilization
 Fast run, low CPU usage
 Effectiveness
 Short development time
 Easy to maintain
 Highly usable
 User friendly
 Rich features

Chapter 1: Introduction © HĐC 2022.1 21


Examples
 Write a program to calculate the factorial of a positive integer n
 The function
int factorial (int N);
 Algorithm:
 Recursive method:
if (N > 1)
return N*factorial(N-1);
return 1;

 Iterative method
int kq = 1;
while (N > 1)
kq *= N--;
return kq;
Chapter 1: Introduction © HĐC 2022.1 22
How to program well?
 Learn the way of thinking and programming methodologies
 Mathematical thinking, logical thinking, structure thinking, OOP thinking
and generic thinking
 Exploring data structures and algorithms
 Understand the computer
 Interaction amongst CPU, Programs and Memory
 Memory management
 Mastering the programming language
 Understand the strengths and limits of the language
 Programming skill
 Self-practicing
 Understand the above aspects
 Learn from professional developers’ codes (but do not copy blindly)
 Keep practicing and be creative

Chapter 1: Introduction © HĐC 2022.1 23


Supreme principle

“Keep it simple :
as simple as possible,
but no simpler”
- Albert Einstein

Chapter 1: Introduction © HĐC 2022.1 24


Programming in Electrical and
Electronic Engineering Applications
 Control applications, e.g. microcontroller, computer,
programmable logic controller, distributed controller system
 Data acquisition and management
 Human Machine Interface
 System integration
 Design and calculation software
 Simulation software
 Optimization
 Communication protocols
 …

Chapter 1: Introduction © HĐC 2022.1 25


1.3. Programming Paradigms
 Sequential programming
 Structured programming
 Modular programming
 Object-oriented programming
 Generic programming
 Component-based programming
 Real-time programming

Chapter 1: Introduction © HĐC 2022.1 26


Sequential programming
 The most classical method, listing sequential
commands, low abstraction
 The operation flow is controlled by performing jumping
instruction, or including subroutines
 Examples:
 Machine code
 ASSEMBLY
 BASIC
 IL (Instruction List), STL (Statement List)
 LD, LAD (Ladder Diagram

Chapter 1: Introduction © HĐC 2022.1 27


Sequential programming
 Example: Machine Code
 Only logic operations with 1 and 0 bits
 Used at early stage of computers
 Too complicated and impossible to deal with big problems!!!

23fc 0000 0001 0000 0040


0cb9 0000 000a 0000 0040
6e0c
06b9 0000 0001 0000 0040
60e8

Chapter 1: Introduction © HĐC 2022.1 28


Sequential programming
 Example: Calculating the factorial of a positive integer
n in Assembly
1: MOV AX, n
2: DEC n
3: CMP n,1
4: JMPI
5: MUL AX, n
6: JMP 2
7: MOV n, AX
8: RET
Chapter 1: Introduction © HĐC 2022.1 29
Sequential programming
 Strengths:
 Simple thinking
 Low-level abstraction, easy to manage resources
 May be high efficient
 Appropriate to small problems or in embedded system
 Weakness:
 Difficult to read, thus easy to make mistakes
 Difficult to reuse
 Low effectiveness
 Not suitable to large-scale applications

Chapter 1: Introduction © HĐC 2022.1 30


Structured programming
 Data is structured as data types, and the programs are
also structured to avoid jumping instructions
 Analysis and design with top-down approach
 Implementation with bottom-up approach
 Structured programming use sequential operations,
selection instruction (if then else), loop (while), and
escape (exit)
 Examples:
 PASCAL, ALGO, FORTRAN, C, …
 SFC (Sequential Function Charts)
 ST (Structured Text)
Chapter 1: Introduction © HĐC 2022.1 31
Structured programming
 Example: Calculating the factorial of a positive integer
n in PASCAL

Chapter 1: Introduction © HĐC 2022.1 32


Structured programming
 Example: Student management application

Chapter 1: Introduction © HĐC 2022.1 33


Modular programming
 It is an improvement of Structured Programming. The
program is built with more strict structure, and the unit
of program structure is a module
 Module:
 An independent unit which has been standardized to establish a
system
 Each module includes an interface (exposed) and an
implementation (hidden)
 The modules communicate with each other via their intefaces
 Examples:
 Modula-2, based on PASCAL and designed by Niclaus Wirth
in 1977
Chapter 1: Introduction © HĐC 2022.1 34
Object-oriented programming
 It programming paradigm based on the concept of "objects",
which can contain data and code: data in the form of fields (often
known as attributes or properties), and code, in the form of
procedures (often known as methods).
 3 principles:
 Data encapsulation
 Subtyping/inheritance
 Polymorphism
 Examples:
 C++, C#, Ojective-C
 Java
 Python
 MATLAB
 …

Chapter 1: Introduction © HĐC 2022.1 35


Object-oriented programming
 Example: Student management application

Chapter 1: Introduction © HĐC 2022.1 36


Object-oriented programming
 Example: Matrix calculation in MATLAB

// read the input u

// return output y

Chapter 1: Introduction © HĐC 2022.1 37


Generic programming
 Generalizing whatever possible to introduce a solution
template for a class of specific programs
 Strength:
 Reduce source codes
 Increase the reusability of software
 Can be combined with other programming paradigms
 Interchangeability
 Generalization approaches:
 Data types
 Basic operations
 Data structures
 Memory management, etc.
Chapter 1: Introduction © HĐC 2022.1 38
Generic programming
 Example: Mathematical structures

Chapter 1: Introduction © HĐC 2022.1 39


Component-based programming
 Software development is based on available “IC”
components or created ones
 Evolved from object oriented programming
 Most of Windows and Internet applications are built
with this paradigm
 Examples:
 C/C++, C#
 Delphi, Visual Basic
 Script, HTML, XML, …

Chapter 1: Introduction © HĐC 2022.1 40


Real-time programming
 Software development with real-time responsive to the
system, especially control systems in manufacturing,
robotics, unmanned vehicles, etc.
 Properties:
 Competitive programming (multi-tasking, multi-threading)
 Event driven
 Scheduling mechanism
 Synchronization
 High efficiency
 Languages: ASM, C/C++, ADA, etc
 Needs the support of
 Operating systems
 Hardware platforms
 Communication networks
Chapter 1: Introduction © HĐC 2022.1 41
1.4. Software development process

PROBLEM ? SOLUTION REAL LIFE

Analysis Coding
Testing

ANALYSED DESIGN DESIGNED


MODEL MODEL WORLD

Chapter 1: Introduction © HĐC 2022.1 42


1.4. Software development process

Chapter 1: Introduction © HĐC 2022.1 43


1.4. Software development process

Chapter 1: Introduction © HĐC 2022.1 44


Waterfall Model
 Requirements – defines
needed information,
function, behavior,
performance and
interfaces.
 Design – data structures,
software architecture,
interface representations,
algorithmic details.
 Implementation – source
code, database, user
documentation, testing.

Chapter 1: Introduction © HĐC 2022.1 45


Waterfall Model – Strengths
 Easy to understand, easy to use
 Provides structure to inexperienced staff
 Milestones are well understood
 Sets requirements stability
 Good for management control (plan, staff, track)
 Works well when quality is more important than cost or
schedule

Chapter 1: Introduction © HĐC 2022.1 46


Waterfall Model – Deficiencies
 All requirements must be known upfront
 Deliverables created for each phase are considered
frozen – inhibits flexibility
 Can give a false impression of progress
 Does not reflect problem-solving nature of software
development – iterations of phases
 Integration is one big bang at the end
 Little opportunity for customer to preview the system
(until it may be too late)

Chapter 1: Introduction © HĐC 2022.1 47


Waterfall Model – When to use
 Requirements are very well known
 Product definition is stable
 Technology is understood
 New version of an existing product
 Porting an existing product to a new platform.
 High risk for new systems because of specification and
design problems.
 Low risk for well-understood developments using familiar
technology.

Chapter 1: Introduction © HĐC 2022.1 48


Agile model

Chapter 1: Introduction © HĐC 2022.1 49


Step 1 – Analyse Requirements
 Tasks:
 List requirements
 Discuss with system analysis staff and users
 Clarify input, output, processing, and data components

Input Process Output (IPO) chart –


Identify inputs, outputs and
processing steps

Chapter 1: Introduction © HĐC 2022.1 50


Step 2 – Design Solution
 Approaches

Object-oriented
design
Two approaches

Structure design, or
top-down design
Break the
system into
procedures
to solve Starting with
problems general then
detailed design

Chapter 1: Introduction © HĐC 2022.1 51


Step 2 – Design Solution
 Hierarchy chart
 Observant modules
 Architecture

Chapter 1: Introduction © HĐC 2022.1 52


Step 2 – Design Solution
 Object-oriented (OO)
design
 Developer encapsulates data
and related procedures into an
object
 Similar objects are grouped
into classes
 A class diagram represents the
relationship amongst the
classes

Chapter 1: Introduction © HĐC 2022.1 53


Step 3 – Validate Design
 Tasks:

Developer verify the logic of


Evaluate the program
the program and identify the
accuracy
logic errors

Logic error
would cause unexpected
Desk check
Developer uses testing data results
to verify the program

Structured walkthrough
Test data Developer describes algorithm
is similar to actual data while the programming team
which should be validate program logic
processed by the program

Chapter 1: Introduction © HĐC 2022.1 54


Step 4 – Implementation
Writing code: transform the design into executable
program
 Syntax – Principles of writing instructions
 Comments – Program documentation
 Extreme programming (XP) – coding and testing right
after requirements have been identified

Chapter 1: Introduction © HĐC 2022.1 55


Step 5 – Test solutions
 Tasks

Debugging – Search and correct


Ensure that the program is
syntax and logic errors
runnable and provides
expected results

Test beta version, deliver


to Users for trial use and
collect feedbacks

Chapter 1: Introduction © HĐC 2022.1 56


Step 6 – Documentation
 It’s an essential task!!!
 2 sub-tasks

Revise program code – remove


Revise and finalise
dead code, i.e. the codes are
documentation
never called

Chapter 1: Introduction © HĐC 2022.1 57


What is programming?
 Programming > Coding
 Programming Design idea + Coding + Testing +
Debugging

Chapter 1: Introduction © HĐC 2022.1 58


Programming steps
Source code

Compiler

Compiled
code

Linker Library

Executable
program

Deploy &
Execute
Chapter 1: Introduction © HĐC 2022.1 59
Developing tools / environment
 IDE (Integrated Development Environment)
 Support the whole process of programming
 E.g.: MS Visual Studio, Borland C++ (Builder), Keil-C
 Tools
 Editor
 Compiler
 Linker
 Loader
 Debugger
 Project manager

Chapter 1: Introduction © HĐC 2022.1 60


Compiler
 A compiler is a computer
program that translates
computer code written in
one programming language
(the source language) into
another language (the
target language).
 The name "compiler" is
primarily used for
programs that translate
source code from a high-
level programming
language to a lower level
language (e.g., assembly
language, object code, or
machine code) to create an
executable program

Chapter 1: Introduction © HĐC 2022.1 61


Interpreter
 An interpreter is a computer
program that directly executes
instructions written in a
programming or scripting
language, without requiring
them previously to have been
compiled into a machine
language program
 It does not generate an object
program

Chapter 1: Introduction © HĐC 2022.1 62


1.5. Fundamentals of C/C++ languages
 C:
 At Bell Labs by Dennis Ritchie between 1972 and 1973 for
Unix.
 K&R C (Brian Kernighan and Dennis Ritchie) in 1978
 During the 1980s, C gradually gained popularity, many C
compilers
 Standardized by the ANSI since 1989 (ANSI C - American
National Standards Institute) and by the International
Organization for Standardization.
 Newest release C18 (ISO/IEC 9899:2018)
 Next version C2x
 Embedded C
Chapter 1: Introduction © HĐC 2022.1 63
1.5. Fundamentals of C/C++ languages
 C++:
 Bjarne Stroustrup’s “C with Classes” in 1979
 C++ in 1982

 First commercial implementation in 1985


 In 1998, C++ started to be standardized as C++98 (ISO/IEC
14882:1998)

 Newest release C++17 (ISO/IEC 14882:2017)


 Next version C++20

Chapter 1: Introduction © HĐC 2022.1 64


Why C/C++
 Features:
 Close to hardware
 High efficiency
 Relatively user-friendly to developers
 Interchangeable
 International Standardization
 Strength of ANSI-C
 Popular in most of microprocessor, microcontroller, DSP
 Popular to world-wide developers
 Strength of ANSI/ISO C++:
 Object-oriented programming
 Generic programming (template)
 Mathematical programming (Data Abstraction and Operator
Overloading)
Chapter 1: Introduction © HĐC 2022.1 65
Why C/C++
2021 2022

https://spectrum.ieee.org/top-programming-languages-2021
Chapter 1: Introduction © HĐC 2022.1
https://spectrum.ieee.org/top-programming-languages-2022 66
“C makes it easy to shoot
yourself in the foot;
C++ makes it harder, but
when you do it blows your
whole leg off.”
― Bjarne Stroustrup
Chapter 1: Introduction © HĐC 2022.1 67
1.6 Getting started
 Windows:
 Install Visual Studio Code (VS Code)
https://code.visualstudio.com/download
 Install C++ extension for VS Code
 Install MinGW-w64 (https://sourceforge.net/projects/mingw-w64/)
in a folder with simple path, i.e.: C\mingw64
 Set Mingw64\bin in Windows Environment Variables
 Linux:
 gcc or g++ may be installed by default, otherwise install it:
sudo apt-get update
sudo apt-get install build-essential gdb
 MacOS: Use Clang for compiling
 Check if Clang is install: clang --version
 Install it: xcode-select --install

Chapter 1: Introduction © HĐC 2022.1 68


Installation
 Install MinGW-w64
 Version: (choose the highest)
 Architecture: x86_64
 Threads: win32 or posix
 Exceptions: seh
 Build revision: 0
 Add the path to your Mingw-w64 bin folder to the
Windows PATH environment variable

Chapter 1: Introduction © HĐC 2022.1 69


MinGW-W64 configuration

Chapter 1: Introduction © HĐC 2022.1 70


MinGW-W64 configuration

Chapter 1: Introduction © HĐC 2022.1 71


MinGW-W64 configuration

Chapter 1: Introduction © HĐC 2022.1 72


MinGW-W64 configuration

Chapter 1: Introduction © HĐC 2022.1 73


MinGW-W64 configuration
 After installing MinGW-w64, you can verify if the
installation is correct by opening a new Command
Prompt and typing:
 gcc --version
 g++ --version
 gdb --version
 Otherwise, the
code cannot be
compiled, and
you have to
reinstall/reconfig
Chapter 1: Introduction © HĐC 2022.1 74
VSCode configuration
 Install C++ extension for VS Code

Chapter 1: Introduction © HĐC 2022.1 75


VSCode configuration
 Configuration files in VSCode project
 c_cpp_properties.json (compiler path and IntelliSense settings)
 tasks.json (build instructions)
 launch.json (debugger settings) (.json JavaScript Object
Notation (JSON) format)

Chapter 1: Introduction © HĐC 2022.1 76


Compiler path

Chapter 1: Introduction © HĐC 2022.1 77


Build task (tasks.json)

Chapter 1: Introduction © HĐC 2022.1 78


Debugger configuration
(launch.json)

Chapter 1: Introduction © HĐC 2022.1 79


Start coding

Chapter 1: Introduction © HĐC 2022.1 80


Homework 1
 Install the following software packages in Windows
7/10:
 Visual Studio Code
 C++ Extension in Visual Studio Code
 Mingw64 and respected configuration
 Create a new project, write a simple program (Hello
World as an example) and execute
 Try to change a few argument in tasks.json and
launch.json to see what will happen

Chapter 1: Introduction © HĐC 2022.1 81


END OF CHAPTER 1

Chapter 1: Introduction © HĐC 2022.1 82

You might also like