Ch1-Introduction To C++
Ch1-Introduction To C++
Fundamentals of Programming
(Pre-Informatics)
Fundamentals of programming 1
Contents
• Elements of a Computer System
• What is computer programming
• Types and usage of computer languages
• Programming paradigms
• System Development Life Cycle
• Algorithm development and representation
• Flow chart
• Psedocode
Fundamentals of programming 2
What is Computer?
Hardware
The physical piece of a computer can be
touched and seen
Fundamentals of programming 4
Elements of A Computer System
Software
Is set of instructions, stored digitally within the
computer's memory, which tells the computer system
what to do
Categorized: System
Fundamentals& Application SW
of programming 5
System Software Operating Systems
Compiler,
Interpreter
Linker, Loader
Device drivers
Fundamentals of programming 7
A Layered View a Computer System
Application Programs
Word-Processors,
Spreadsheets,
Database Software, IDEs,
etc…
System Software
Compilers, Interpreters,
Preprocessors, etc.
Operating System, Device
Drivers
Machine with all its
hardware
Fundamentals of programming 8
Computer Programming
Computer programs (source code) are the
instructions that tells the computer what to do.
Object code == Binary code == Machine code.
The process of writing, testing,
debugging/troubleshooting, and maintaining the
source code of computer programs
A computer program usually consists of two elements:
– Data – characteristics
– Code – action
Computer programs is often written by professionals
known as Computer Programmers (simply
programmers)
Fundamentals of programming 11
Types of computer programming language
Programming Language
Fundamentals of programming 12
Evolution of Programming
Languages
Machine language:
It is the lowest-level programming language
Provides program instructions in bit
The only languages understood by computers
Low level languages are machine specific or dependent.
A bit is a binary digit, the smallest increment of data on a computer.
Binary: 10110000 01100001
Fundamentals of programming 13
Assembly Language
An assembly language is a low-level language for programming
computers
In assembly language, an instruction is an easy-to-remember form
called a mnemonic
Computers produced by different manufacturers have different machine
languages and require different assemblers and assembly languages.
Assembler: A program that translates a program written in
assembly language into an equivalent program in machine
language
Fundamentals of programming 14
High-Level Languages
• High-level languages are relatively easy to learn because
the instructions are more closer to human natural language
such as English, French, German, and Spanish
• Basic, FORTRAN, COBOL, Pascal, C, C++, C#, and Java are all
high-level languages
Compiler: A program that translates a program written in
high-level language into an equivalent program in machine
language
Example:- C, C++, Java, C#....
Interpreters translate source code programs one line at a
time, easy dubbing and cross platform.
Example:- Python, Ruby, JavaScript, Pearl …..
Fundamentals of programming 15
Working of Compiler and Interpreter
Interpreter convert the code the HLL code into intermediate code while the program is running means the
execution of the program, and the translation runs parallel.
Fundamentals of programming 16
Working of Compiler and Interpreter
Fundamentals of programming 17
Working of Compiler
Steps of Compiler How Compiler work
Fundamentals of programming 18
Short Different
Fundamentals of Programming 19
Con…
Fundamentals of programming 20
Programming paradigm
Programming paradigm is a fundamental style of
computer programming
Programming paradigm:
– programming “technique” (?)
– way of thinking about programming
– view of a program
Fundamentals of programming 23
Procedural Programming: Example
• Writing a program to handle bank accounts
– Customer can open different type of accounts, such as
cash account, check account and Loan account
– For each account, customer can deposit, withdraw or
transfer
Struct account {
char name; Procedure 1: Deposit() {...}
Data Structure: int accountId;
Bank Account float balance; Procedure 1: Withdraw() {...}
float interestYTD;
char accountType; Procedure 1: Transfer() {...}
};
Fundamentals of programming 24
Some Limitations of Procedural Programming
Procedural programming mainly focuses on procedures or functions. Less
attention is given to the data.
The data and functions are separate from each other.
The program code is harder to write when Procedural Programming is
employed.
The Procedural code is often not reusable, which may pose the need to
recreate the code if is needed to use in another application.
Difficult to relate with real-world objects/ It does not model real-world
entities
The procedural programming approach does not work well for large and
complex systems.
Fundamentals of programming 25
Object-Oriented Programming
Widely used programming methodology
Some terminology:
object - usually a person, place or thing (a noun)
Each object has an identity, a behavior and a state. The state of an object is stored in
fields (variables), while methods (functions) display the object's behavior.
method - an action performed by an object (a verb)
type or class - a category of similar objects
Fundamentals of programming 32
• An algorithm can be represented as:-
– Flowchart
– Pseudocode
– Structured chart
33
Pseudocode
Pseudocode is an artificial and informal language that helps
programmers to develop algorithms.
Pseudocode (which means fake code, because its not really
programming code) specifies the steps required to accomplish
the task.
Pseudocode is a type of structured English that is used to
specify an algorithm.
Pseudocode cannot be compiled nor executed, and there are
no real formatting or syntax rules.
Fundamentals of programming 34
Programs Vs. Algorithms
• To make a computer do anything, you have to write a program.
• To write a computer program, you have to tell the computer, step by step,
exactly what you want it to do.
• The computer then "executes" the program, following each step mechanically,
to accomplish the end goal.
• An Algorithm is a finite, step-by-step sequence of instructions that describe how
the data is to be processed to produce the desired outputs.
• The algorithm is implemented by a program.
• An algorithm is a step-by-step procedure for solving the problem while
programming is a set of instructions for a computer to follow to perform a task.
• A program could also be an implementation of code to instruct a computer on
how to execute an algorithm.
35
An algorithm needs to be:
Fundamentals of programming 36
Example 1:
Write a Pseudocode algorithm that obtains two integer
numbers from the user. It will print out the sum of those
numbers.
Fundamentals of programming 37
Example 2:
Write a Pseudocode algorithm that finds average of any three
numbers.
Step 1: Start
Step 2: Read values of X,Y,Z
Step 3: S=X+Y+Z
Step 4: A=S/3
Step 5: Write value of A
Step 6: Stop
Fundamentals of programming 38
Example 3:
Write a Pseudocode algorithm that finds the biggest of two
numbers.
Step 1: Start
Step 2: Read A,B
Step 3: If A>B, then BIG=A, other wise BIG=B
Step 4: Write BIG
Step 5: Stop
Fundamentals of programming 39
Flow Chart
• A flowchart is a graphical representation of an algorithm
• Graphically depict the logical steps to carry out a task and show how the
steps relate to each other
• Once the flowchart is drawn, it becomes easy to write the program in
any high level language
• A flowchart (also spelled flow-chart and flow chart) is a
schematic representation of an algorithm or a process.
• The advantage of flowchart
– is it doesn’t depend on any particular programming language, so that
it can used,
– to translate an algorithm to more than one programming language
Fundamentals of programming 40
Flow Chart Symbols
Symbol Name Meaning Examples
Flow line • Used to connect symbols Start
& indicate the flow of logic
Read A, B
Fundamentals of programming
41
Rules for Flowchart
• Every flowchart has a START symbol and a STOP symbol
• The flow of sequence is generally from the top of the page to the
bottom of the page. This can vary with loops which need to flow
back to an entry point
• Use arrow heads on connectors where flow direction may not be
obvious
• There is only one flowchart per page
• A page should have a page number and a title
• A flowchart on one page should not break and jump to another
page
• A flowchart should have no more than around 15symbols (not
including START and STOP).
Fundamentals of programming 42
Example 1
• Draw a flow chart that finds average of any two numbers.
START
Input
x
Algorithm: Input
y
1. Input x, y
2. Sum x + Y Sum =
x+y
3. Average (A) = sum/2
4. output average Average=Sum
/2
Average
output
END
Fundamentals of programming 43
Example 2
• Draw a flowchart to find the largest of three numbers A, B, and C.
Fundamentals of programming 44
Add two number by Flowchart symbols
45
Control structures and statements in C++
Control structures: are the basic entities of a structured programming language.
All program processes can be implemented with these 3 control structures only.
There are three types of control structures(programming conventions)
available in C++.
1) Sequence structure (straight line paths)
2) Selection structure (one or many branches)
3) Loop structure (repetition of a set of activities)
Selection structures are implemented using If , If
Else and Switch statements.
Looping structures are implemented using While, Do
While and For statements.
46
Flowchart of Sequence structure
47
Flowchart for if and else..if statement
48
Cont.….
Fundamentals of programming 49
Selection statements using switch
• Multiple Selection (switch)
If the light is ...
red -> stop
green -> go
True
yellow -> slow down Light
Stop
Red?
False
True
Light
Green? Go
False
True
Light Slow
Yellow? Down
False
50
do… while and while loop
51
for Loop Flowchart in C++
52
Repetition (continued)
For loop:-
Teaching a baby to
Counter = 1
count from 1 to 10:
counter = 1
Add 1
if counter <= 10:
to counter
increment counter
Counter True
Print print
≤ 10?
counter counter number
False
53
…continued
The flowchart below represents an algorithm that displays
the numbers 20, 40, 60, 80, 100, 120, 140, 160, 180 on the
screen by do while loop.
54
Flowchart for break statement
55
Flowchart for continue statement
56
Flowchart for goto statement
57
Structure Charts
The primary tool used in structured design
is the structure chart.
Structure charts are used to graphically
show a modular design of a program.
Specifically, they show how the program
has been partitioned into smaller more
manageable modules, the hierarchy and
organization of those modules, and the
communication interfaces between
modules.
58
…continued
Structure charts, however, do not
show the internal procedures
performed by the module or the
internal data used by the module.
• There are three components in the
structured chart:
Modules
Connections between modules
Communication between
modules
59
…continued
Example:
• Write a program that asks the user to enter a temperature
reading in centigrade and then prints the equivalent Fahrenheit
value. CelsusToFarh
(main func)
centigrad Fahrenheit
centigrad
Fundamentals of programming 60
Translating algorithms to programming languages
61
Data structure
A data structure is a specialized format for
organizing and storing data in the memory of a
computer.
General data structure types include the array,
file, record, table, tree, lists, stacks, queues, heaps,
and graphs.
Any data structure is designed to organize data to
suit a specific purpose so that it can be accessed and
worked with in proper ways.
Review Questions
1. Define “Machine Language.”
• A language consisting of a series of numbers. These numbers
represent the actual instructions used by the computer. Easy for
computers to understand, but very difficult for humans.
2. Define “Assembly Language.”
• A language in which a single instruction translates directly into
a single machine instruction.
3. Define “source code.”
• The high level code written by the programmer. In a high-level
language, the source code is usually machine independent.
4. Define “object code.”
• The source code after it has been translated into machine
language.
Fundamentals of programming 63
Review Questions
5. Define “linker.”
• A program that combines one or more object files with a set of
libraries and produces an executable program.
6. Define “executable program.”
• A machine dependent file that contains all the instructions
necessary to perform a task.
7. Assembly language versus machine language?
• Machine language is solely numbers. Assembly language uses
words to represent those numbers.
8. What does a compiler do?
• It takes a source file and transforms it into an object file. Note:
many “compilers” are actually wrappers that run a compiler and
a linker.
Fundamentals of programming 64
Review Questions
9. How is a compiler different from an assembler?
• One assembly instruction translates to one machine language
instruction. Assembly language is a low-level language.
• A compiler translates one statement into many instructions.
• Compilers are also machine-independent, while assemblers are
machine-dependent.
10. What are the two common forms of algorithms?
• Pseudocode
• Flowcharts
Fundamentals of programming 65
Worksheet One
For each of the problems below, write a pseduocode algorithm and draw a flow
chart
1. Receive a number from the keyboard and determine whether it is odd or even.
2. Obtain two numbers from the keyboard, and determine which is the larger of the two
numbers.
3. Enter three numbers and display them in ascending order from smallest to largest.
5. Find the average, maximum, minimum, and sum of three numbers given by the user.
6. Find the area of a circle where the radius is provided by the user.
8. Take an integer from the user and display the factorial of that number
Fundamentals of programming 66
9. Computes sum from 1 to 100(sum=1+2+3….+100).
The End !
Question ?
Fundamentals of programming 67