SIMPLE SOLUTIONS TO 207 ALGORITHM PROBLEMS WITH COMMENTS
Please note this:
‘A <- B’ Means we are assigning the value in variable B, to that in A, so variable A is now the
same as variable B
‘ //... ’ Indicates that the following line, is a comment used to further explain what is
happening, so the computer ignores this line
‘ /* ... */ ’ Indicates that all lines within these 2 symbols xd are comments, this is used as
a multi-line comment :P
‘ != ’ Means NOT EQUAL TO
1. ODD AND EVEN NUMBER CHECKER
BEGIN
DECLARE num
// Declares a variable num
INPUT “Enter a number”
// Prompts the user to “Enter a number”
READ num
// Reads the number entered by the user and saves it to variable num
IF (num % 2 EQUALS 0) THEN
DISPLAY “The number entered is even”
ELSE
DISPLAY “The number entered is odd”
ENDIF
/*
This if-else condition, checks if the number saved in variable ‘num’ is divisible by 2 then tell the user if it is! :P
*/
END
2. GRADE CALCULATOR
BEGIN
DECLARE studentGrade
// Declare a variable studentGrade to store the
// student's grade
INPUT “Please input your grade”
// Prompt the user to enter his grade
READ studentGrade
// Read and store the user's input in
// studentGrade variable
IF (studentGrade >= 80) THEN
DISPLAY “A”
/* We first check if the value stored in studentGrade is greater than or equal to 80 and print an A */
ELSE-IF (studentGrade >= 65) THEN
DISPLAY “B”
ELSE-IF (studentGrade >= 50) THEN
DISPLAY “C”
ELSE
DISPLAY “F”
ENDIF
END
NB:
➔ We assumed the user entered a number, between 0 – 100
➔
/*
Also see that the we started checking from the greatest, that is 80 and above such that we don’t need to use other
operators like less than (<). In order words if we know that studentGrade >= 80 is false, studentGrade must be less
than(<) 80, which could also be greater than or equal to (>=) 65, as our following IF checks this, and so on, hope
this makes sense??
*/
3. LARGEST OF THREE NUMBERS
BEGIN
DECLARE number1, number2, number3, result
// We declare all the variables we are going to use. I’d explain as we go on!
INPUT “Enter a value for number one”
//We prompt the user to enter the first number
READ number1
// We read and store the value the user enters into variable ‘number1’
INPUT “Enter a value for number two”
//We prompt the user to enter the second number
READ number2
// We read and store the value the user enters into variable ‘number2’
INPUT “Enter a value for number one”
//We prompt the user to enter the last number
READ number3
// We read and store the value the user enters into variable ‘number3’
result <- number1
// Firstly, we assume the value in ‘number1’ is the greatest and store in ‘result’
/*
Note that we are going to use variable ‘result’ to store the largest number after our comparisons
*/
IF (result < number2) THEN
result <- number2
/*
First, we check if the value stored in result (remember we assumed this was number1) is less than the value in
variable ‘number2’ If this is the case (that is value in number2 is greater than value in number1, we forget about
what was in variable ‘result’ because hey ‘we now have a value greater than what was in result(number1) and now we
assign the value in number2 to result, so result is effectively number2
*/
ELSE-IF (result < number3) THEN
result <- number3
/*
If the previous condition fails, then by intuition number1 is greater than number2 so we would now proceed to
check and see if number2 is greater than number3... recall that if we firstly, stored number1 in result, and compared
it to number2, based on the result, we discarded one of the values (the smaller) and stored the larger into variable
result
*/
ENDIF
DISPLAY (result)
END
// Henceforth I would comment out only on the logic, some basic structures should be
// obvious by now
4. NUMBER SIGN DETECTOR
BEGIN
DECLARE number
INPUT “Enter a number to check”
READ number
IF (number < 0) THEN
DISPLAY “Negative”
// We check if the number is less than zero and display it
ELSE-IF (number > 0)
DISPLAY “Positive”
// Same, we check if the number is greater than 0 and display
ELSE
DISPLAY “Zero”
// If bot cases above fails, then obvious this number is neither less than, nor
// greater than zero. It must be zero itself by intuition
ENDIF
END
// Take note that ELSE executes only when all the conditions proposed in IF and IF-ELSE have
// failed, it’s like what should happen when all conditions fail, and we often use this, to
// effectively narrow-down our sample space
/*
For (4) above our sample space was {“greater than 0”, “less than zero”, “zero”}
Since the first 2 events failed in our test, we obviously know the only thingy left was the number is zero itself.
*/
// GUYS BY NOW Y’ALL SHOULD SEE HOW THIS THING IS LITERALLY SIMPLE LOGIC... JUST BREAK DOWN THE
STUFF INTO CHUNKS AND TRY SOLVING IT, THAT SHOULDN’T BE SO TEDIOUS ~P xd
// OOFFY IM TIRED OF TYPING LOL: XD ANYWAYS LET’S CONTINUE
// For this (5) I want to make this very easy, without a lot of branching... follow along
5. LEAP YEAR CHECKER :P
/*
Let’s use a simple definition
IF the year is divisible by 4, it’s a leap year
IF it is divisible by both 100 AND 400 THEN it is not a leap year
IF it is divisible by 400 THEN it is a leap year
*/
BEGIN
DECLARE year
INPUT “Enter the year e.g. (2000)”
READ year
IF (year % 4 = 0) THEN
// If the remainder when the value in variable ‘year’, divided by 4 has no remainder
DISPLAY “Leap year”
END
// The trick I am about to use here, isn’t what Sir taught, but I found it to be easier to understand. Bear with
// me!!!!!!
/*
See, we first check if that year is divisible by 4 by definition, if that is TRUE we simply display that it is a leap
year and end our program (no need for ENDIF anymore :P) why did I do so? because I would be so sure that if indeed our
program didn`t end, that year the user entered, must have left a remainder. makes sense? i.e. year % 4 != 0
Remember, ignore the comments, the computer does same :P let`s continue
*/
IF ((year % 100 = 0) AND (year % 400 = 0)) THEN
DISPLAY “Not a leap year”
END
// Hello! are you still there? OKAYY!!
/*
Recall the ‘AND’? also known as ‘^’ in logic. It is true only when both operands on each side are true, that is
‘FALSE ^ FALSE = FALSE’ right? likewise ‘TRUE AND TRUE = TRUE’ right????? YESS!!
Another thing to note is anything inside ‘(...)’ kinda always ‘EVALUATES’ to some value/data-type e.g. (number,
Boolean) etc so in our case (year % 4) evaluates to true or false, so the if statement effectively simplifies to
something like IF ( (TRUE) AND (TRUE) ) and since (TRUE) AND (TRUE) is the same as TRUE AND TRUE, which is TRUE, we
have just made our PROGRAM A WHOLEEE LOTT EASIERR SO we are checking if it is divisible by 100 ‘AND’ divisible by 400
:P wasn’t that easier? XD
*/
// We can do our last check as per the definition now: V
IF (year % 400 = 0)
DISPLAY “Leap year”
END
DISPLAY “Not a leap year”
// We use this last DISPLAY statement to make sure, it the worst of cases where all
// conditions above fails, our program won’t catch us OFF-GUARD and just doesn’t print
// anything :) #weAreWiser: V
// We can now gracefully end our algo, sadly based!
END
/*
I’ll end here for now, just practice these ones and understand what’s
happening, try the others if you face difficulties pose in the group :P maybe
someone somewhere could help you out, who knows!!! :P well, I have some very
pertinent courses to attend/study [:( they are hard lol]
PS: ~ BY AN ANONYMOUS CLASSMATE XD
Even if you know who it might be, trust your instincts, it’s not that person XD
But hey! 207 should make some sense now, right??
*/