Chapter 1: Programming Principles
Muhamad Najib Zamri
Unless otherwise specified, all materials and diagrams are adapted / modified from the slides
accompanying Gary J Bronson’s C++ for Engineers & Scientists 4 th Ed.
SBN-13: 978-1133187844 / ISBN-10: 1133187846
1
Chapter Outline
Section 1
1. Unit analysis
2. Classification of Software
Section 2
3. Software Development Procedure
Section 3
4. Algorithms
5. Programming Languages
2
1. Unit Analysis
3
Unit Analysis
• Using the correct units when performing computations is crucial.
• Consider calculating the area of a rectangle:
𝐴𝑟𝑒𝑎 = 𝐿𝑒𝑛𝑔𝑡ℎ × 𝑊𝑖𝑑𝑡ℎ
• The unit for length and width must be the same.
• For example, length is given as 2 cm, and the width as 3 m.
– at least one of these units must be converted such that both are in the same
units.
4
Unit Analysis
• Converting the length to m, the rectangle’s area is calculated as
1𝑚
𝐴𝑟𝑒𝑎 = 2 𝑐𝑚 × 3 𝑚 = 0.06 𝑚2
100 𝑐𝑚
• Similarly, if you choose to convert the width to cm, the calculation
becomes
100 𝑐𝑚
𝐴𝑟𝑒𝑎 = 2 𝑐𝑚 × 3 𝑚 = 600 𝑐𝑚2
1𝑚
5
Unit Analysis
• Performing a unit analysis:
– Include only the units and conversion factors in an equation.
– Cancel out corresponding units in the numerator and denominator.
1𝑚
𝐴𝑟𝑒𝑎 = 2 𝑐𝑚 × 3 𝑚 = 0.06 𝑚2
100 𝑐𝑚
Or
100 𝑐𝑚
𝐴𝑟𝑒𝑎 = 2 𝑐𝑚 × 3 𝑚 = 600 𝑐𝑚2
1𝑚
6
Conversion Factor
• Another example of conversion factor: 1 day = 24 hours.
• As a conversion factor, this equality can be written as either of the following
factions:
1 𝑑𝑎𝑦 24 ℎ𝑜𝑢𝑟𝑠
or
24 ℎ𝑜𝑢𝑟𝑠 1 𝑑𝑎𝑦
• Consider converting days to seconds:
24 ℎ𝑜𝑢𝑟𝑠 60 𝑚𝑖𝑛 60 𝑠𝑒𝑐
𝑑𝑎𝑦𝑠 × × × = 𝑠𝑒𝑐
1 𝑑𝑎𝑦 1 ℎ𝑜𝑢𝑟 1 𝑚𝑖𝑛
7
Unit Systems
• Commonly used unit systems for Engineering and scientific works:
– English Engineering system
– International System (SI)
8
Unit Systems
Table 1.1 Commonly Used Physical Quantities
9
Unit Analysis
• Apply one conversion factor at a time and cancel units as you go along
to avoid multiplying or dividing by the wrong conversion factor.
• If the final unit does not yield the correct resulting unit, then the
resulting numerical answer must be incorrect.
10
2. Classification of Software
11
Classification of Software
• Software can be classified into:
– System software: Collection of programs to operate the computer
system
• It must be loaded first; called booting the system.
• It is the interface between application software and the system.
– Application software: Programs written to perform particular tasks
for users
• It runs on the platform which is provided by system software.
12
Application Software
• The purpose of most application programs is to process data to
produce specific results
Figure 1.0 Basic procedural operations
13
Operating System
• Operating system: The set of system programs used to operate and
control a computer
– Also called OS
• Tasks performed by the OS include:
– Memory management
– Allocation of CPU time
– Control of input and output
– Management of secondary storage devices
14
3. Software Development Procedure
15
Software Development
• Computer program: Self-contained set of instructions used to
operate a computer to produce a specific result
– Also called software
– Solution developed to solve a particular problem, written in a form
that can be executed on a computer
16
Software Development
• Software development procedure: Helps developers understand the
problem to be solved and create an effective, appropriate software
solution
• Software engineering:
– Concerned with creating readable, efficient, reliable, and maintainable programs
and systems
– Uses software development procedure to achieve this goal
17
Software Development
Figure 1.2 The three phases of program development
18
Phase I: Development and Design
• Program requirement: Request for a program or a statement of a
problem
• After a program requirement is received, Phase I begins:
• Phase I consists of four steps:
– Analysis
– Design
– Coding
– Testing
19
Phase I: Development and Design
Figure 1.3 The development and design steps
20
Phase I: Step 1
• Step 1: Analyze the Problem
– Basic Analysis:
• Determine and understand the output items the program must
produce
• Determine the input items
• Both items referred to as the problem's input/output (I/O)
– Extended Analysis
• To gather more information about the problem
21
Phase I: Step 2
• Step 2: Develop a Solution
– Select the exact set of steps, called an “algorithm,” to solve
the problem
– Refine the algorithm
• Start with initial solution in the analysis step until you have an
acceptable and complete solution
– Check solution
22
Phase I: Step 2
• Step 2: Develop a Solution
– For example
• Program to track the number of a product in inventory
• Program output:
– Description of all products
– The number of units of each product
23
Phase I: Step 2
• Step 2: Develop a Solution
(Example)
– A first-level structure
diagram for an inventory
tracking system
Figure 1.4 A first-level structure diagram
24
Phase I: Step 2
• Step 2: Develop a
Solution (Example)
– An inventory tracking
system with further
refinements.
Figure 1.5 A second-level structure diagram
25
Phase I: Step 3
• Step 3: Code the Solution
– Consists of actually writing a C++ program that corresponds to the
solution developed in Step 2
– Program should contain well-defined patterns or structures of the
following types:
• Sequence
Also known as
• Selection
Programming
• Iteration Structure / Paradigm
• Invocation
26
Phase I: Step 3
• Step 3: Code the Solution
– Sequence: Defines the order in which instructions are executed
Instruction
Instruction
Instruction
27
Phase I: Step 3
• Step 3: Code the Solution
– Selection: Allows a choice between different operations, based on
some condition
yes condition no
Instructions A Instructions B
28
Phase I: Step 3
• Step 3: Code the Solution
– Iteration: Allows the same operation to yes
be repeated based on some condition condition
no
• Also called looping or repetition
Instructions
Instructions
29
Phase I: Step 3
• Step 3: Code the Solution
– Invocation: Involves invoking a set of statements when needed
Task 1
Instruction
Task 2 Instruction
Instruction
Task 3
30
Phase I: Step 4
• Step 4: Test and Correct the Program
– Testing: Method to verify correctness and that requirements are met
– Bug: A program error
– Debugging: The process of locating an error, and correcting and
verifying the correction
– Testing may reveal errors, but does not guarantee the absence of
errors
31
Phase I: Step 4
• Step 4: Test and Correct the Program
– Table 1.3 lists the comparative amount of effort typically expended on each
development and design step in large commercial programming projects
Table 1.3 Effort Expended in Phase I
“It’s impossible to write a successful program for a problem that’s not fully understood.”
32
Phase II: Documentation
• Five main documents for every problem solution:
– Program description
– Algorithm development and changes
– Well-commented program listing
– Sample test runs
– Users' manual
33
Phase III: Maintenance
• Maintenance includes:
– Ongoing correction of newly discovered bugs
– Revisions to meet changing user needs
– Addition of new features
• Usually the longest phase
• May be the primary source of revenue
• Good documentation vital for effective maintenance
34
Backup
• Process of making copies of program code and documentation
on a regular basis
• Backup copies = insurance against loss or damage
– Consider using off-site storage for additional protection
35
4. Algorithms
36
Algorithms
• Algorithm: Step-by-step sequence of instructions
– Must terminate
– Describes how the data is to be processed to produce the desired
output
Every individual has a different method of solving problems.
37
Algorithms: Case Study – Method 1
Case Study: How many ways can you approach the problem of calculating
the sum of all whole numbers from 1 through 100?
Figure 1.6 Summing the numbers 1 to 100
38
Algorithms: Case Study – Method 2
Case Study: How many ways can you approach the problem of calculating
the sum of all whole numbers from 1 through 100?
Figure 1.6 Summing the numbers 1 to 100 (continued)
39
Algorithms: Case Study – Method 3
Case Study: How many ways can you approach the problem of calculating
the sum of all whole numbers from 1 through 100?
Figure 1.6 Summing the numbers 1 to 100 (continued)
40
Algorithms: Case Study – Algorithm
Case Study: How many ways can you approach the problem of calculating
the sum of all whole numbers from 1 through 100?
Algorithm:
Set n equal to 100
Set a equal to 1
Set b equal to 100
Calculate n*(a+b)/2 and store the result in sum
Display the sum
41
Algorithms
Pseudocode Flowchart
• English-like phrases used to describe • Diagrams with symbols
steps in an algorithm
42
Flowchart Symbols
Figure 1.7 Flowchart symbols
43
Example: Flowchart
Example:
Calculating the average of three
numbers. Figure 1.8
1. Input the three numbers into Flowchart for
the computer’s memory calculating the
2. Calculate the average by average of
adding the numbers and three numbers
dividing the sum by three
3. Display the average
44
5. Programing Languages
45
Level of Programming Languages
• Programming Languages can be broadly classified into three
categories:
– Machine Language
– Assembly Language
– High Level Languages
46
Machine Language
• Machine language programs: only programs that can actually be used
to operate a computer
– Also referred to as executable programs (executables)
– Consists of a sequence of instructions composed of binary numbers
– Contains two parts: an instruction and an address
– Example: R-type MIPS instructions
47
Assembly Language
• Assembly language programs: Substitute word-like symbols, such as
ADD, SUB, and MUL, for binary opcodes
– Use decimal numbers and labels for memory addresses
• Example: ADD 1, 2
• Assemblers: Translate programs into machine language
Figure 1.10 Assembly-language programs must be translated
48
Low- and High-Level Languages
• Low-level languages: Languages that use instructions tied directly to
one type of computer
– Examples: machine language, assembly language
• High-level languages: Instructions resemble written languages, such as
English
– Can be run on a variety of computer types
– Examples: Visual Basic, C, C++, Java
49
Low- and High-Level Languages
• Source code: The programs written in a high- or low-level
language
– Source code must be translated to machine instructions in one of two
ways:
• Interpreter: Each statement is translated individually and executed
immediately after translation
• Compiler: All statements are translated and stored as an executable program,
or object program; execution occurs later
– C++ is predominantly a compiled language
50
Procedural and Object Orientations
• Programs can also be classified by their orientation:
– Procedural: Available instructions are used to create self-contained units called
procedures
– Object-oriented: Reusable objects, containing code and data, are manipulated
• Object-oriented languages support reusing existing code more easily
• C++ contains features of both
51
Summary
52
Common Programming Errors
• Common errors include:
– Failing to use consistent units
– Using an incorrect form of a conversion factor
– Rushing to write and run a program before fully understanding the
requirements
– Not backing up a program
– Not appreciating that computers respond only to explicitly defined
algorithms
53
Concerns?
54
012-4579063
55