0% found this document useful (0 votes)
7 views

CH 1

Uploaded by

Solomon Asfaw
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

CH 1

Uploaded by

Solomon Asfaw
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

Bridge International College

Department Computer science

Course title: Fundamentals of programming in C++

Credit hours :3

Year :II

S :-N.T
10/29/2024 Year 2017
Bridge International College

ASSESSMENT METHODS
Assignment 10%
Lab exam 20%
Test/mid semester exam 20%
Final exam 50%

10/29/2024
Bridge International College

Text book
Ravichandran,”problem solving with C++”Tata
Msc.Grew Hill Company.
References
Brian W.Kernighan,Dennis M. Ritchie, “The C
programming Language ”,Prentice Hall of India
Problem_Solving_with_C++_(9th_Savitch)

10/29/2024
Introduction
What is a Computer?

 Device capable of performing computations and making logical decisions.

 process data under the control of sets of instructions called computer programs

•Hardware :-Various devices comprising a computer – Keyboard, screen, mouse,

disks, memory, CD-ROM, and processing units.

 Software :-Programs that run on a computer (operation systems, application programs)

– Structured programming, top-down stepwise refinement, functionalization, and

object-oriented programming .

10/29/2024 N.T
Some Terminology
Binary Digits (bit):- 1 and 0 .
 The computer can combine the two digital states to represent letters, numbers, colors, sounds, images,
shapes, and even odors.
 An “on” or “off” electronic state is represented by a bit, short for binary digit.
Encoding Systems: Bits and Bytes .
 Bits are combined according to an encoding system to represent letters, numbers, and special characters,
collectively referred to as alphanumeric characters .
 The combination of bits used to represent a character is called a byte (Binary Term, 8 bits/byte)
 8 bits = 1byte •
Representation of a Character .
 ASCII (American Standard Code for Information Interchange) is the most popular encoding system for PCs
and data communication
 ASCII – 7 bits
 ANSI – 8 bits/byte
 UNICODE – 16 bits
 Big5 – 16 bits
Storage Capacities
-
-
-
- 10/29/2024
Computer Organization
Five logical units in every computer:
1. Input Unit:-Obtains information from input devices (keyboard, mouse, scanner)
2. Output Unit :-Outputs information (to screen, to printer, to speakers, to projector, to
control other devices)
3. CPU (Central Processing Unit) .
 Arithmetic and Logic Unit (ALU):- Performs arithmetic calculations and logic decisions.
 Control Unit (CU) :-
o Execute programs/instructions.
o Supervises and coordinates the other sections of the computer.
o Move data from one memory location to another
4. Memory Unit :-Rapid access, low capacity, stores input and output information
5. Secondary Storage Unit • Cheap, long-term, high-capacity storage (e.g., Hard Disks,
Memory Sticks)
Stores inactive programs

10/29/2024
Machine Languages, Assembly Languages, and High-level Languages

Machine languages :-Strings of numbers giving machine specific instructions.


 Is low level programming language.
 Instruction executed directly by a computer's CPU without
the need for translation or interpretation.
Assembly languages :-English-like abbreviations representing elementary computer operations
(translated via assemblers)
Example: LOAD BASEPAY
ADD OVERPAY
STORE GROSSPAY.
High-level languages:-Codes similar to everyday English.
Use mathematical notations (translated via compilers).Example: grossPay = basePay + overTimePay
Example C++,PYTHON,JAVA,COBOL.
High-Level Languages
|
Assembly Language

Machine Language
10/29/2024
Problem Solving Techniques

A typical programming task can be divided into two phases:


Problem solving phase
◦ produce an ordered sequence of steps that describe solution of problem
◦ this sequence of steps is called an algorithm
Implementation phase
◦ implement the program in some programming language.(example C++)
Steps in Problem Solving
 First produce a general algorithm (one can use pseudo code)
 Refine the algorithm successively to get step by step detailed algorithm that is very close
to a computer language.
 Pseudo code is an artificial and informal language that helps programmers develop
algorithms. Pseudo code is very similar to everyday English.

10/29/2024
Problem Solving Techniques

10/29/2024
Algorithm, pseudo code and Flow Chart
Algorithm
 A set of step-by-step instructions to accomplish a task.
 In mathematics, computer science, and related subjects, an algorithm is a finite
sequence of steps expressed for solving a problem
 A process that performs some sequence of operations in order to solve a given
problem
 Algorithms are used for calculation, data processing, and many other fields.
 In computing, algorithms are essential because they serve as the systematic
procedures that computers require.

10/29/2024
reasons for using algorithms

Three reasons are:-Efficiency, Abstraction and reusability.

A. Efficiency: Certain types of problems, like sorting, occur often in computing.


