Fundamental of C++ Programming
Fundamental of C++ Programming
Fundamental of C++
Programming
Objectives
After learning this chapter, you will be able to…
Given a task, create pseudocode
Given pseudocode, create a flowchart
Define/describe these terms: program, compile vs. interpret, loop, variable,
function, syntax, code, debug, IF THEN ELSE
What is programming?
A program is a set of instructions that guide the computer in
carrying out a specific task.
Programming is a skill that can be acquired by a computer
professional
that gives him/her the knowledge of making the computer perform
the required operation or task.
To write a program, we need to have programming languages such
as c ++ and java
Computer can not understand these languages. In order for
computer to carry out the instructions, the program must be
translated to a language that the machine can understand, called
machine language and then executed.
Types of Translations:
Assembler converts program developed with assembler Language to
machine language
Compiler converts program developed with High Level Language to
machine language
Interpreter converts program developed with High Level language to
machine language.
What is programming?
Compiler converts all contents to machine code at a time where as
interpreter converts the program to machine code line by line.
Some programming languages (like Java or C++)
require the code to be compiled (translated to binary)
before it can be started.
Others (like JavaScript) are interpreted, meaning that
each command is translated separately when the
program is started.
What is a programming language?
Set of commands that a computer has been “taught” to understand
Languages that look like “machine code” (e.g., 82A8: jsr r5,@#82AE
82AC: sob r0,8296) are used for…
Writing games
Writing application programs (like Excel)
Other languages look like English (“high level,” e.g., PRINT “HELLO”)
Logo
JavaScript
And many more
Programming language Overview
What does programming look like?
Here are some examples of an instruction to print the word ‘Hello’
Logo PR [Hello]
JavaScript alert(“Hello”);
FORTRAN PRINT “Hello”
BASIC PRINT “Hello”
COBOL DISPLAY ‘Hello’.
C++ COUT<<“Hello”;
Pascal WRITELN(‘Hello’);
Assembly XPRNT MESSAGE1 Language MESSAGE1 DC
‘Hello’
How do you write a program?
Decide what steps are needed to complete the task
Write the steps in pseudocode (written in English) or as a
flowchart (graphic symbols)
Translate into the programming language
Try out the program and “debug” it (fix if necessary)
Algorithms and Pseudo code
Example
Task: add two numbers
Pseudocode:
Start
Get two numbers
Add them
Print the answer
End
Flowchart
A flowchart is a graphical diagram that depicts the “flow” of a program using a
series of standard geometric symbols and lines, which are connected according
to the logic of the algorithm. There are a lot of symbols used in flowcharting
but the common five are:
Programming control structures
2. Selection
The selection control structure is the presentation of a
condition and two actions between two actions. The choice
depending on whether the condition is true or false.
• IF’ s pseudo code: IF-ELSE’ s pseudo code:
• Program Start Program Start
• IF (Condition) Then IF (Condition) Then
• Process A Process A
• End IF ELSE
• Program Stop Process B
End IF
• Program Stop
Programming control structures
Programming control structures
3. Iteration (Looping)
The iteration control structure can be defined as the representation of a set of
instructions to be performed repeatedly, as long as the condition is satisfied (TRUE or
FALSE). There are two main types of iteration control structure: DOWHILE and
REPEAT_UNTIL.
DOWHILE’ s pseudo code:
Program Start
DOWHILE (Condition)
Process A
Process B
Process C
Process D
Process E
Process F
.
.
Process n
ENDWHILE
Program Stop
Programming control structures
Pseudo code:
Program Start
Get number
IF number/2==0
Display Even
ELSE
Display Odd
Program Stop
General Rules for flowcharting
1. All boxes of the flowchart are connected with Arrows. (Not lines)
2. Flowchart symbols have an entry point on the top of the symbol
with no other entry points. The exit point for all flowchart symbols is
on the bottom except for the Decision symbol.
3. The Decision symbol has two exit points; these can be on the sides
or the bottom and one side.
4. Generally a flowchart will flow from top to bottom. However, an upward flow can be shown as
long as it does not exceed 3 symbols.
5. Connectors are used to connect breaks in the flowchart. Examples are:
• From one page to another page.
• From the bottom of the page to the top of the same page.
6. All flow charts start with a Terminal or Predefined Process (for interrupt programs or
subroutines) symbol.
7. All flowcharts end with a terminal or a contentious loop.
Exercise
1. Design an algorithm and the corresponding flowchart
for finding the sum of the numbers 2, 4, 6, 8, …, n
2. Using flowcharts, write an algorithm to read 100
numbers and then display the sum.
3. Write an algorithm to read two numbers then display
the largest
Questions??