0% found this document useful (0 votes)
41 views15 pages

Pseudocode City Ravi

Uploaded by

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

Pseudocode City Ravi

Uploaded by

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

Pseudocode: City Ravi

Lesson 1:
Algorithm:
Set of instructions designed in an order to solve a specific problem.
Flow Chart:
Pictorial representation of an algorithm.
Pseudocode:
Representation of an algorithm using Keywords is pseudocode.
NOTE: All the pseudocode keywords are in uppercase.
1) Identifiers: [Memory Locations where data can be stored]
a) Constant
Any storage location where a value never changes during the execution of the program.
b) Variable
Any storage location where a value may change during the execution of the program.
c) Array
Data structures that can hold multiple data items of same data type.
2) Input & Output
a) Input : INPUT
To take a value from user and store it into an identifier.
b) Output : PRINT/OUTPUT
To display something on user’s screen, that can be a string or a value from an identifier.
i) DECLARATION:
NOTE : Every identifier must be declared within the program.

Syntax :
Constant:
DECLARE/CONST <identifier> : Data Type
<identifer>= “value”
Variable:
DECLARE <identifier> : Data Type
Data Types:
1) STRING (Always encapsulated in double quotation i.e., “ ”)
Alphabets, alphabets + numbers, alphabets + numbers + symbols.
Haseeb, haseeb123, [email protected]
Any non-calculative data.
Serial Numbers, Ids, Phone Numbers
2) INTEGER
Whole Numbers : -55, +55
3) REAL
Decimal Places : -55.76, +55.89
4) BOOLEAN
Two States : Discrete behavior
ON-OFF, TRUE-FALSE,YES-NO
5) CHAR (Always encapsulated in single quotation i.e., ‘ ’)
Store one single character. Male=M, Female=F
6) DATE & TIME
Data & Time
Variable:
DECLARE MaxMarks : INTEGER
Constant:
DECLARE pi : REAL
pi=3.1416
NOTE : Please never initialize an identifier while declaring.

Rules to Name Identifiers:


1) All the identifiers must have meaningful names.
2) There should be no space in between identifier names.
3) You can not even separate the words with underscores.
4) All you can do is capitalize 1st letter of every word.
5) None of the identifier names can start with an integer.

Max marks xx
Max_marks xx
MaxMarks (accepted)
Num1 (accepted)
1num xx
Arithmetic Operators:
+,-,*,/,^
X^2=
Boolean Operators:
AND, OR NOT

NOTE : 😊
Where ever you get stuck in P2, using FLAG LOOPING.

Basic:
Sum of two numbers
DECLARE num1,num2 : INTEGER
DECLARE sum : INTEGER
PRINT “please enter 1st number”
INPUT num1
PRINT “please enter 2nd number”
INPUT num2
sum=num1+num2
PRINT sum
NOTE : please never encapsulate identifiers in double quotations
Average of three Numbers
DECLARE num1,num2,num3,sum : INTEGER
DECLARE avg : REAL
PRINT “please enter three numbers”
INPUT num1,num2,num3
sum=num1+num2+num3
avg=sum/3
PRINT avg

Lesson 2:
Conditional Statements/Selections:
Conditional statements are used when one or more outcomes are required for a particular condition.
1) IF (it can give maximum of two outcomes for one condition/ true or false outcomes)
IF…(condition)…THEN…ELSE…(kbhe condition mat likhna)…ENDIF
IF (condition) THEN ELSE ENDIF
EXTEND: [NESTED IFs]
IF THEN ELSEIF THEN ELSEIF THEN ELSEIF THEN ELSE ENDIF

2) CASE (many possible outcomes of one condition)


CASE OF (identifier) OTHERWISE ENDCASE

Largest of three numbers


