Pseudocode City Ravi
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.
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
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
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) 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.
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
Arrays:
A list that can hold multiple data items of SAME DATA TYPE.
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
DECLARATION :
Practice: Set up an array to store 5 games names.
DECLARE Games : ARRAY[1:5] OF STRING
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
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
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.