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

Class 1

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

Class 1

comp
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Introduction to

Algorithms &
Programming I
COMP 1400-1
Fall 2019

Instructor: Dr. Pooya Moradian Zadeh


[email protected]
1
May not be replicated without permission
ntroduction to Algorithms & Programming I

ALGORITHM AND FLOWCHART


Example 6:
• ESSENCE OF Get an integer value (n) from the user and display all even numbers
PROBLEM-
SOLVING between 1 to n
Sample Input: Sample output:
10 2 4 6 8 10
• ALGORITH 21 2 4 6 8 10 12 14 16 18 20
M Algorithm 6

For Step1: Start


1: Start
i=1,2,3, Step2: Get an integer number say n 2: Read (n)
…, n
repeat the Step3: For i= 1 to n Do 3: For i  1 to n Do
loop Step4: If (i%2=0) Then 4: If (i%2=0) Then
Step5: Display (i ) 5: Print (i )
Step6: End IF 6: End If
Step7: End For 7: End For
Step8: Stop 8: Stop

2
ntroduction to Algorithms & Programming I

ALGORITHM AND FLOWCHART


Example 6:
• ESSENCE OF Get an integer value (n) from the user and display all even numbers
PROBLEM-
SOLVING between 1 to n
Sample Input: Sample output:
10 2 4 6 8 10
• ALGORITH 21 2 4 6 8 10 12 14 16 18 20
Algorithm 6_1
M
While the Step1: Start
1: Start
condition
Step2: Get an integer number say n 2: Read (n)
is true,
repeat the Step3: While (n >= 2) Do 3: While (n >=2) Do
loop Step4: If (n%2==0) Then 4: If (i%2==0) Then
Step5: Display (i ) 5: Print (i )
Step6: End IF 6: End If
Step7: n = n/2 7: n  n/2
Step7: End While 7: End While
Step8: Stop 8: Stop 3
ntroduction to Algorithms & Programming I

ALGORITHM AND FLOWCHART


Example 6:
• ESSENCE OF Get an integer value (n) from the user and display all even numbers
PROBLEM-
SOLVING between 1 to n
Sample Input: Sample output:
10 2 4 6 8 10
• ALGORITH 21 2 4 6 8 10 12 14 16 18 20
Algorithm 6_2
M
Step1: Start 1: Start
2: Read (n)
Step2: Get an integer number say n
3: i=2
Step3: i=2
4: While (n >=i) Do
Step4: While (n >= i) Do
5: Print (i )
Step5: Display (i )
6: i  i+2
Step6: i= i+2
7: End While
Step7: End While
8: Stop
Step8: Stop

4
ntroduction to Algorithms & Programming I

ALGORITHM AND FLOWCHART


Flowchart Begi
• ESSENCE OF A Begin or End n
End
PROBLEM- A graphical representation
SOLVING
of an algorithm or a
• ALGORITH process

M Read Write
Process Documentation Input/Output
• FLOWCHA
Better Communication
RT
Useful for Analysis Process
Process
Helpful in Debugging

Decision

5
ntroduction to Algorithms & Programming I

ALGORITHM AND FLOWCHART


Begi
• ESSENCE OF
The process starts
n
PROBLEM-
SOLVING

