8 PseudocodePython
8 PseudocodePython
OUTPUT "Enter your name : " FUNCTION CalculateAverage(Array: ARRAY[1:6] OF INTEGER) RETURNS INTEGER
INPUT Name Total <- 0
FOR c <- 1 TO 6
Constant - A variable that doesn't change its value later on in the code Total <- Total + Array[c]
NEXT c
CONSTANT VariableName <- value // 50+60+70+80+90+100
RETURN ROUND(Total/6, 0) // 75
Types ENDFUNCTION
ArrayOneAverage <-- CalculateAverage(ARRAY) // [50,60,70,80,90,100]
STRING - A combination of different letters and numbers - Alphanumeric - "John Smith" OUTPUT ArrayOneAverage // 75
CHAR - A singular letter - 'A'
INTEGER - A whole number - 53
REAL - A number going into decimal points - 6784.23
BOOLEAN - TRUE or FALSE - TRUE
DECLARATION SYNTAX
CONDITIONAL STATEMENTS
IF…THEN...ELSE…ENDIF
IF Condition
THEN Condition result is 1
ELSE Condition result is 0
ENDIF
0/FALSE 1/TRUE 0 /FALSE
(Num1 > Num2) OR NOT (Num2 > Num3) OR (Num4 > Num3)
CASE OF…ENDCASE
CASE OF VariableName
Value1: Statement
Value2: Statement
Value3: Statement
ENDCASE
OUTPUT "Enter num between 1-3"
INPUT Num
CASE OF Num
1: OUTPUT "Hello"
2: OUTPUT "Bye"
3: OUTPUT "You're stupid"
OTHERWISE: OUTPUT "You're too stupid"
ENDCASE
Loops
Going over a code over and over and over and over in iterations
FOR c <- 1 TO 50
ACTION
NEXT c
REPEAT
OUTPUT "Enter a number between 1 and 50"
INPUT Num
UNTIL Num < 1 OR Num > 50
1 - 50
// 49
Total = 0
for count in range(1,50):
record = int(input('Input student record for student : '))
Total = Total + record
print(Total)
A teacher inputs 50 students marks. Count how many of them passed. Passed = above
50. Use a FOR loop
Passed = 0
for count in range(1,50):
marks = int(input('Enter student marks : '))
if(marks > 50):
Passed = Passed + 1
Highest = 0
Lowest = 100
Total = 0
for count in range(0,5):
mark = int(input('Enter mark : '))
if(mark > Highest):
Highest = mark
if(mark < Lowest):
Lowest = mark
Total = Total + mark
avg = Total/5
print(Highest, Lowest, avg)
'''
Test Data = 40, 70, 10, 5, 90
Lowest - 40 , Highest - 40
Lowest - 40 , Highest - 70
Lowest - 10 , Highest - 70
Lowest - 5 , Highest - 70
Lowest - 5 , Highest - 90
Output - 90, 5, 43.0
'''
You're the teacher at a school. Your task is to add all student's marks and figure out the grade.
Procedures
PROCEDURE ProcedureName(Parameters)
ProcedureWork
ENDPROCEDURE
SendStars(5)
*****
Arrays
1 2 3 4 5
ArrayName[Index]
ArrayName[1,
An array AccDetails[] is a 2D Array with 50 clients information. And each client has their balance,
withdrawal balance & debt balance inside.
1 1 1
1 1 1
1 1 1
1 1 1
ROUND(Number, DecimalPoints) Rounds the number to the amount of d.p specified ROUND(6.7382, 2) -> 6.74
RANDOM() Returns a random number B/W 1 and 0 Inclusive RANDOM() -> 1 or 0
LEFT(string, Returns the string from the left to the amount of LEFT("Ateeb Sohail", 5) -> "Ateeb"
amountOfCharactersToLeft) character specified
RIGHT(string, Returns the string from the right to the amount of RIGHT("Ateeb Sohail", 6) -> "Sohail"
amountOfCharactersToRight) character specified
MID(String, Position, Returns the string from the position it started at MID("Ateeb Sohail", 3, 5) -> "eeb S"
AmountOfCharacters)
LENGTH(String) Returns the length of the string LENGTH("Ateeb Sohail") -> 12
UCASE(String) Returns string in upper case UCASE("Ateeb Sohail") -> ATEEB SOHAIL
LCASE(String) Returns string in lower case LCASE("Ateeb Sohail") -> ateeb sohail
Linear Search
Used to find a value in an array using a FOR loop usually(unless mentioned otherwose)
Find 'Rick' in the Array. Return the position of Rick, if not found return -1. Create it in a function named
Find with 2 parameters, the array below and the findvalue variable below
Array <- ["Ateeb", "Rick", "Brittany", "Megatron", "Sam"]
FindValue <- "Rick"
Bubble Sort
REPEAT
Swap <- FALSE
FOR Index <- 1 TO 2
IF Numbers[Index] > Number[Index+1]
THEN
Temp <- Numbers[Index]
Numbers[Index] <- Number[Index +1]
Number[Index+1] <- Temp
Swap <- TRUE
ENDIF
NEXT Index
UNTIL Swap = FALSE
Range Check - It's used to check whether or not a certain number is within a range, e.g 50 -100.
Length Check - It's used to check the length of a string
Type Check - It's used to check the data type of a variable
Presence Check - It's used to check if anything has been input
Format Checks - It's used to check the format of a string or verify it starts with something
Check Digits - It's used mainly in ISBN-13 or Modulo-11
Range Check
Question : Input a number and verify its between 1 through 10 inclusive. You do not
Need to reinput the number
INPUT Number
IF Number < 1 OR Number > 10
THEN OUTPUT "Number isn't within range"
ENDIF
REPEAT
OUTPUT "Enter Number between 1 and 10 :"
INPUT Number
UNTIL Number >= 1 AND Number <= 10
Length Check
Question : Input a string and verify that it is 8 characters long. You do not need to reinput.
INPUT String
IF LENGTH(String) <> 8
THEN OUTPUT "Length is not 8"
ENDIF
REPEAT
OUTPUT "Enter string of length 8 :"
INPUT String
UNTIL LENGTH(String) = 8
Type Check
INPUT Number
IF Number <> DIV(Number, 1)
THEN OUTPUT "Enter Whole Number"
ENDIF
REPEAT
OUTPUT "Enter a whole number : "
INPUT Number
UNTIL Number = DIV(Number, 1)
Presence Check
INPUT String
IF String = ""
THEN OUTPUT "Empty"
ENDIF
REPEAT
OUTPUT "Enter a non-empty string : "
INPUT String
UNTIL String <> "" [10]
Format Check
PK844
PKNNN
// Initializing all variables to their default values or for lowest we're initializing to the highest
LEFT 2 CHARACTERS ARE PK possible.
Question : Input a string and check if it follows the format "PKNNN" NoOfDays <-- 5
TotalClassMinutes <- 0
INPUT String Lowest <-- 1000
IF LEFT(String, 2) <> "PK" LowestIndex <- 0
THEN OUTPUT "Invalid Format" // Starting a for loop to the class size
ENDIF FOR Student <- 1 TO ClassSize
// Initializing the default values for total screen time and number of 300 greaters for
REPEAT each student
OUTPUT "Enter a string in format PKNNN" TotalScreenTime <- 0
INPUT String NumberOf300Greaters <- 0
UNTIL LEFT(String, 2) = "PK" // Starting a for loop till the number of days
FOR Day <- 1 TO NoOfDays
Question : Input a string and check if it follows the format "NNNPK" // Inputting screen time for a certain number of day
OUTPUT "Enter Amount of screentime for day ", Day
INPUT String INPUT ScreenTime
IF RIGHT(String, 2) <> "PK" // Adding screen time to total
THEN OUTPUT "Invalid Format" Total <- ScreenTime + Total
ENDIF // Adding screen time to total class screen time
TotalClassMinutes <-- ScreenTime + TotalClassMinutes
REPEAT // Adding the screentime to the ScreenTime array
OUTPUT "Enter a string following pattern NNNPK" ScreenTime[Student, Day] <-- ScreenTIme
INPUT String // Calculating amount of 300 greaters
UNTIL RIGHT(String, 2) = "PK" IF ScreenTime > 300
THEN Number300Greaters <- Number300Greaters + 1
REMEMBER TO LEARN DIFF BETWEEN VERIF AND VALID ENDIF
NEXT Day
// Checking if total screen time is less than the lowest recorded
IF(TotalScreenTime < Lowest)
Distinctions <- 0
Merits <- 0
Passes <- 0
Fails <- 0
FOR Student <- 1 TO ClassSize
TotalStudentMark <- 0
FOR Subject <- 1 TO SubjectNo
TotalStudentMark <- TotalStudentMark + StudentMark[Student,Subject]
NEXT Subject
Average = ROUND(TotalStudentMark/SubjectNo, 0)
IF Average >= 70
THEN Grade <- "distinction"
Distinctions <- Distinctions + 1
ENDIF
IF Average >= 55 AND Average < 70
THEN Grade <- "merit"
Merits <- Merits + 1
ENDIF
IF Average >= 40 AND Average < 55
THEN Grade <- "pass"
Passes <- Passes + 1
ENDIF
If Average < 40
THEN Grade <- "fail"
Fails <- Fails + 1
ENDIF
OUTPUT "Student Name : ", StudentName[Student]
OUTPUT "Student Mark Total : ", TotalStudentMark
OUTPUT "Student Average Marks : ", Average
OUTPUT "Student Grade : ", Grade
NEXT Student
READ
WRITE
CLOSEFILE(Filename/filepath)
REPEAT
// Inputting and validating amout
INPUT Amount
IF Amount < 0
THEN OUTPUT "Reenter a valid Amount"
ENDIF
// Checking if amount is within withdrawal Limit
IF Amount > AccDetails[AccID, 3]
THEN OUTPUT "You may only withdraw ", AccDetails[AccID, 3] " money at a time."
ELSE
IF Amount <= AccDetails[AccID, 1]
THEN
// Withdrawing the balance
OUTPUT "Successfully Withdrew ", Amount, " From your balance"
AccDetails[AccID, 1] <- AccDetails[AccID, 1] - Amount
ELSE
// Checking the overdraft limit and applying it
BalanceWithWithdrawal <- AccDetails[AccID, 1] + AccDetails[AccID,
2]
IF Amount <= BalanceWithWithdrawal
THEN
OUTPUT "Successfully Withdrew ", Amount, " From
your balance"
AccDetails[AccID, 1] <- AccDetails[AccID, 1] - Amount
ELSE
OUTPUT "You do not have enough balance including
withdrawal limit to make this transaction"
ENDIF
ENDIF
ENDIF
UNTIL (Amount <= (AccDetails[AccID, 1] + AccDetails[AccID, 2])) OR (Amount <= AccDetails[AccID,
1] AND Amount < AccDetails[AccID, 3]
ENDPROCEDURE
IF Valid
THEN
REPEAT
// Displaying Choices
OUTPUT "1. display balance"
OUTPUT "2. withdraw money"
OUTPUT "3. deposit money"
OUTPUT "4. exit"
INPUT Choice
//Handling choices
CASE OF Choice
1: CALL DisplayBalance(AccountNumber)
2: CALL WithdrawBalance(AccountNumber)
3: CALL DepositBalance(AccountNumber)
4: OUTPUT "Goodbye have a nice rest of your fucking day"
OTHERWISE: OUTPUT "Please re-enter a choice between 1-4"
UNTIL Choice = 4
ELSE
OUTPUT "Invalid Credentials Provided"
ENDIF