Chapter Two - Algorithms and Data Structures

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 57

Programming I (in C++)

Chapter 2:
Algorithms and Data Structures

FOP -- Chapter One -- Introduction 1


06/27/2024
Outline of Topics

◦ What is an algorithm?
◦ Writing Algorithms
◦ Translation of algorithms to flowcharts
and pseudo code
◦ Data Structures and computer programs

FOP -- Chapter One -- Introduction 2


06/27/2024
2.1. What is an algorithm?
Performing a Task on The Computer
 Step 1: Determine what the output should be
- that is, exactly what the task should produce.
 Step 2: Identify the data, or input, necessary
to obtain the output.
 Step 3: Determine how to process the input to
obtain the desired output, that is, to determine
what formulas or ways of doing things can be
used to obtain the output.

FOP -- Chapter One -- Introduction Lemma N. 3


06/27/2024
2.1. What is an algorithm?
Program Development Cycle
1. Analyze: Define the problem.
 Be sure you understand what the program
should do, that is, what the output should be.
Have a clear idea of what data (or input) are
given and the relationship between the input
and the desired output.
.

FOP -- Chapter One -- Introduction Lemma N. 4


06/27/2024
2.1. What is an algorithm?
Program Development Cycle
2. Design: Plan the solution to the problem.
 Find a logical sequence of precise steps that
solve the problem.
Such a sequence of steps is called an
algorithm.
Every detail, including obvious steps, should
appear in the algorithm.

FOP -- Chapter One -- Introduction Lemma N. 5


06/27/2024
2.1. What is an algorithm?
Program Development Cycle
Algorithm can be defined as:

“A sequence of activities to be processed for


getting desired output from a given input.”

FOP -- Chapter One -- Introduction Lemma N. 6


06/27/2024
2.1. What is an algorithm?
Program Development Cycle
3. Choose the interface: Select the objects.
 Determine how the input will be obtained and
how the output will be displayed. Then create
appropriate commands to allow the user to
control the program.

FOP -- Chapter One -- Introduction Lemma N. 7


06/27/2024
2.1. What is an algorithm?
Program Development Cycle
4. Code: Translate the algorithm into a
programming language.
 Coding is the technical word for writing the
program.

FOP -- Chapter One -- Introduction Lemma N. 8


06/27/2024
2.1. What is an algorithm?
Program Development Cycle
5. Test and debug: Locate and remove any
errors in the program.
 Testing is the process of finding errors in a
program (Trace the errors …), and
 Debugging is the process of correcting errors
that are found. (An error in a program is called
a bug.) (Fix the errors …)

FOP -- Chapter One -- Introduction Lemma N. 9


06/27/2024
2.1. What is an algorithm?
Program Development Cycle
Types of Errors
Syntax Errors: Violation of syntactic rules in
a Programming Language generates syntax
errors.
Effect? Interpreter or Compiler finds it in
Syntax Check Phase.

FOP -- Chapter One -- Introduction Lemma N. 10


06/27/2024
2.1. What is an algorithm?
Program Development Cycle
Types of Errors
Semantic Errors: Doing logical mistakes
causes semantic errors in Source code.
Effect? Interpreters and Compilers can not
notice them, but on execution, they causes
unexpected results.

FOP -- Chapter One -- Introduction Lemma N. 11


06/27/2024
2.1. What is an algorithm?
Program Development Cycle
Types of Errors
Run-time Errors: Occur on program
execution. Mostly caused by invalid data entry
or tries to use not existing resources.
Effect? It occurs on run time and may crash
the program execution.

FOP -- Chapter One -- Introduction Lemma N. 12


06/27/2024
2.1. What is an algorithm?
Program Development Cycle
6. Complete the documentation: Organize all
the material that describes the program.
 Documentation is intended to allow another
person, or the programmer at a later date, to
understand (and modify) the program.

FOP -- Chapter One -- Introduction Lemma N. 13


06/27/2024
1.2. Writing algorithms

Programming Tools: are used to convert


algorithms into computer programs:
A) Pseudocode: An informal high-level
description of the operating principle of a
computer program.
 It uses the structural conventions of a
programming language, but is intended for
human reading rather than machine reading.

FOP -- Chapter One -- Introduction 14


06/27/2024
1.2. Writing algorithms

B) Flowcharts: Graphically depict the logical


steps to carry out a task and show how the steps
relate to each other.

FOP -- Chapter One -- Introduction 15


06/27/2024
1.2. Writing Algorithms

Pseudocode Flowchart
Artificialand Informal • A graphical way of
language writing pseudocode
 Helps programmers to • Rounded rectangle -