Read (msg) Read from input and store it in a variable (msg


• ALGORITH

M
Print (msg) Display the content (value) of the variable msg
• FLOWCHA

RT
End End of the process

1: Start
2: Read (msg)
3: Print (msg)
4: End 6
ntroduction to Algorithms & Programming I

ALGORITHM AND FLOWCHART


Example
Begin
• ESSENCE OF
PROBLEM-
SOLVING
Algorithm1 Read
• ALGORITH 1: Start (Num1)

M 2: Read (Num1) Read


(Num2)
• FLOWCHA 3: Read (Num2)

4: sum Num1 + Num2 sum  Num1 +


RT
Num2
5: Print (“The sum of the given numbers is”,
sum”.”) Print (“The sum of
the given numbers
6: Stop is”, sum ”.”)

End
7
ntroduction to Algorithms & Programming I

ALGORITHM AND FLOWCHART


Example Begin
• ESSENCE OF
PROBLEM-
SOLVING
Algorithm1_1 Read (Num1,
• ALGORITH 1: Start Num2)

2: Read (Num1, Num2)


M
3: sum Num1 + Num2 sum  Num1 +
Num2
• FLOWCHA 4: Print (“The sum of the given numbers is”,
sum”.”)
RT 5: Stop Print (“The sum of
the given numbers
is”, sum ”.”)

End

8
ntroduction to Algorithms & Programming I

ALGORITHM AND FLOWCHART


Algorithm 4

Begin
ESSENCE OF 1: Start
PROBLEM-
SOLVING 2: Read (a, b)
Read
3: If a<b then (a,b)
• ALGORITH
4: print “The smallest number is”, a
M 5: Else No Yes
a<b
• FLOWCHA 6: print “The smallest number is”, b
7: End IF
RT
8: Stop Print (“The Print (“The
smallest number smallest number
is”, b) is”, a)

End
9
ntroduction to Algorithms & Programming I

ALGORITHM AND FLOWCHART

• ESSENCE OF Decision Structure IF Then


PROBLEM- Else
SOLVING

• ALGORITH
No Yes
M

• FLOWCHA

RT Process B Process A

10
ntroduction to Algorithms & Programming I

ALGORITHM AND FLOWCHART

• ESSENCE OF Decision Structure IF Then


PROBLEM-
SOLVING

• ALGORITH
No Yes
M

• FLOWCHA

RT
Process A

11
ntroduction to Algorithms & Programming I

ALGORITHM AND FLOWCHART

• ESSENCE OF Loop Structure


PROBLEM-
SOLVING

• ALGORITH
Yes
M

• FLOWCHA
No
RT
Process A

12
• ESSENCE OF Example Begin
PROBLEM-
SOLVING
Read
Algorithm 5 (a,b,c)
• ALGORITH 1: Start
2: Read (a, b,c) small  a
M
3: small  a
• FLOWCHA Yes
4: If small>b Then small > b
RT 5: small  b small  b
No
6: End If
7: If small >c Then small > c
Yes
8: small  c
No small  c
9: End If
10: Print “The smallest number is”, small
Print (“The
11: Stop smallest number
is”, b)

End
13
ntroduction to Algorithms & ProgrammingALGORITHM
I AND FLOWCHA

Begin

Algorithm 6
• ESSENCE OF 1: Start Read (n)
PROBLEM-
Main components of a For loop
SOLVING 2: Read (n)
i 1
3: For i  1 to n Do
• ALGORITH
4: If (i%2==0) Then
M No Yes
i
5: Print (i) i  i+ 1
• FLOWCHA <=n
6: End If
RT
i Yes
7: End For
%2==0
8: Stop No
Print (i)

End
14
ntroduction to Algorithms & ProgrammingALGORITHM
I AND FLOWCHA

Begin

Algorithm 6_2
• ESSENCE OF 1: Start Read (n)
PROBLEM-
SOLVING 2: Read (n)
i 2
3: i=2
• ALGORITH
4: While (n >=i) Do Yes
M No n
5: Print (i ) >=i
• FLOWCHA
6: i  i+2 Print (i)
RT
7: End While

8: Stop i  i+ 2

End
15
ntroduction to Algorithms & Programming I

ALGORITHM AND FLOWCHART

• ESSENCE OF
PROBLEM-
SOLVING

• ALGORITH
How To Write Algorithms
M
ntroduction to Algorithms & Programming I

ALGORITHM AND FLOWCHART

• ESSENCE OF Define the problem


PROBLEM-
SOLVING • State the problem clearly and concisely
• ALGORITH

M List the inputs and outputs

Describe the steps needed to process the inputs to produce outputs

• First, start at a high level view,


• Then refining the steps.
• Get the inputs
• Define the variables
• Output the results

Test the algorithm

You might also like