IF CASE
DECLARE num1,num2,num3 : INTEGER DECLARE num1,num2,num3 : INTEGER
DECLARE Largest : INTEGER DECLARE Largest : INTEGER
PRINT “please enter three numbers” PRINT “please enter three numbers”
INPUT num1,num2,num3 INPUT num1,num2,num3
IF num1>num2 AND num1>num3 THEN CASE OF Largest
Largest=num1 num1: num1>num2 AND num1>num3
ELSEIF num2>num1 AND num2>num3 THEN num2: num2>num1 AND num2>num3
Largest=num2 num3: num3>num1 AND num3>num2
ELSE ENDCASE
Largest=num3 PRINT Largest
ENDIF
PRINT Largest

Homework : Find largest of 5 numbers 😊


Grades Table
DECLARE marks : INTEGER DECLARE marks : INTEGER
DECLARE Grade : STRING DECLARE Grade : STRING
PRINT “please input marks” PRINT “please input marks”
INPUT marks INPUT marks
//validation, please NEVER use conditional //validation, please NEVER use conditional
statements to validate statements to validate
IF marks<0 OR marks>100 THEN IF marks<0 OR marks>100 THEN
PRINT “invalid marks entered, please re- PRINT “invalid marks entered, please re-
enter” enter”
INPUT marks INPUT marks
ENDIF ENDIF
IF marks>= 90 THEN CASE OF Grade
Grade=”A*” “A*” : marks>=90
ELSEIF marks>=80 THEN “A” : marks>=80
Grade=”A” “U” : marks<80
ELSE ENDCASE
Grade=”U” PRINT Grade
ENDIF
PRINT Grade

Simple Calculator
DECALRE num1,num2 : INTEGER DECALRE num1,num2 : INTEGER
DECLARE choice : STRING DECLARE choice : STRING
DECLARE Result : REAL DECLARE Result : REAL
PRINT “please enter two numbers” PRINT “please enter two numbers”
INPUT num1,num2 INPUT num1,num2
PRINT “please select your choice, +,-,*,/” PRINT “please select your choice, +,-,*,/”
INPUT choice INPUT choice
IF choice=”+” THEN CASE OF choice
Result=num1+num2 “+”: Result=num1+num2
ELSEIF choice=”-“ THEN “-”: Result=num1-num2
Result=num1-num3 “*”: Result=num1*num2
ELSEIF choice=”*” THEN “/”: Result=num1/num2
Result=num1*num2 END CASE
ELSE PRINT Result
Result=num1/num2
ENDIF
PRINT Result

Pseudocode.pdf : examples 15-20


Lesson 3:
Loops : The most important, for programming!
To handle infinite numbers, to handle large amount of numbers, to run the program on a user-defined
condition, we need loops.
LOOPS/ITERATION/REPETITION:

Basic Algorithms related to LOOPS.


Counting:
[Increment/Decrement]
Count=0 0
Count=count+1 ,1,2,3,4,5,6,7

Count=0 0
Count=Count+2 ,2,4,6,8,10

Totaling:
[Whenever there is a need to add many numbers. Don’t use: (num1+num2+num3+num4…..)]
Total=0 0
Total=Total + num //number being entered by 0=0+10…10
the user
10=10+20…30
30=30+40…70

Highest:
[Whenever you want to find out highest of many numbers. Don’t use: (num1>num2 AND num1>blah
blah….)]
Highest=0 0
IF num>Highest THEN Highest=num 13>0…13
21>13…21
8>21…21

Lowest:
[Whenever you want to find out lowest of many numbers. Don’t use: (num1<num2 AND num1<blah
blah….)]
Lowest=9999 9999
IF num<Lowest THEN Lowest=num 13<9999…13
21<13…13
7<13…7
Let’s Differentiate Loops 😊

1) WHILE…ENDWHILE (pre-conditioned loop)


2) REPEAT…UNTIL (post-conditioned loop)
3) FOR…TO…NEXT (counter control loop)
WHILE REPEAT FOR
Pre-conditioned Loop Post-Conditioned Loop Counter Control Loop
Conditioned will be given at the Condition is given at the end of Works on set number of
start of loop the loop times[Range]
The program won’t run if the Program will run at least once, Program would run within a
condition isn’t justified even if the condition isn’t specific range
justified
Works on FALSE condition Works on TRUE condition Range
< , <> >, = 1 TO 100
Num <> -1 Num = -1 For can’t handle conditions

Print 1st 100 Whole Numbers (0-99)


WHILE REPEAT FOR
count=0 count=0
WHILE count<100 REPEAT FOR count = 0 TO 99
PRINT count PRINT count PRINT count
count=count+1 count=count+1 NEXT
ENDWHILE UNTIL count=99

1) There is no need to initialize the counter using for loop, as you can initialize within the structure
of loop that is, 0 TO 99, 0 here is the starting while 99 is the ending point.
2) NEXT acts as a built-in counter, that is count is incremented by 1.
Print 1st 50 Even Numbers (2-100[Inclusive])
count=0 count=0 FOR count= 2 TO 100
WHILE count<101 REPEAT PRINT count
count=count+2 count=count+2 count=count+1
PRINT count PRINT count NEXT
ENDWHILE UNTIL count=100
Home Work : Solve all example questions and exam style questions from pseudocode.pdf.

Assignments will be reported to PARENTS. 😊


Take 50 numbers from user, find out the average, highest & lowest
DECLARE DECLARE DECLARE
Total,Highest,Lowest,count,num : Total,Highest,Lowest,count,num : Total,Highest,Lowest,count
INTEGER INTEGER ,num : INTEGER
DECLARE Avg : REAL DECLARE Avg : REAL DECLARE Avg : REAL
Total=0,Highest=0,Lowest=9999,c Total=0,Highest=0,Lowest=9999,c Total=0,Highest=0,Lowest=
ount=0 ount=0 9999
WHILE count < 50 REPEAT FOR count= 1 TO 50
PRINT “please enter a number” PRINT “please enter a number” PRINT “please enter a
number”
INPUT num INPUT num INPUT num
Total=Total+num Total=Total+num Total=Total+num
IF num>Highest THEN IF num>Highest THEN IF num>Highest THEN
Highest=num Highest=num Highest=num
IF num<Lowest THEN Lowest=num IF num<Lowest THEN Lowest=num IF num<Lowest THEN
Lowest=num
count=count+1 count=count+1 NEXT
ENDWHILE UNTIL count=50 Avg=total/50
Avg=total/50 Avg=total/50 PRINT Avg, Highest, Lowest
PRINT Avg, Highest, Lowest PRINT Avg, Highest, Lowest
Lesson 4:
Practice Loos:
A user can enter as many positive numbers he wants to, an input of -1 stops the program and outputs
the average, highest & lowest number, it also outputs the range i.e., the difference between highest
and lowest number. *IMPORTANT* *USER-DEFINED VALUE*
WHILE REPEAT FOR
DECLARE avg : REAL DECLARE avg : REAL Can we do it with FOR Loop?
DECLARE num, total, highest, DECLARE num, total, highest, We cannot use for loop, as we
lowest, range, count : INTEGER lowest, range, count : INTEGER have NO IDEA how many times
the loop should run.
total=0, highest=0, total=0, highest=0,
lowest=9999, count=0 lowest=9999, count=0
PRINT “please enter a number” REPEAT
INPUT num PRINT “please enter a number”
WHILE num <> -1 INPUT num
total=total + num IF num <> -1 THEN
IF num > highest THEN highest = total=total + num
num
IF num < lowest THEN lowest = IF num > highest THEN highest =
num num
count = count + 1 IF num < lowest THEN lowest =
num
PRINT “please enter next count = count + 1
number”
INPUT num UNTIL num=-1
END WHILE avg=total/count
avg=total/count range=highest-lowest
range=highest-lowest PRINT avg, highest, lowest,
range
PRINT avg, highest, lowest,
range
Whenever working on a user- Whenever working on a user-
defined value, using while loop, defined value, using repeat until
there is a need to take the input loop, there is always a need to
twice, once, outside the loop for validate at the start of the loop
the condition to be justified, to get rid of 1st invalid or
and once inside the loop for the conditional value being entered.
loop to run again if required.

