C++ Programming
Problem Solving and
Programming
Chapter 1a
Introduction To
Program Development
Objectives
In this chapter you will:
• Program and software
• Learn about the language of a computer
• Learn about the evolution of programming languages
• Examine high-level programming languages
• Discover what a compiler is and what it does
• Examine how a high-level language program is processed
• Compilation, executing and debugging
• Good programming habits
2
Computer Components
Input
Hardwar
e
Output Process
Computer Operations
Processin
• Data g • Information
• Keyboard data • Validation • Reports
• Mouse click • Calculation • Audio
• Others • Storing
• Others
Input Output
Program & Software
Computer – programmable machine designed to
follow instructions
Program – instructions in computer memory which
computer follows to perform a task
Programmer – person who writes instructions
(programs) to make computer perform a task
So, without programmers, no programs; without
programs, a computer cannot do anything
5
Program & Software
• Software is a collection of programs and data that
instruct the computer what to do
• Categories of software:
− System software: Operating System that
manage the computer hardware and the
programs that run on them. Examples:
Windows, UNIX, Linux
− Application software: programs that provide
services to the user. Examples : word
processing, games, programs to solve specific
problems
6
Program & Software
• The programmer uses Computer / Programming
Language to write programs
• Syntax refers to the spelling and grammars of a
programming language
• Algorithm is a sequence of instructions written for
computer to solve a problem.
7
Algorithms (Example)
Steps for a customer to buy a movie ticket
1. Queue up
2. Go to the designated counter
3. Order ticket
✔ Specify movie
✔ Specify time
✔ Specify number of tickets
4. Make payment
5. Get tickets
6. Leave the counter
Computer Languages
• Categories of computer languages:
o Machine language
o Symbolic/Assembly language
o High-level language
9
Evolution of
Computer Languages
10
Machine Language
• The computer can only understand and execute
machine language instructions
• Machine language instructions consist of binary
numbers (0s and 1s) such as 10110100000101
• It is platform-dependent
• Rather than writing programs in machine language
which is very hard, the programmers use other
programming languages
11
Symbolic/Assembly Language
• Use symbolic codes, mnemonic and meaning
abbreviations. It is also platform-dependent
• Assembler: used to translate a program written in
assembly language into machine language for
execution by the computer
• Easier to program
12
High-Level Language
• Use English-like codes and mathematical
operators
• It is platform-independent
• High-level languages include Python, C++, C, C#,
Java, Swift, etc
• Compiler: used to translate a program written in a
high-level language to machine language
• Very easy to program
13
Creating a Simple Program
• Program coding
• Text editor (such as Notepad)
• Integrated Development Environment (IDE)
• Type of application
• Console (command line)
• Windows (desktop)
• Web
Example of IDE - Visual Studio
Build & Run: To
compile and execute
program
Text editor: write codes
Output: Check error/status
Console Application
Compilation process of
C++ program
1) Source code is written Editor
Library
in C++
2) Convert the C++ code Compiler
into object code
3) Linker links the object Disk
Linker
code with libraries
4) Loader puts program
Loader
in memory
Memory
5) Ready to run (platform-
CPU
dependent)
Program Development
• To develop a program, programmer must
complete the following steps:
− Understand the problem
− Develop a solution
− Write the program
− Test the program
18
Program Development
Understand
the problem
Understand the problem
• Carefully study the user
Develop a requirements.
solution
• Understand what he wants the
program to do and what kind of
Write the output he wants to have.
program
Test the
program
19
Program Development
Understand
the problem Develop a solution
• Design the logic of the program by
Develop a using tools such as:
solution − Structure chart
− Pseudocode
Write the − Flowchart
program
Test the
program
20
Program Development
Understand
the problem
Write the program
• Code the actual program by using
Develop a the preferred programming
solution
language.
Write the
program
Test the
program
21
Program Development
Understand
the problem
Test the program
• Run and test the program to
Develop a ensure it is free of logical errors.
solution
Write the
program
Test the
program
22
Example 1-1 : Rectangle
• Write a C++ program to find the area and
perimeter of a rectangle
• The area and perimeter of the rectangle
are given by the following formulas:
perimeter = 2 * (length + width)
area = length * width
23
Tool: Structure Chart
• A hierarchy chart that shows the
functional flow through the program.
• Shows how the program is broken into
logical steps. Each step will be a separate
module.
24
Example 1.1: Structure chart
Area &
perimeter of
rectangular
Calculate
Get length & Display area
area &
width & perimeter
perimeter
Tool: Pseudocode (Algorithm)
• Use English words to convey program
logic.
• Contains the logical steps / algorithms to
accomplish the task.
26
Example 1-1 : Pseudocode
Start
1. Get length of the rectangle
2. Get width of the rectangle
3. perimeter = 2 * (length + width)
4. area = length * width
5. Display area and perimeter
End
27
Tool: Flowchart
• Use standard graphical symbols to
represent the logical flow of data through
a program.
28
Flowchart Symbols
Input/Output Processing Module
Terminal Decision Flowlines
On-page Off-page
Connector Connector
Example 1.1: Flowchart
START
Read
length &
width
area =
Length * width
perimeter =
2 * (Length + width)
Display area
& perimeter
END
Example 1-1 : C++ program
1. #include <iostream>
2. using namespace std;
3. main() {
4. int length, width, area, perimeter;
5. cout<<"Enter length and width of
rectangle\n";
6. cin>>length;
7. cin>>width;
8. area = length * width;
9. perimeter = 2*(length + width);
10. cout<< "Area = "<<area;
11. cout<< "Perimeter = "<<perimeter;
12. return 0;
13. }
31
Exercise
By using structure chart, pseudocode and
flowchart, find the total and product of 2
numbers. Display the result.
32
Debugging
• To find any error in the program
• Bugs – error, also known as exception, occurs during
compilation or execution.
• Common programming errors:
• Syntax errors (Compilation errors)
• Run-time errors
• Logic errors
Common programming errors
• Syntax Errors
✔ Code violation during compilation, e.g.:
o Missing semicolon (;)
o Undefined variable
o Did not close ’/*’ with ‘/’
• Run-time Errors
✔ Illegal operation occur during execution , e.g.:
o Divide a number by zero
o Open a non-existing file
o Write to a non-existing printer
Common programming errors
• Logic Errors
✔ Passed Syntax and Run-time Errors but produce
false result
✔ It is usually caused by the algorithm of the
program
// E.g. Calculate the average
int a =20;
int b = 34;
cout << “Average = ” << (a + b); ← Used wrong formula
C++ Basic Syntax
1. #include <iostream> //allow input/output
2. int main() //starting point
3. {
4. int mark; // variable declaration
5. std::cout << “Sample: \n Enter mark:”;
//display
6. std::cin >> mark; //get input
7.
8. return 0; //end point
9. }
Good Programming Habits
⮚ Use program comments
⮚ Use meaningful identifiers
⮚ Design clear statements
⮚ Write clear message & clear instruction
⮚ Backup your codes