plan an algorithm terminal
 Similar to everyday
• Parallelogram-input /
output
English
• Rectangle - actions
 Not an actual
• Diamonds - decision
programming language /conditional
• Circles - connector
FOP -- Chapter One -- Introduction 16
06/27/2024
1.2. Writing Algorithms
Flowchart Symbols

Oval Used to connect multiple flows in a


flowchart.

FOP -- Chapter One -- Introduction 17


06/27/2024
1.3. Translation

Once an algorithm is defined for a problem,


the programmer needs to translate it to its
equivalent representation using pseudocode or
flowchart.

Easierto translate this representation to a


programming language construct.

FOP -- Chapter One -- Introduction 18


06/27/2024
1.3. Translation

Example:
Write an algorithm that receives two numbers
from an input device and returns their sum.
Translate the algorithm to a pseudocode and a
flowchart.

FOP -- Chapter One -- Introduction 19


06/27/2024
1.3. Translation

Algorithm:
Begin
Step 1: Receive two integers
Step 2: Calculate their sum
Step 3: Display sum
End

FOP -- Chapter One -- Introduction 20


06/27/2024
1.3. Translation
Pseudocode Flowchart
Start
Read A, B
Calculate C=A+B
Display C
Stop

FOP -- Chapter One -- Introduction 21


06/27/2024
1.3. Translation

The C++ code …


#include<iostream>
using namespace std;
int main()
{
float A, B, C;
cin>>A,B;
C=A+B;
cout<<C;
return 0;
}

FOP -- Chapter One -- Introduction 22


06/27/2024
1.3. Translation
Variations in Structures of a program
Bohm Corrado and Guiseppe Jacopini
(inventors of Structured programming)
demonstrated that any computer program can be
written with just three structures:
◦ Sequences,
◦ Decisions, and
◦ Loops

FOP -- Chapter One -- Introduction 23


06/27/2024
1.3. Translation

Sequences: one
command is
executed after
previous one.

FOP -- Chapter One -- Introduction 24


06/27/2024
1.3. Translation

Decisions (selections): Statement(s) is (are)


executed if certain condition gives TRUE or
FALSE value.

FOP -- Chapter One -- Introduction 25


06/27/2024
1.3. Translation

Three types of Decision Structures


◦ IF Structure

FOP -- Chapter One -- Introduction 26


06/27/2024
1.3. Translation

◦ IF … ELSE Structure

FOP -- Chapter One -- Introduction 27


06/27/2024
1.3. Translation

◦ Switch Structure

FOP -- Chapter One -- Introduction 28


06/27/2024
1.3. Translation

Loops (repetition): Statement(s) is (are)


executed repeatedly until certain condition
gives TRUE or FALSE value.

FOP -- Chapter One -- Introduction 29


06/27/2024
1.3. Translation

Three kinds of repetition structures


◦ While

FOP -- Chapter One -- Introduction 30


06/27/2024
1.3. Translation

Three kinds of repetition structures


◦ Do … While

FOP -- Chapter One -- Introduction 31


06/27/2024
1.3. Translation

Three kinds of repetition structures


◦ For statement

FOP -- Chapter One -- Introduction 32


06/27/2024
1.3. Translation

Pseudocode and Flowchart for a Decision


Structure For statement

FOP -- Chapter One -- Introduction 33


06/27/2024
1.3. Translation

Example:
 Write an algorithm that receives two non-equal
numbers and outputs the maximum.

FOP -- Chapter One -- Introduction 34


06/27/2024
1.3. Translation

Pseudocode

Start
Read A, B
Max = A
If (B > A)
Max = B
Display Max
Stop

FOP -- Chapter One -- Introduction 35


06/27/2024
1.3. Translation

Example:
 Write an algorithm to determine a student’s
average grade and indicate whether he is
successful or not.
 The average grade is calculated as the average
of mid-term and final marks.
 Student will be successful if his average grade
is grater or equals to 60.

FOP -- Chapter One -- Introduction 36


06/27/2024
1.3. Translation
Pseudocode
Start
Use variables mid term, final and average
Input mid term and final
Calculate the average by summing mid term and final
and dividing by 2
If average is below 60
Print “FAIL”
else
Print “SUCCESS”
Stop
FOP -- Chapter One -- Introduction 37
06/27/2024
1.3. Translation
Pseudocode (Detailed/Simplified!)
Start
Input mid-term and final
Average = (mid-term + final)/2
If (average < 60) then
Print “FAIL”
else
Print “SUCCESS”
Endif
Stop

FOP -- Chapter One -- Introduction 38


06/27/2024
1.3. Translation
Flowchart