ALGORITHM :
1) 365 TIMES
2) 10 TIMES
3) INPUT a Temperature
4) Calculate highest temperature including 10 temp for 365 Days (3650)
5) Calculate lowest temperature including 10 temp for 365 Days (3650)
6) Step 2
7) Daily Basis Average Temp (total-daily)
8) Step 1
9) Yearly Basis Average (total-yearly)
DECLARE temp, highest, lowest, TotalDaily, TotalYearly : INTEGER
DECLARE AvgDaily, Avg,Yearly : REAL
TotalYearly = 0, highest = 0, lowest = 9999
FOR day= 1 TO 365
TotalDaily = 0
FOR readings = 1 TO 10
PRINT “please input temperature”
INPUT temp
TotalDaily=TotalDaily+temp
TotalYearly=TotalYearly+temp
IF temp>highest THEN highest=temp
IF temp<lowest THEN lowest=temp
NEXT readings
AvgDaily=TotalDaily/10
PRINT AvgDaily
NEXT day
AvgYearly=TotalYearly/3650
PRINT AvgYearly, highest, lowest

DECLARE slowest, fastest, time, TotalSpeed : INTEGER


DECLARE AvgSpeed, FinalSpeed : REAL
slowest=9999, fastest=0, TotalSpeed=0
FOR count = 1 TO 500
PRINT “please enter time”
INPUT time
FinalSpeed=200/Time
PRINT FinalSpeed
IF FinalSpeed > Fastest THEN Fastest=FinalSpeed
IF FinalSpeed < Slowest THEN Slowest=FinalSpeed
TotalSpeed=TotalSpeed+FinalSpeed
NEXT
AvgSpeed=TotalSpeed/500
PRINT AvgSpeed,Fastest,Slowest

Arrays:
A list that can hold multiple data items of SAME DATA TYPE.

CONCEPT: There is a need to store 10 numbers being inputted by the user.

Num1,num2,num3,num4,num5,num6,num7,num8,num9,num10
Sum= Num1+num2+num3+num4+num5+num6+num7+num8+num9+num10

1 10
2 11
3 67
4 98
5 43
6 12
7 54
8 76
9 34
10 12
Position: INDEX Dimension:
Logical It is where the actual element or data is being stored. You may refer
It gives the position; where Dimension as a column in an array.
the element is being stored in
the array, always access the
index to access the element
within the array

There is no need to make separate variables we can simply use :


Name of the array [x]
[x] : index of the array
Example : Num[x]

DECLARATION :
Practice: Set up an array to store 5 games names.
DECLARE Games : ARRAY[1:5] OF STRING

INPUT Names (Programmer-Defined)


DECLARE Games : ARRAY[1:5] OF STRING
Games[1]=”Unchartered 4”
Games[2]=”Tekken 7”
Games[3]=”COD”
Games[4]=”FIFA”
Games[5]=”PUBG”

OUTPUT Names(Programmer-Defined)
PRINT Games[3],Games[5]
INPUT-OUTPUT (User-Defined Values)
Practice: Set up a 1D array to store 5000 Names, take names from the user and populate the array,
output the names from index 700 to 4167.
DECLARE Names : ARRAY[1:5000] OF STRING
FOR count = 1 TO 5000
PRINT “please enter names”
INPUT Names[count]
NEXT
FOR count = 700 TO 4167
PRINT Names[count]

Manipulating 1D Arrays:

Set up a 1D array Marks to store marks for 300 students, find out the average, highest and lowest
marks.

Please solve: Set up a 1D array Info to hold info about the salaries of 800 employees, take all the
values from user and populate the array, output the average salary, highest and lowest salaries, do
find out the range as well where range=highest salary paid – lowest salary paid.