Efficient algorithms must be used to solve such problems considering the time and cost
factor involved in each algorithm.

B. Abstraction: Algorithms provide a level of abstraction in solving problems because


many seemingly complicated problems can be distilled into simpler ones for which well
known algorithms exist.

C. Reusability: Algorithms are often reusable in many different situations.

10/29/2024
Characteristics of algorithm

 must have start instruction .


 Each instruction must be precise.
 Each instruction must be unambiguous.
 Each instruction must be executed in finite time.
 must have stop instruction(Termination).

10/29/2024
Algorithm Example 1
1. Suppose you are given a set of 1. Start
mark sheets where each mark sheet 2. Take a mark sheet
bears A, B, C or F letter grades. • and read the grade.
Write an algorithm to read mark  3. Print the grade
sheet and print the grade that it  4. Stop
contains.

10/29/2024
Example 2
Suppose you are given a set of mark 1. Start
sheets where each mark sheet bears
2. Take a mark sheet and read the
A, B, C or F grades. Write an
grade.
algorithm to read a mark sheet and
3. If grade is A then Print the grade
print the grade if the grade is A only.
4. Stop

10/29/2024
Algorithm representation
 A pseudo code
 A flowchart

Programs statements in a programming language .

10/29/2024
Pseudo Code

 Pseudo code is another program analysis tool that is used for planning program
logic.
 "Pseudo" means imitation or false •
 "Code" refers to the instructions written in a programming language.
 Pseudo code, therefore, is an imitation of actual computer instructions.
 These pseudo instructions are phrases written in ordinary natural language (e.g.,
English).

10/29/2024
Pseudo code Structure
 Pseudo code is made up of the following basic logic structures that have
been proved to be sufficient for writing any computer program.

A. Sequence

B. Selection (IF...THEN...ELSE or IF....THEN) .

C. Iteration (DO...WHILE or REPEAT...UNTIL)

10/29/2024
EX1:-Write an algorithm to determine a student’s final grade and indicate whether it is passing or failing. The final grade is calculated as the average of four marks.

ALGORITHM PSEUDO CODE

Step 1: InputM1,M2,M3,M4 1. Input a set of 4 marks


Step 2: GRADE 
(M1+M2+M3+M4)/4 2. Calculate their average by
Step 3: if (GRADE < 50) then summing and dividing by 4
3. if average is below 50
Print “FAIL”
4. Print “FAIL”
else
5. else
Print “PASS” 6. Print “PASS”
endif

10/29/2024
What is a Flowchart?
 A flowchart is a diagram that depicts the “flow” of a program.
 A Flowchart is a type of diagram (graphical or symbolic) that represents an
algorithm or process.
 Each step in the process is represented by a different symbol and contains a
short description of the process step.
 The flow chart symbols are linked together with arrows showing the
process flow direction.
 A flowchart describes what operations (and in what sequence) are required
to solve a given problem.
 A Flowchart
o shows logic of an algorithm
o emphasizes individual steps and their interconnections
o e.g. control flow from one action to the next
.

10/29/2024
Con’t
 Flowcharts are used in:-
 Analyzing,
 Designing,
 Documenting or managing a process or program in various fields.
 Flowcharts are generally drawn in the early stages of formulating computer
solutions.
 Flowcharts often facilitate communication between programmers and
business people.
 These flowcharts play a vital role in the programming of a problem and are
quite helpful in understanding the logic of complicated and lengthy
problems.
 Once the flowchart is drawn, it becomes easy to write the program in any
high level language.

10/29/2024
Flowchart Symbols & Guidelines:
 Flowcharts are usually drawn using some standard symbols; however, some special
symbols can also be developed when required.
 Some standard symbols, which are frequently required for flowcharting many
computer programs are shown.

Terminator: An oval flow chart shape indicates the start or end of the process, usually
containing the word “Start” or “End”.

10/29/2024
Con’t
Process: A rectangular flow chart shape indicates a normal/generic process flow
step.
For example, “Add 1 to X”, “M = M*F” or similar.

Decision: A diamond flow chart shape indicates a branch in the process flow.
 This symbol is used when a decision needs to be made, commonly a Yes/No
question or True/False test.

10/29/2024
Con’t
Connector: A small, labelled, circular flow chart shape used to indicate a jump in the
process flow. Connectors are generally used in complex or multi-sheet diagrams.

Data: A parallelogram that indicates data input or output (I/O) for a process.
Examples: Get X from the user, Display X.

Delay: used to indicate a delay or wait in the process for input from some other process.

Arrow: used to show the flow of control in a process.


An arrow coming from one symbol and ending at another symbol represents that control
passes to the symbol the arrow points to.

10/29/2024
Basic guidelines for drawing a flowchart with the symbols are that:

 In drawing a proper flowchart, all necessary requirements should be listed out in