FOP -- Chapter One -- Introduction 39


06/27/2024
1.3. Translation
Pseudocode and Flowchart for a Loop

FOP -- Chapter One -- Introduction 40


06/27/2024
1.3. Translation
Example:
Write an algorithm which calculates the average exam
grade for a class of 5 students.

What are the program inputs?


• The exam grades
Processing:
 Find the sum of the grades;
 Count the number of students; (counter
controlled);
 Calculate average grade = sum of grades /
number of students.
What is the program output?
 The average exam grade
FOP -- Chapter One -- Introduction 41
06/27/2024
1.3. Translation
Pseudocode

Start
Use variables total, counter, grade, average
Initialize total = 0
Initialize counter = 1
While (counter <= 5)
Input grade
Calculate total = total + grade
Calculate counter = counter + 1
End-while
Calculate average = total / 5
Display average
Stop
FOP -- Chapter One -- Introduction 42
06/27/2024
1.3. Translation
Flowchart

FOP -- Chapter One -- Introduction 43


06/27/2024
1.3. Translation
Reading assignment …

Properties of algorithms
• Finiteness
• Definiteness
• Input
• Output
• Effectiveness

FOP -- Chapter One -- Introduction 44


06/27/2024
1.4. Data Structures
There are often many approaches to solving a
problem. How do we choose
between them?
At the heart of computer program design are
two goals:
 To design an algorithm that is easy to
understand, code, and debug.
 To design an algorithm that makes efficient
use of the computer’s resources.

FOP -- Chapter One -- Introduction 45


06/27/2024
1.4. Data Structures
Data structure is the structural representation
of logical relationships between elements of
data.

Aggregation of atomic and composite data


into a set with defined relationships.

Structure refers to a set of rules that hold the


data together.

FOP -- Chapter One -- Introduction 46


06/27/2024
1.4. Data Structures

Data structure …
 mainly specifies the structured
organization of data, by providing accessing
methods with correct degree of
associatively.
 affects the design of both the structural and
functional aspects of a program.

Algorithm + Data Structure = Program

FOP -- Chapter One -- Introduction 47


06/27/2024
1.4. Data Structures

Example

FOP -- Chapter One -- Introduction 48


06/27/2024
1.4. Data Structures

Role of Data Structure …

Data structures organize data


 more efficient programs.
More powerful computers
 more complex applications.
More complex applications demand more
calculations.
Complex computing tasks are unlike our
everyday experience.
FOP -- Chapter One -- Introduction 49
06/27/2024
1.4. Data Structures

Any organization for a collection of records


can be searched, processed in any order, or
modified.

The choice of data structure and algorithm


can make the difference between a program
running in a few seconds or many days.

FOP -- Chapter One -- Introduction 50


06/27/2024
1.4. Data Structures

Efficiency

A solution is said to be efficient if it solves


the problem within its resource constraints.
• Space
• Time
The cost of a solution is the amount of
resources that the solution consumes.

FOP -- Chapter One -- Introduction 51


06/27/2024
1.4. Data Structures

Selecting a Data Structure

Select a data structure as follows:


1) Analyze the problem to determine the
basic operations that must be supported.
2) Quantify the resource constraints for each
operation.
3) Select the data structure that best meets
these requirements.

FOP -- Chapter One -- Introduction 52


06/27/2024
1.4. Data Structures

Cost and Benefit


Each data structure has costs and benefits.
Rarely is one data structure better than
another in all situations.
Any data structure requires:
• space for each data item it stores,
• time to perform each basic operation,
• programming effort.

FOP -- Chapter One -- Introduction 53


06/27/2024
CYP

1) What is an algorithm?
2) Explain the need for writing an algorithm?
3) What are the benefits of developing
flowchart/pseudocode?
4) Discuss decision structure and looping.
5) What is Data Structure?
6) Explain the role of data structures in
programming.

FOP -- Chapter One -- Introduction 54


06/27/2024
Problems

Prepare an algorithm, Pseudocode and


Flowchart.

1) Find average age of a group of 10 players


2) Calculate the area of a circle.
3) Convert temperature from Fahrenheit to
Celsius.
4) Find the largest value of any three numbers.
5) Calculate even numbers between 0 and 99.

FOP -- Chapter One -- Introduction 55


06/27/2024
Problems

6) Calculate the factorial of an integer n.


7) Generate the first 50 items of the Fibonacci
series.
8) Read the coefficients of a quadratic equation
and decide the solution/s if any.

FOP -- Chapter One -- Introduction 56


06/27/2024
Next …

Chapter 3: Programming Constructs

FOP -- Chapter One -- Introduction 57


06/27/2024

You might also like