Docsity

Pseducode notes for examination, Summaries of Computer science

Toggle sidebar
bg1
Pseudocode
There are 3 programming/pseudocode constructs:
1. Sequence: It refers that instructions should be executed one after another.
2. Selection: This construct is used to make a decision in choosing an option from
many available options on the basis of a condition. So, if a condition is true then one
option would be chosen while if a condition is false then another option will be
chosen.
3. Repetition: This construct is used to repeat a block of code as per the given
condition.
Algorithm is step by step solution to a given problem. It is written in plain
English statements. Algorithm is usually transformed into pseudocode or program
flowchart.
Once the algorithm is tested in pseudocode or program flowchart then finally it is
written in a specific programming language.
Pseudocode is a false code which consists of plain English statements,
mathematical notations and keywords that are commonly found in high level
languages.
It does not follow strict rules and style of any particular programming language.
Pseudocode is used for learning programming concepts and to describe ideas before
coding begins.
ARITHMETIC OPERATORS: In pseudocode arithmetic operators are used to
perform arithmetic operations. These operators are listed below:
Arithmetic Operator
Meaning
+
Addition
-
Subtraction
*
Multiplication
/
Division
^
Show power of a number
COMPARISION OPERATORS: These operators are used to compare different
values.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Partial preview of the text

Download Pseducode notes for examination and more Summaries Computer science in PDF only on Docsity!

Pseudocode

There are 3 programming/pseudocode constructs:

1. Sequence: It refers that instructions should be executed one after another. 2. Selection: This construct is used to make a decision in choosing an option from many available options on the basis of a condition. So, if a condition is true then one option would be chosen while if a condition is false then another option will be chosen. 3. Repetition: This construct is used to repeat a block of code as per the given condition.

Algorithm is step by step solution to a given problem. It is written in plain English statements. Algorithm is usually transformed into pseudocode or program flowchart.

Once the algorithm is tested in pseudocode or program flowchart then finally it is written in a specific programming language.

Pseudocode is a false code which consists of plain English statements, mathematical notations and keywords that are commonly found in high level languages. It does not follow strict rules and style of any particular programming language. Pseudocode is used for learning programming concepts and to describe ideas before coding begins.

ARITHMETIC OPERATORS: In pseudocode arithmetic operators are used to perform arithmetic operations. These operators are listed below:

Arithmetic Operator Meaning

  • Addition
  • Subtraction
  • Multiplication / Division ^ Show power of a number

COMPARISION OPERATORS: These operators are used to compare different values.

Assignment Operator: Assignment operator is used to assign the value or expression to a variable. The value or expression is on the right side of assignment operator while the variable is on the left side of the assignment operator.

It is denoted by either of the following:  

For eg: num1  5 num2  10 Sum  num1 + num Sum = num1 +num Sum := num1 +num

Note: In CIE Exam, mostly  sign is used for assignment operator.

In the above written pseudocode, num1 and num2 are the variable names where the two input number given by user will get store. The sum of num1 and num2 is a mathematical operation and the answer is saved in a variable named as sum. It is not necessary that name of variable should be same but it should be relevant. For example, the variable name Sum could be answer, ans, result etc.

Example 2: Write Pseudocode that will take two numbers as input, calculates their product and displays output. Solution: (Solve at your own)

Example 3: Write down Pseudocode that will take marks of physics, chemistry and math as input, calculates the average and displays output. Solution: WRITE “please enter marks of physics” READ Phy_marks WRITE “please enter marks of chemistry” READ Chem_marks WRITE “please enter marks of maths” READ math_marks Avg  (Phy_marks + Chem_marks + math_marks)/ WRITE Avg

TOTALLING & COUNTING: Totalling is a process to add up the series of number. A variable named as total or sum is used to hold on to the running total. So

Total  Total + Number

It actually means: Total(New Value)  Total(Old Value) + Value of Number

Note: To perform the totaling, total variable must be total  0 at the start of pseudocode.

Counting is a process to count how many times something happens.

Count  Count + 1 Count  Count - 1 Count  Count + 5

A variable named as count is used for counting purposes.

Sometimes, it is also represented as Count Incremented By 1 or Count Decremented By 1

REPETITION: The process of repeating a block of pseudocode is called as repetition. It is also known as looping or iteration.

There are three types of repetition statements

**1. FOR … TO …. NEXT

  1. WHILE… DO …. ENDWHILE
  2. REPEAT … UNTIL**

FOR… TO … NEXT

This repetition statement is used when we know how many times an instruction or set of instructions is to be repeated.

Few things to remember about FOR…TO…NEXT loop are:

 There is a variable in FOR … TO … NEXT loop for controlling the number of iterations and is known as a control variable. The name of the control variable is usually ‘Count’.  We specify the initial (lower) and final (higher) values of the control variable in the opening statement of the loop. These initial and final values are not restricted to the numerical values only, they can be variables as well.  The Control Variable is automatically incremented by ‘1’ each time the loop ends.  The value of the control variable is tested at the beginning of the loop & loop is repeated until the value of control variable is less than or equal to the specified final value.

Example 4: Write pseudocode that will take 10 numbers as input and print their average by using FOR…TO…NEXT loop. Solution:

WRITE “Average Sunshine hours for whole year is:”, AvgShrs

Example 6: Mr. John wants to calculate the average marks for his class of 28 students for Urdu, Islamiat and Pak.studies. Write down pseudocode that will help him to accomplish this task. Solution: (Solve at your own)

