Double Revolving field theory-how the rotor develops torque
UNIT I_PSPP - Illustrative Problems (1).pptx
1. Knowledge Institute of Technology, Salem-637504
(Affiliated to Anna University, Chennai)
(Accredited by NAAC)
Department of Computer Science and Engineering
(Accredited by NBA)
Course
Code
GE8151 Course Name
PROBLEM SOLVING AND
PYTHON PROGRAMMING
Year I SEM I CLASS CSE
Name of
the Faculty
Mrs.R.Sathya Priya, Assistant Professor Dept. CSE
PSPP U-I
2. UNIT-I ALGORITHMIC PROBLEM
SOLVING Algorithms, Building Blocks of
algorithms (statements, state, control flow,
functions), Notation (pseudo code, flow chart,
programming language), Algorithmic problem
solving, Simple strategies for developing
algorithms (iteration, recursion).
Illustrative problems: find minimum in a list,
insert a card in a list of sorted cards, Guess an
SYLLABUS
2
3. 3
Illustrative problems:
Find minimum in a list.
Insert a card in a list of sorted cards.
Guess an integer number in a range.
Towers of Hanoi.
5. List
5
List is a collection of elements stored under a
common name.
List is commonly represented by using [ ].
Example:
b[ ] = {10, 20, 30, 40, 50}
b [0] b [1] b [2] b[3] b[4]
10 20 30 40 50
6. Finding Minimum in a list
6
Given a list of numbers from which you are
supposed to find the minimum value.
An algorithm is required for entering the numbers
in the list and then calculate the minimum value.
How it works?
Take the first number in the list and call it
minimum.
Compare the minimum’s value with all other
values in the list one by one.
The moment you find a smaller element than the
7. 7
Example
Initial List 8 6 9 10 5 7 3 21
Let Min = 8, Compare MIN with every element in the list one by one.
8 6 9 10 5 7 3 21
Step 1: 6<Min, So set Min=6
8 6 9 10 5 7 3 21
Step 2: 9 > Min , So no change
8 6 9 10 5 7 3 21
Step 3: 10 > Min , So no change
8 6 9 10 5 7 3 21
Step 4: 5 < Min, So Set Min =5
8 6 9 10 5 7 3 21
Step 5: 7 > Min, So No Change.
8 6 9 10 5 7 3 21
Step 6: 3 < Min, So Set Min=3
8 6 9 10 5 7 3 21
Step 7: 21 > Min , So No change
So the minimum value in the given list is 3.
8. 8
Algorithm:
Step 1: Start the program.
Step 2: Read the number of elements in list n.
Step 3: Initialize i=0 as the starting index
Step Repeat steps 4.1 and 4.2 until the while
condition is true (i<n ) , otherwise continue with
step5 .
Step 4.1: Read the element a[i]
Step 4.2: i=i+1
Step 5: initialize k=1, Min=a[0]
Step 6: Repeat step 6.1 and 6.2 until the while
condition is true (k<n), otherwise continue with
step7.
Step 6.1: If a[k]<Min then set Min=a[k]
Step 6.2: k=k+1
9. 9
Pseudo code:
READ N
INITIALIZE i=0,
WHILE i<n
READ a[i]
SET i=i+1
ENDWHILE
INITIALIZE Min=a[0], k=1
WHILE k<n
IF a[k] < Min THEN
Min=a[k]
k=k+1
ENDWHILE
PRINT Min
12. Inserting a Card into a list of sorted
Cards
12
Inserting a card in a list of sorted cards is same as
inserting an element into a sorted list of numbers.
For this, start from the end of the list and compare
the new element with the elements of the list to
find a suitable position at which the new element
has to be inserted.
While comparing, also shift the elements one step
ahead to create a vacancy for the new element.
13. 13
Example : Insert the element 23 into the given sorted list
Position 0 1 2 3 4 5 6
Original list 10 15 20 25 30 35
Step 1: 23>35 ? No so move 35 to position 6
10 15 20 25 30 35
Step 2: 23>30 ? No so move 30 to position 5
10 15 20 25 30 35
Step 3: 23>25 ? No so move 25 to position 4
10 15 20 25 30 35
Step 4: 23>20 ? Yes insert 23 at position 3
10 15 20 23 25 30 35
14. 14
Algorithm:
Step 1: Start the program.
Step 2: Read the number of elements in list n.
Step 3: Initialize i=0
Step 4: Repeat steps 4.1 and 4.2 until the while condition is true
(i<n ) , otherwise continue with step5 .
Step 4.1: Read the element a[i] –in sorted order
Step 4.2: i=i+1
Step 5: Read the element to be inserted X
Step 6: Set j=n-1
Step 7: Repeat steps 7.1 and 7.2 until the while condition is true
( (j>=0) && (X<a[j])) otherwise continue with step8 .
Step 7.1: a[j+1]=a[j]
Step 7.2: Set j=j-1
Step 8: a[j+1] = X
Step 9: Stop the Program.
15. 15
Pseudo code:
READ n
SET i=0
WHILE i<n
READ a[i]
i=i+1
ENDWHILE
READ X
SET j=n-1
WHILE j>=0 && a[j]>X
a[j+1]=a[j]
j=j-1
ENDWHILE
a[j+1]=X
18. Guess an integer number in a
range
18
It is a game.
The user selects a range.
Let’s say User selected a range, i.e., from A to B,
where A and B belong to Integer.
Some random integer will be selected by the system and
the user has to guess that integer in the minimum number
of guesses.
There is no limit on number of guesses.
Example:
If the User inputs range, let’s say from 1 to 100. And System
randomly selected 42 as the integer.
Now the guessing game started, if the user entered 50 as
his/her first guess. The compiler shows “Try Again! You
guessed too high”.
Now the second guess started.
19. 19
Algorithm:
Step 1: Start the program.
Step 2: Read the range of integers Start, End.
Step 3: Set num = random number from Start to End.
Step 4: Set =1
Step 5: Read the Guess number as G
Step 6: If G=num then print ‘you win in the ith guess’
Step 7: Else If G>num the print ‘your guess is too high’ go to step
5.
Step 8: Else print ‘your guess is too low’ go to step 5.
Step 9: Stop the program.
20. 20
Pseudo code:
READ Start, End
SET num=Random(Start, End)
SET i =1
LOOP:
READ G
IF G=num THEN
PRINT ‘You win in ith turn’
EXIT
ELSEIF G>num THEN
PRINT ‘Too High’
ELSE
PRINT ‘Too Low’
i=i+1
ENDLOOP
23. Tower of Hanoi
23
Tower of Hanoi is a mathematical puzzle where we
have three poles (namely A, B, C) and n disks.
The objective of the puzzle is to move the disks from
pole A to Pole C using the Pole B.
simple rules:
1) Only one disk can be moved at a time.
2) Each move consists of taking the upper disk from
one of the poles and placing it on top of another pole
i.e. a disk can only be moved if it is the uppermost
disk on a pole.
3) No disk may be placed on top of a smaller disk.
26. 26
Algorithm:
Step 1: Start
Step 2: Read the number of disc n
Step 3: Call the function TOH(n, A, B, C)
Step 4: Stop
Algorithm for Function Definition TOH
Step 1: if n==1 then Move disc from A to C and
go to step 5 else continue with step 2
Step 2: Call the function TOH(n-1,A,C,B)
Step 3: Move disc n from A to C
Step 4: Call the function TOH(n-1,B,A,C)
Step 5: Return
27. 27
Pseudo code:
READ n
CALL TOH(n,A,B,C)
Function Definition TOH(n, A, B, C)
IF n==1
Move disc from A to C
return
ELSE
CALL TOH(n-1,A,C,B)
Move disc n from A to C
CALL TOH(n-1,B,A,C)
Return