0% found this document useful (0 votes)
62 views28 pages

Fundamental of C++ Programming

The document discusses fundamentals of C++ programming including objectives, what programming is, types of translations between programming languages and machine language, an overview of programming languages, what programming looks like in different languages, how to write a program by planning steps and debugging, algorithms and pseudocode, flowcharts, and programming control structures like sequence, selection, and iteration. Key terms like compiler, interpreter, variable, function, loop, and debug are also defined.

Uploaded by

tazeb Abebe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views28 pages

Fundamental of C++ Programming

The document discusses fundamentals of C++ programming including objectives, what programming is, types of translations between programming languages and machine language, an overview of programming languages, what programming looks like in different languages, how to write a program by planning steps and debugging, algorithms and pseudocode, flowcharts, and programming control structures like sequence, selection, and iteration. Key terms like compiler, interpreter, variable, function, loop, and debug are also defined.

Uploaded by

tazeb Abebe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Chapter 2

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

An algorithm is a sequence of a finite number of steps arranged in


a specific logical order which, when executed, produces the
solution for a problem.
An algorithm must satisfy these requirements:
 It must have an input
 It must have an output
 It should not be ambiguous (there should not be different interpretations to
it)
 It must be general (it can be used for different inputs)
 It must be correct and it must solve the problem for which it is designed
 It must execute and terminate in a finite amount of time
 It must be efficient enough so that it can solve the intended problem using
the resource currently available on the computer
 An algorithm can be represented using pseudocodes or flowcharts
What is pseudocode?
Pseudo code is Structured English used to represent an
algorithm.
Pseudo code is English-like language used to express
algorithms (less restrictive/formal than a programming
language) .
It is easy for both users and programmers to read and write
algorithms regardless of the programming experience.
There is no standard pseudo code but we should follow some
conventional rules below:
 Statements are written in simple English.
 Each statement is written on a separate line.

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

Programming control structure controls how the each statement


(step) of the algorithm flows (executes). Control structures in
most programming languages typically include the following.
1. Sequence
The sequence control structure is the straightforward execution of
one processing step after another.

Sequence’s pseudo code:


Program Start
Process A
Process B
Process C
Program Stop
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

 REPEAT_UNTIL structure is similar to DOWHILE structure, in that a


group of statements are repeated in accordance with a specified condition.
But REPEAT_UNTIL tests the condition at the end of the loop. This means
that the statements within the loop will be execute once before the condition
is tested. The loop will be terminated if the result of condition is True.
 REPEAT_UNTIL’ s pseudo code:
Program Start
REPEAT
Process A
Process B
Process C .
.
.
Process n
StopUNTIL (Condition)
Program
Programming control structures

Algorithm development examples


 In developing an algorithm, the problem should be
defined and divided into three separate components:
Input: a list of the source data provided to the problem.
Output: a list of the outputs required.
Processing: a list of actions needed to produce the
required output.
Example01: Add three numbers
A program is required to read three numbers from a user,
add them together, and print their total on the screen.
Identify I/O
 Input: number1, number2, number3
 Output: Total
 Process: read three numbers, add numbers together, print total to
the screen
Programming control structures
Write pseudo code and flow chart according
 Pseudo code:
Program Start
Set number1, number2, number3, total = 0
Read number1, number2, number3
total = number1+number2+number3
Print total
Program Stop
Programming control structures
Compare two numbers
A program is required to read two numbers from a
user, compare them to find out whether number1 is
greater than number2 or not. Print the result of the
comparison on the screen. i.e. number1 is greater than
number2
Identify I/O
Input:number1, number2
Output: larger number
Process: Read number1, number2
Compare number1 and number2
Print output message on the screen
Programming control structures
 Write pseudo code and flow chart according to this defining diagram
 Pseudo code:
Program Start
Set number1, number2 = 0
Read number1, number2
IF number1> number2 THEN
Print “Number1 is greater than number2”
ELSE
Print “Number2 is greater than number1”
ENDIF
Program Stop
Programming control structures
Find average score
A program will accept 10 students’ scores then calculate the
average score. After finishing the calculation, the program
will print the average score on the screen.

Input: Score (10 scores)


Output: Average
Process: Read score,
Calculate total,
Calculate count
Compute average
Print average on the screen
Pseudo code:
Program Start
Set score, total, count, average = 0
DOWHILE count<10
Read score
Add score to total
Add 1 to count
ENDDO
average = total/count
Print average
Program Stop
Check weather a given number is even or odd

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??

You might also like