WHILE … DO … ENDWHILE

This repetition statement is used when we don’t know how many times an instruction or set of instructions is to be repeated.

Few things to remember about WHILE…DO…ENDWHILE loop are:

 The loop is repeated until a condition is true and halted when the condition is false.

 Condition is tested at the beginning of the loop.  The statements/instruction between DO and ENDWHILE keywords are repeated.

Example 7: Write pseudocode that will take numbers input and add them while the input number is greater than or equal to 0. Print the final result. Solution Total  0 WRITE “Input Number” READ num

WHILE num>= 0 DO Total  Total + num READ num ENDWHILE

WRITE “Total sum is:”, Total

Example 8: Write pseudocode that will take numbers input, add them and calculates the average while the input number is greater than or equal to 0. Print the average of all input numbers. Solution: Total  0 Count  1

WRITE “Input Number” READ num

WHILE num>= 0 DO Total  Total + num READ num Count  Count + 1 ENDWHILE

avg  Total/Count WRITE “The Average is:”, avg

Note: Although there is a difference of operation between FOR…TO…NEXT Loop and WHILE…DO…ENDWHILE Loop but still there is a possible way to rewrite a code written in FOR…TO…NEXT loop into WHILE…DO…ENDWHILE Loop.

READ num Total  Total + num NEXT

avg  Total/Count WRITE “The Average is:”, avg

Example 11: A pseudocode is given below which will take age of 18 students and print out the min age of students. Rewrite all the pseudocode using WHILE…DO…ENDWHILE loop.

WRITE “Enter age of student” min_age  100 FOR COUNT  1 TO 18 READ age IF age < min_age THEN min_age  AGE ENDIF NEXT WRITE “Minimum AGE:” min_age

Solution: (Solve it at Your Own)

Example 12: Rewrite the code written below using WHILE…DO…ENDWHILE Loop. Total_temp  0

Total_Shrs  0

FOR Count  1 to 365

WRITE “Please enter temperature”

READ temp

Total_temp  Total_temp + temp

WRITE “Please enter sunshine hours”

READ Shours

Total_Shrs = Total_Shrs + Shours

NEXT

Avgtemp  Total_temp/

AvgShrs  Total_Shrs/

WRITE “Average temperature for whole year is:”, Avgtemp

WRITE “Average Sunshine hours for whole year is:”, AvgShrs

Solution:

REPEAT … UNTIL

It is a repetition statement that is used when we don’t know how many times an instruction or set of instructions is to be repeated.

 It is different from WHILE… DO … ENDWHILE because this loop will be repeated until a condition is false and it will stop executing once the condition is true.

ELSE IF num = 0 THEN WRITE “The number is zero” ELSE WRITE “The number is negative” ENDIF ENDIF

Example 16: Write pseudocode that will take marks in an exam as input and will tell the grade A* for 90 to 100, A for 80 to 89, B for 70 to 79, C for 60 to 69 and F grade for 0 to

WRITE “Enter Marks” READ marks IF marks >= 90 AND marks <= 100 THEN WRITE “The Grade is A*” ELSE IF marks >= 80 AND marks < 90 THEN WRITE “The Grade is A” ELSE IF marks >= 70 AND marks < 80 THEN WRITE “The Grade is B” ELSE IF marks >= 60 AND marks < 70 THEN WRITE “The Grade is C” ELSE IF marks >= 0 AND marks < 60 THEN WRITE “The Grade is F” ELSE WRITE “Invalid Marks” ENDIF ENDIF ENDIF ENDIF ENDIF

Note: Each IF statement and ELSE IF statement must have an ENDIF statement to terminate.

Example 17: Write pseudocode that performs the following: Ask a user to enter a number. If the number is between 0 and 10, write the word blue. If the number is between 10 and 20, write the word red. if the number is between 20 and 30, write the word green. If it is any other number, write that it is not a correct color option.

Solution: (Solve at Your Own)

Example 18: Write down pseudocode that will take three numbers as input and tells (a) The Largest Number (b) The Smallest Number

Solution: (a) WRITE “Enter three numbers” READ num READ num READ num

IF num1 > num2 AND num1 > num THEN WRITE “The largest Number is”, num ELSE IF num2 > num1 AND num2 > num THEN WRITE “The largest Number is”, num ELSE WRITE “The largest Number is”, num ENDIF ENDIF

Solution: (b) Solve at Your Own

7: DAYNAME  ”SUNDAY”

OTHERWISE

WRITE “ERROR”

DAYNUM  ”UNKNOWN”

ENDCASE

WRITE”THE DAY IS:”, DAYNAME

Example 20: write a pseudo code that will take two numbers as input perform any of the basic operation (+,-,*,/) as per user requires Solution: WRITE “ENTER TWO NUMBER” READ NUM READ NUM WRITE “ENTER THE OPERATION” READ OPRT CASE OPRT OF

  • : ANS  NUM1 + NUM
  • : ANS  NUM1 – NUM / : ANS  NUM1 / NUM 2
  • : ANS  NUM1 * NUM

OTHERWISE

WRITE : "INVALID OPERATION "

Ans  "Invalide" ENDCASE WRITE: “Answer is: ” , Ans

Practice Questions (Past Paper)

In CIE exam, mostly the pseudocodes are asked that can select maximum from many values, minimum from many values, can calculate average of given values, can count the number of digits in a given input or number and then performing various operations on its basis.

Question1:

Question 3:

Question 4: