TDB2073 - SPDB
Introduction
Adapted from C++ for Everyone and Big C++ by Cay Horstmann
Objectives
To know what is programming
To know how to write simple first C++ program
To recognize syntax and logic errors
To understand how compilation works
To understand the activity of programming
To understand the notion of an algorithm
What Is a Computer Program?
- Software
The programs the computer executes are called
the software.
A computer program tells a computer, the
sequence of steps that are needed to fulfill a task.
A typical operation may be one of the following:
Add up these two numbers.
If this value is negative, continue the program
at a certain instruction.
What is Programming?
The act of designing and implementing
computer programs is called programming.
A programmer designs and implements these
programs.
Most computer users are NOT computer
programmers.
To implement program we use programming
languages
Ex : C, Visual Basic, C++
What is Programming?
For example, to tell the computer to write
Hello World! on the screen, you will write a
program.
You will be creating software:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!" <<
endl;
return 0;
}
Your First Program Explained
But first, some things about the code:
C++
is case sensitive. Typing:
int MAIN()
will not work
has free-form layout
int main(){cout<<"Hello, World!"<<endl;return 0;}
will work (but is practically impossible to read)
A good program is readable.
Your First Program Explained
Your First Program Explained
The main Function
A function is a collection of programming instructions that carry out a
particular task.
The construction
int main()
{
...
return 0;
}
defines a function.
The functions name is main.
The function returns the integer 0
(which indicates that the program finished successfully).
Every C++ program must have one and only one main function.
Most C++ programs contain other functions besides main (more about
functions later).
Your First Program Explained
#include<iostream>
- read the file iostream that contains the definition for
the stream input/output package.
using namespace std;
- all names in the program belong to the "standard
namespace"
Your First Program Explained
The Output Statement
cout << "Hello World!" << endl;
Each C++ statement ends in a semicolon.
"Hello World!" is called a string.
The endl symbol denotes an end of line marker.
OR:
cout << "Hello World!\n";
The sequence of characters enclosed in quotations
marks ("Hello, World\n") is called a string.
Escape sequence indicates a newline.
Errors (Syntax and Logic Errors)
Syntax error or compile-time error:
Something violates the language rules.
Compiler finds the error.
cot << "Hello, World!";
cout << "Hello, World!\";
Logic error or run-time error:
Program doesn't do what it's supposed to do. The syntax is
correct, but executes without performing the intended action.
Programmer must test and find the error.
cout << "Hell, World\n";
cout << "Sum of num1 and num2:" << 5 - 6;
The Compilation Process
The compiler translates C++ programs into machine
code.
The linker combines machine code with library code into an executable program.
The Edit-Compile-Run
Loop
This process reflects the way
programmers work
(shown as a flowchart)
Algorithms
The Software
Development Process
For each problem
the programmer goes
through these steps
Describing an Algorithm with
Pseudocode
An informal description
Not in a language that a computer can understand, but easily
translated into a high-level language (like C++).
The method described in pseudocode must be
Unambiguous
There are precise instructions for what
to do at each step
and where to go next.
Executable
Each step can be carried out in practice.
Terminating
It will eventually come to an end.
Describing an Algorithm with
Pseudocode
Consider this problem:
You put RM10,000 into a bank account that earns 5%
interest per year. How many years does it take for the
account balance to be double the original?
Determine the inputs and outputs.
In our sample problem, we have these inputs:
Bank deposit : RM10,000
Interest rate : 5%
We simply want to know how many years does it take for the account
balance to be double the original (10k x 2 = 20k).
That is the desired output.
Output : Years
Describing an Algorithm with
Pseudocode
Step 1: Set balance to 10,000 and year to 0
Step 2: Repeat steps 2a-2b while balance <
20,000
Step 2a. Add one to the year value
Step 2b. Calculate the new balance value
(multiply the balance value by 1.05) .
Describing an Algorithm with
Pseudocode
Step 3: Report the last value of the year in the repetition process.
That value is the number of years required to double the investment
After Year
Balance (RM)
10,000
10,000.00 x 1.05 = 10,500.00
10,500.00 x 1.05 = 11,025.00
11,025.00 x 1.05 = 11,576.25
11,576.25 x 1.05 = 12,155.06
After year 14 , Balance = RM 19799.32
After year 15 , Balance = RM 20789.28
So output = 15
Describing an Algorithm with
Flowchart
Describing an Algorithm with
Flowchart
Start
year = 0 , balance = 10 000
balance = balance * 1.05
year = year + 1
True
balance < 20k
False
Print year, balance
End
Flowchart - Example
Sub-process (Add Value)
Summary
1.
2.
3.
4.
5.
6.
A computer program is a sequence of instructions and
decisions.
Programming is the act of designing and implementing
computer programs.
Computer programs are stored as machine instructions in a
code that depends on the processor type.
High-level programming languages are independent of the
processor.
A syntax error is a part of a program that does not conform to
the rules of the programming language while logic errors are
errors in a program that executes without performing the
intended action.
The compiler translates C++ programs into machine code.
Summary
9.
10.
11.
The linker combines machine code with library code into an
executable program.
An algorithm for solving a problem is a sequence of steps
that is unambiguous, executable, and terminating.
Algorithm can be described using pseudocode and
flowchart.
Lecture Exercise 2
Expression and Equation
Volume = a x b x h
Write the algorithm to solve this equation.
Lecture Exercise 2
As an EE engineer, you are required to develop a program,
which will check for the size of motherboard depending
on the size (in cm) and numbers of CPUs to be put on the board.
Given formula:
motherboard size/5 = CPU Size * Numbers of CPU
CPU size and numbers of CPU are to be entered by the user!
Write the algorithm to solve this equation.
References
Big C++ 2nd Edition, Cay S. Horstmann, Timothy A.
Budd, Wiley.
C++ for Everyone, 1st Edition, Cay S.
Horstmann,Wiley