0% found this document useful (0 votes)
19 views67 pages

Ch1-Introduction To C++

Chapter One covers the fundamentals of programming, including the elements of a computer system, types of programming languages, and programming paradigms. It explains the roles of hardware and software, the software development life cycle, and the importance of algorithms and pseudocode. The chapter also contrasts procedural and object-oriented programming methodologies, highlighting their respective advantages and limitations.

Uploaded by

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

Ch1-Introduction To C++

Chapter One covers the fundamentals of programming, including the elements of a computer system, types of programming languages, and programming paradigms. It explains the roles of hardware and software, the software development life cycle, and the importance of algorithms and pseudocode. The chapter also contrasts procedural and object-oriented programming methodologies, highlighting their respective advantages and limitations.

Uploaded by

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

Chapter One

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?

 Computer is derived from the word “Compute” which means


calculate

 Computer is considered to be a calculating device that can


perform arithmetic operations at enormous speed

 Computer is an electronic device capable of performing


commands
INPUT PROCESS OUTPUT

 The basic commands that a computer performs:


 input (get data)
 performance of arithmetic and logical operations
 output (display result) andFundamentals of programming 3

Elements of A Computer System

 Hardware
 The physical piece of a computer can be
touched and seen

 Central Processing Unit (CPU)


 Main memory….Random Access Memory (RAM)
 Input/output devices
 Secondary storage

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

 Software is a collection of instructions that enable


the user to interact with a computer

 Without software, computers would be useless

 Software is a generic term for organized collections of


computer data and instructions

 Categorized: System
Fundamentals& Application SW
of programming 5
System Software  Operating Systems
 Compiler,
Interpreter
 Linker, Loader
 Device drivers

 Responsible for controlling, integrating, and managing

the individual hardware components of a computer


system
 The system program that loads first when you turn on

your PC is called the operating system


 The operating system monitors the overall activity of

the computer and provides services


 Some of these services include memory
Fundamentals of programming 6
management, input/output activities, and storage
Application Software 

Word Processor
Spread Sheets
 DBMS
 Game Software

 It is designed to allow the user of the system


complete a specific task or set of tasks
 It refers to programs that enable the user to be
productive when using the computer
 They include programs such as web browsers,
office software, games and so on
 Operating system is the program that runs
application programs

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)

 Source code is written in one of programming


languages
Fundamentals of programming 9
Programming Language
 A programming language is an artificial language that
can be used to control the behavior of a machine,
particularly a computer
 Consists of words, symbols, and rules for writing a
program
 Programming languages, like human languages, are
defined through the use of syntactic and semantic rules,
to determine structure and meaning respectively
– A set of rules and symbols used to construct a computer
program
– A language used to interact with the computer
Syntax – Specifies legal instructions
– the rules that control the structure of the symbols,
punctuation, and words of a programming
language Fundamentals of programming 10
Reasons to study programming

 To provide instructions to a computer.


 Increased capacity to express programming concepts.
 Increased ability to learn and design new languages.
 Understand the significance of implementation.
 Overall advancement of computing.

Some Benefits of a Computer Program


Used to develop graphics and special effects in movie making.
Used to perform Ultrasounds, X-Rays, and other Medical examinations.
Used in our Mobile phones for SMS, Chat, and Voice communication.

Fundamentals of programming 11
Types of computer programming language

Programming Language

Low-level Languages High-level Languages

Machine Language Assembly 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

Compiler converts it before the execution of the program


Translation, linking and loading is done before the execution.
Program Execution
Steps taken by the CPU to run a program: Fetch an instruction=>Decode (interpret) the instruction=>Execute
(perform) actual processing=>Store the results, if needed

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

 Paradigms differ in concepts and abstractions


used to represent the elements of program

 Two popular approaches


1. Procedural Programming
2. Object-orientedFundamentals
Programmingof programming 21
Procedural Programming

• It contains a systematic order of statements, functions and


commands to complete a computational task or program
• It uses a list of instructions to tell the computer what to do
step-by-step.
• Procedures also known as routines or subroutines.
• A procedure contains a series of computational steps to be
carried out.
 The building-block of this type program is the procedure
or function
• Procedural programming languages are also known as top-
down languages.
Fundamentals of Programming 22
Procedural Programming
• If you want a computer to do something, you
should provide step-by-step instructions on how
to do it.
• Therefore most of the early programming
languages are all procedural.
• Examples: Some procedural languages include
Fortran, COBOL, C and BASIC (Beginners All-
purpose Symbolic Instruction Code), which have
been around since the 1960s and 70s.

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

• Where all computations are carried out using objects.


• Objects contain data in the form of attributes and code in the form of
methods.
• An object is a component of a program that knows how to perform certain
actions and how to interactFundamentals
with other elements of the program.
of programming 26
…continued
 Object Example:-
Person:
 Name  Property
 To do something(E.g. Waking)  Method
• A method in object-oriented programming is like a
procedure in procedural programming.
• The key difference here is that the method is part of an
object.
• In object-oriented programming, you organize your code
by:-
– Creating objects
– Give those objects properties
– Make them do certain things
Fundamentals of Programming 27
…continued

