0% found this document useful (0 votes)
13 views4 pages

Ix p2 Final Term Handout 3

The document outlines methods for handling input in programming, including fixed-length input, user-defined length input, and input until a sentinel value is reached. It also details standard methods for solving problems with arrays, such as calculating total, average, minimum, maximum, performing linear search, and bubble sort. Example pseudocode is provided for each method to illustrate the concepts.

Uploaded by

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

Ix p2 Final Term Handout 3

The document outlines methods for handling input in programming, including fixed-length input, user-defined length input, and input until a sentinel value is reached. It also details standard methods for solving problems with arrays, such as calculating total, average, minimum, maximum, performing linear search, and bubble sort. Example pseudocode is provided for each method to illustrate the concepts.

Uploaded by

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

HANDOUT – 3 Final Term (2024-2025)

(Standard Methods of solution)


Name: Class: Roll: Section:
Subject: Computer Science Paper 2 Teachers: Md. Shahinur Alam Date:

1. Three Common Types of Input:


1.1. Fixed-Length Input: Example – Enter marks for 5 students.

DECLARE marks: Array[0:4] OF INTEGER

FOR i ← 0 TO 4
DISPLAY "Enter marks for student: "
INPUT result
marks[i] ← result
NEXT i

1.2. User-Defined Length: First, ask the user how many values they want to enter, then
take input accordingly.

OUTPUT "How many values do you want to enter?"


INPUT n

DECLARE marks: Array[0:n-1] OF INTEGER

i ← 0

REPEAT
OUTPUT "Enter marks for student:"
INPUT result
marks[i] ← result
i ← i + 1
UNTIL i = n
* same algorithm using FOR loop

OUTPUT "How many values do you want to enter?"


INPUT n

DECLARE marks: Array[0:n-1] OF INTEGER

FOR i ← 0 TO n-1
OUTPUT "Enter marks for student: "
INPUT result
marks[i] ← result
NEXT i

1.3. Input Until Sentinel Value: Keep taking input until the user enters -1.

1
DECLARE marks: Array OF INTEGER
i ← 0

REPEAT
OUTPUT "Enter marks for student (enter -1 to stop): "
INPUT result

IF result <> -1 THEN


marks[i] ← result
i ← i + 1
ENDIF
UNTIL result = -1

2. Standard method of Solutions


(Total, Average, Count, Min, max, Linear Search, Bubble sort)
Data is given as array here (we might take input if asked so in question). Here the example
data set is marks = [20, 15, 10, 17, 18, 9, 19]

Example of basic Loop algorithm: Print all the values


marks = [20, 15, 10, 17, 18, 9, 19]

FOR i ← 0 TO 6 FOR i ← 0 TO LENGTH(marks)-1


OUTPUT marks[i] OUTPUT marks[i]
NEXT i NEXT i

2.1. Total, Average

total ← 0

FOR i ← 0 TO 4
total ← total + marks[i]
NEXT i

average ← total / 5
OUTPUT "Total Marks: ", total
OUTPUT "Average Marks: ", average

2.2. Count

count ← 0

FOR i ← 0 TO 4
IF marks[i] >= 90 THEN
count ← count + 1
END IF
NEXT i

OUTPUT "No of Passed students: ", count

2
2.3. Min, max

MinMark ← marks[0]
MaxMark ← marks[0]

FOR i ← 0 TO 4
IF marks[i] < MinMark THEN
MinMark ← marks[i]
ENDIF

IF marks[i] > MaxMark THEN


MaxMark ← marks[i]
ENDIF
NEXT i

OUTPUT "Minimum Mark is: ", MinMark


OUTPUT "Maximum Mark is: ", MaxMark

2.4. Linear Search

Example 1 : Write an algorithm to find if any student got 20.


marks = [20, 15, 10, 17, 18, 9, 19]

searchNum ← 20
found ← false

FOR i ← 0 TO 6
IF marks[i] = searchNum THEN
found ← true
ENDIF
NEXT i

IF found = true THEN


OUTPUT "Searched value is found in the array"
ELSE
OUTPUT "Not found"

Example 2 : Write an algorithm to find the name “Don” from the given array names.
names = [“Jamal”, “Kamal”, “Rahim”, “Don”]

searchName ← “Don”
found ← false

FOR i ← 0 TO 3
IF names[i] = searchName THEN
found ← true
ENDIF
NEXT i

IF found = true THEN


OUTPUT "Searched Name is found "
ELSE
OUTPUT "Not found"

3
* The pseudocode line ( IF found THEN ) is the short form of ( IF found = true THEN )
both are correct.

2.5. Bubble sort


Write an algorithm to sort the students mark array in ascending order.
marks = [20, 15, 10, 17, 18, 9, 19]

Note: sorted array after running the algorithm should be [9, 10, 15, 17, 18, 19, 20]

marks = [20, 15, 10, 17, 18, 9, 19]

REPEAT
Swap = false
FOR i ← 0 TO 5
IF marks[i] > marks[i+1] THEN
Swap = true
temp ← marks[i]
marks[i] ← marks[i+1]
marks[i+1] ← temp
ENDIF
NEXT i
UNTIL Swap = false

* Optimised version of this algorithm. Updates are highlighted

marks = [20, 15, 10, 17, 18, 9, 19]


First ← 0
Last ← 6

REPEAT
Swap = false
FOR i ← First TO Last -1
IF marks[i] > marks[i+1] THEN
Swap = true
temp ← marks[i]
marks[i] ← marks[i+1]
marks[i+1] ← temp
ENDIF
NEXT i

Last ← Last - 1
UNTIL Swap = false OR Last = 1

* The pseudocode line ( UNTIL Swap OR Last = 1) is the short form ( UNTIL Swap =
false OR Last = 1) both are correct.

You might also like