logical order.
 The flowchart should be neat, clear and easy to follow. There should not be any
room for ambiguity in understanding the flowchart.
 The flowchart is to be read left to right or top to bottom.
 A process symbol can have only one flow line coming out of it.
 For a decision symbol, only one flow line can enter it, but multiple lines can leave it
to denote possible answers.
 The terminal symbols can only have one flow line in conjunction with them.

10/29/2024
Con’t

10/29/2024
Algorithm flowchart

1. Read A, B
2. If A is less than B
3. BIG=B
4. SMALL = A
5. Else BIG=A SMALL = B
6. Write (Display) BIG,
SMALL

Write algorithm and flowchart that display the largest number between A and B

10/29/2024
Algorithm flowchart

1. Read X, Y, Z FLOW CHART


2. Compute Sum (S) as X + Y + Z

3. Compute Average (A) as S / 3


4. Compute Product (P) as X x Y
xZ
5. Write (Display) the Sum,
Average and Product
find the sum, average and product of 3 numbers given by the user.

10/29/2024
EX1:-Draw an a flow chart to determine a student’s final grade and indicate whether it is passing or failing. The final grade is calculated as the average of four
marks.

ALGORITHM Flow chart

Step 1: InputM1,M2,M3,M4 .
Step 2: GRADE 
(M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then

Print “FAIL”
else

Print “PASS”
endif
Print
“Pass”

10/29/2024
EX2:-Write an algorithm and draw a flowchart that will read the two sides of a rectangle and calculate its area.
.

ALGORITHM Flow chart

Step 1: start . START


Step 2: Input W,L
Step 3: A  L x W Input
W, L
Step 4: Print A
Step5 : stop
A LxW
Pseudo code?
Print
A

STOP

10/29/2024
flow chart structure
.

Sequence control structure example

. start
. Begin

Statement 1
Read birth date

Statement 2
Calculate
Age = current year – birth date

Statement 3
Display
age
:statement 4

End
end

10/29/2024
flow chart structure
.

Selection control structure example

. .

No Yes
Condition

else- then-
statement statement(s
(s) )

10/29/2024
flow chart structure
.

Iteration control structure example

. .

ye Loop
Condition s Statement
(s)

no

10/29/2024
When to Use a Flowchart
 To communicate to others how a process is done.
 A flowchart is generally used when a new project begins in order to plan for the
project.
 A flowchart helps to clarify how things are currently working and how they could
be improved.
 It also assists in finding the key elements of a process, while drawing clear lines
between where one process ends and the next one starts.
 Developing a flowchart stimulates communication among participants and
establishes a common understanding about the process. Flowcharts also uncover
steps that are redundant or misplaced.
 Flowcharts are used to help team members, to identify who provides inputs or
resources to whom, to establish important areas for monitoring or data collection, to
identify areas for improvement or increased efficiency, and to generate hypotheses
about causes.

10/29/2024
Advantages of Using Flowcharts:
 Communication: Flowcharts are better way of communicating the logic of a
system to all concerned.
 Effective analysis: With the help of flowchart, problem can be analyzed in more
effective way.
 Proper documentation: Program flowcharts serve as a good program
documentation, which is needed for various purposes.
 Efficient Coding: The flowcharts act as a guide or blueprint during the systems
analysis and program development phase.
 Proper Debugging: The flowchart helps in debugging process.
 Efficient Program Maintenance: The maintenance of operating program becomes
easy with the help of flowchart. It helps the programmer to put efforts more
efficiently on that part.

10/29/2024
Limitations of Using Flowcharts:
 Complex logic: Sometimes, the program logic is quite complicated. In that case,
flowchart becomes complex and clumsy.
 Alterations and Modifications: If alterations are required the flowchart may
require re-drawing completely.
 Reproduction: As the flowchart symbols cannot be typed, reproduction of
flowchart becomes a problem.
 The essentials of what is done can easily be lost in the technical details of how it is
done.

10/29/2024
Exercise
1. Create an algorithm and a flowchart that will compute the sum of two numbers. If
the sum is below or equal to ten, two numbers will be entered again. If the sum is
above 10, it will display the sum.
2. Create an algorithm and a flowchart that will compute the area of a circle.
3. Create an algorithm and a flowchart that will output the factorial of a given number.
4. Create an algorithm and a flowchart that will output the Fibonacci series up to a
given number.
5. Create an algorithm and a flowchart that will output all the prime numbers less than
20.
6. Create algorithm and flow chart that display odd,even,prime number less than 50
7. write an algorithm that display the below structure using
iteration(for ,while,do..while)
x
x x
x x x
x x x x
x x x x x
10/29/2024

You might also like