• A key aspect of object-oriented programming is the use of


classes.
• A class is a blueprint of an object.
• Let's say, you want to use a person in your program. You want to
be able to describe the person and have the person do
something.
• A class called 'person' would provide a blueprint for what a
person looks like and what a person can do.
• Examples of object-oriented languages include C#, Java, Perl and
Python.
Fundamentals of Programming 28
Procedure Oriented Programming Object Oriented Programming
Divided Into In POP, program is divided into small In OOP, program is divided into parts
parts called functions. called objects.
Importance In POP, Importance is not given In OOP, Importance is given to the data
to data but to functions as well rather than procedures or functions
as sequence of actions to be done. because it works as a real world.
Approach POP follows Top Down approach. OOP follows Bottom Up approach.
Access Specifiers POP does not have any access specifier. OOP has access specifiers named Public,
Private, Protected, etc.
Data Moving In POP, Data can move freely from In OOP, objects can move and
function to function in the system. communicate with each other through
member functions.
Expansion To add new data and function in POP is OOP provides an easy way to add new
not so easy. data and function.
Data Access In POP, Most function uses Global data In OOP, data can not move easily from
for sharing that can be accessed freely function to function, it can be kept public
from function to function in the system. or private so we can control the access of
data.
Data Hiding POP does not have any proper way for OOP provides Data Hiding so
hiding data so it is less secure. provides more security.
Overloading In POP, Overloading is not possible. In OOP, overloading is possible in the form
of Function Overloading and Operator
Overloading.
Examples Example of POP are : C, VB, FORTRAN, Example of OOP are : C++, JAVA, VB.NET,
Fundamentals of Programming 29
Pascal, BASIC. C#, .NET, PHP, JavaScript.
Software Development Life Cycle (SDLC)
The Software Development Life Cycle (SDLC) is an efficient
process for designing and developing high-quality software. The goal
of SDLC is to minimize risks through initial planning to ensure that the
software meets the customer's expectations during development and
other stages.
Stages of SDLC
The SDLC includes a series of stages that represent the sequence of
steps required to move from a concept to an end result.
Fundamentals of programming 30
Stages of SDLC
Collecting and analyzing requirements:
 Feasibility study.
 Define the specific features and functionalities of software.
 Prepare document that sets expectations and defines overall goals.
Planning and analysis:
 It involves defining the project scope & goals. & will serve as a blueprint for the system.
 Estimating the needed resources;
 Analyzing cost, profitability, implementation time, and risks;
 Developing a work schedule, etc.
Design/Prototyping:
 Creating a detailed design of the software architecture.
 It needs to develop an Algorithm, Flow charts, and Pseudo codes.
 It will serve as a blueprint for the system.
Development/Implementing/Coding:
 Developers write the actual code for the software product.
Testing:
 Verifying that the software meets all of the requirements and functions as intended.
Deployment:
 Finally ready for deployment & to win your users’ hearts.
Maintenance:
 It involves ongoing maintenance and updates to the software
Fundamentals of programming 31
Algorithm Development and
Representation
 An algorithm is defined as a step-by-step sequence of instructions that must terminate
and describe how the data is to be processed to produce the desired outputs
 A procedure or formula for solving a problem
 Often used for calculation, data processing and programming
 Algorithms can be expressed in any language
 Two common forms of algorithm are flow chart and Pseudocode.
 Algorithms for making things will often be divided into sections
– The parts/components/ingredients(inputs)required to accomplish the task
– Actions/steps/methods(processing)to produce the required out come(output)

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:

 Precise and unambiguous (no ambiguity in any instruction


and in the order of execution)
 Simple
 General (one inch is equal to 2.54cm is not an algorithm,
it has to convert a supplied number of inches)
 Correct
 Finite (has to have an end)
 Handles all exceptions
 Produce expected output
 Efficient: in time, memory and other resources

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.

The possible Pseudocode algorithm:

Step 1: Prompt the user to enter the first integer


Step 2: Prompt the user to enter a second integer
Step 3: Compute the sum of the two user inputs
Step 4: Display an output prompt that explains the answer as the sum
Step 5: Display the result

Fundamentals of programming 37
Example 2:
Write a Pseudocode algorithm that finds average of any three
numbers.

The possible pseudocode algorithm:

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.

The possible pseudocode algorithm:

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

• Used to represent the


Start/End beginning & the end of a
task End
• Used for input and output Print
Input/ operations C
Output
• Used for arithmetic and C=A+B
Process data manipulation
operations If
C>50
Decision • Used for any logic and
comparison operations Yes No
Print Print
Pass Fail

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

E.g. Enter an integer: 5

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

InPutCen CalcFar OutPutFar

Fundamentals of programming 60
Translating algorithms to programming languages

To translating algorithms to programming languages


you must follow the following steps.
Understand the problem
Do a small example by hand
Write an algorithm to solve the problem
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.

4. Find the average of two even numbers given by the user.

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.

7. Swap the contents of two variables using a third variable.

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

You might also like