Lesson 5:
DECLARE Info : ARRAY[1:800] OF REAL
DECLARE total,highest,lowest,Range : INTEGER
DECLARE avg : REAL
total=0,highest=0,lowest=9999
FOR count = 1 TO 800
PRINT “please enter salaries”
INPUT Info[count]
total=total+Info[count]
IF Info[count]>highest THEN highest=Info[count]
IF Info[count]<lowest THEN lowest=Info[count]
NEXT
Range=highest-lowest
Avg=total/800
PRINT Avg, Range, highest, lowest

Linear Search 😊
Algorithm:
1) Keep searching index after index
2) If found, break the loop using flag looping
3) Else increment the index
4) Until found
5) Or the element does not exist
Pseudocode:
DECLARE Nums : ARRAY[1:10] OF INTEGER
DECLARE search, index : INTEGER
DECLARE Found : BOOLEAN
index=1
Found=False
PRINT “please enter value you’re looking for”
INPUT search
REPEAT
IF Nums[index]=search THEN
Found=True
ELSE
index=index+1
ENDIF
UNTIL Found OR index>10
IF Found THEN
PRINT “element found at: ”, index
ELSE
PRINT “not found”
ENDIF

Practice: Set up a 1D array named details to store names of 300 employees.

BUBBLE SORT:
PSEUDOCODE:
DECLARE Names : ARRAY[1:5] OF STRING
DECLARE swap : BOOLEAN
DECLARE temp,top : INTEGER
top=5
REPEAT
swap=False
FOR count = 0 to top-1
IF Names[count]>Names[count+1] THEN
temp=Names[count]
Names[count]=Names[count+1]
Names[count+1]=temp
Swap=TRUE
NEXT
top=top-1
UNTIL NOT swap OR top=0

2D Arrays:
If an array has more than 1 dimension.
1,1 1,2

6,3

8,5

NOTE : ALWAYS MANIPULATE THE CO-ORDINATES that is THE INDEX…


YOU WILL FACE NO ISSUES IN MANIPULATING THE 2D ARRAYS:

INPUT/OUTPUT:
A 2D array details is required to store names and email of 200 students. Take values from the user and
populate the array and then output the contents of the array.
DECLARE Details : ARRAY[1:200,1:2] OF STRING
FOR row = 1 TO 200
PRINT “please enter your name”
INPUT Details[row,1]
PRINT “please enter your email”
INPUT Details[row,2]
NEXT row
FOR row= 1 TO 200
FOR col = 1 TO 2
PRINT Details[row,col]
NEXT col
NEXT row

Set all elements of 2D array named info to “empty”, the array has 500 indexes and 200 dimensions.

DECLARE Info : ARRAY[1:500,1:200] OF STRING


FOR row = 1 TO 500
FOR col = 1 TO 200
Info[row,col]=”EMPTY”
NEXT col
NEXT row
An array has names and emails of 300 students, there is a need to change the order, that is: the array

should be arranged in ascending order of names. 😊


DECLARE swap : BOOLEAN
DECLARE temp1,temp2,top,col : INTEGER
top=300,col=1
REPEAT
Swap=False
FOR row= 1 TO top-1
IF Info[row,col]>Info[row+1,col] THEN
temp1=Info[row,col]
Info[row,col]=Info[row+1,col]
Info[row+1,col]=temp1
swap=True
temp2=Info[row,col+1]
Info[row,col+1]=Info[row+1,col+1]
Info[row+1,col+1]=temp2
ENDIF
NEXT
top=top-1
UNTIL not swap or top=0

If you have 50000 dimensions: 😊


DECLARE swap : BOOLEAN
DECLARE temp1,temp2,top,col : INTEGER
top=300,col=1
REPEAT
Swap=False
FOR row= 1 TO top-1
IF Info[row,col]>Info[row+1,col] THEN
temp1=Info[row,col]
Info[row,col]=Info[row+1,col]
Info[row+1,col]=temp1
swap=True
FOR col = 2 TO 50000
temp2=Info[row,col]
Info[row,col]=Info[row+1,col]
Info[row+1,col]=temp2
NEXT col
ENDIF
NEXT
top=top-1
UNTIL not swap or top=0

You might also like