0% found this document useful (0 votes)
15 views19 pages

Computer Algorithms and Programming Basics

Uploaded by

dilmiherath07
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)
15 views19 pages

Computer Algorithms and Programming Basics

Uploaded by

dilmiherath07
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

Subject : Information & Communication Technology

Grade :11
Lesson : Writing programs to solve problems- Unit 01

This section discusses computer-based solutions to a problem in society


What is an algorithm?

 An Algorithm is, a systematic methodology used to solve a problem.


 In here, all the steps required to solve the problem are given step by step.
Problem solving phases

 Problem analysis
 Building an algorithm for the Problem
 Develop a computer program and correcting errors.
 Executing a computer program
Use this QR code or this link to learn more about this lesson
through videos.
Problem analysis [Link]
In analyzing the problem, Considers

 inputs
 processes
 outputs

Building Algorithm for Problem

Algorithm

Graphically Method Textually Method

Flow Chart Pseudo Code

Begin
Input Age
If Age>= 18 then
Display “Adult”
else
Display “Child”
end if
end

Department of Education - Western Province - Development Division 1


The algorithm is considered under the following main control structures
Control structures

(i). Sequence  Flowing gradually flow charts


(ii). Selection  Choose one of two options.
(iii). Repetition  Repeating number of times

Key Symbols Used to Draw Flow Charts

connector

Start/Stop
Input/output Process
Decision
Data Flow

(01). Sequential type flow charts, pseudo-codes, Pascal codes

 When two numbers are given, add them and show the answer
Let us take the first number as X, the second number as Y, the answer as TOTAL.

FLOWCHART PSEUDO CODE PASCAL CODE


ගැලීම් සටහන ව්‍යාජ ක ත ය පැස්කල් ත ය
BEGIN Program addition; The name of the program
INPUT X,Y Var X,Y,TOTAL: Integer; Introducing the variable
TOTAL = X + Y Begin Start
DISPLAY Writeln(‘Enter First no:’); X input
TOTAL Read(X);
END Writeln(‘Enter Second no:’); Y input
Read(Y);
Collection processing
TOTAL:=X+Y;
Writeln(‘Total is ‘ , TOTAL); Show the answer
END. the end

Department of Education - Western Province - Development Division 2


(02) Selective type flow charts , pseudo-codes, Pascal codes
 Find the largest number given two numbers
Let us take the two numbers x and y.
FLOWCHART PSEUDO CODE PASCAL CODE
ගැලීම් සටහන ව්‍යාජ ක ත ය පැස්කල් ත ය
BEGIN Program FINDMAX; The name of the program
INPUT X,Y Var X,Y: Integer; Introducing the variable
Begin Start
IF X>Y THEN Writeln(‘Enter First no:’); Input X
DISPLAY X Read(X);
ELSE Writeln(‘Enter Second no:’); Input Y
DISPLAY Y Read(Y);
END IF if X > Y then See if Y is greater than X. [Check if X> Y
writeln(X) is true]
END else Show the answer
Writeln(Y); If not
End. Show the answer
the end

03).පුනර්කරණ ආකාරතේ ගැලීම් සටහන් , ව්‍යාජ ක ත , පැස්කල් ත


FLOWCHART PSEUDO CODE PASCAL CODE
ගැලීම් සටහන ව්‍යාජ ක ත ය පැස්කල් ත ය
BEGIN Program FINDMAX; The name of the program
X=1 Var X: Integer; Introducing the variable
Begin Start
While X<=10 X:=1; Assign the value of X as 1
Display X while X<=10 do Check if the value of X is 10 or less
X=X+1 Begin Initiation of recurrence
End While writeln(X); Show the current value of X
END X:=X+1; Add one to the current value of X and assign it
End; back to X.
End. End of repetition
the end

Array
The array is used to store values in memory using the same name for data belonging to the
same data type.
Features of an Array -

 Belonging to the same data type.


 Use one variable name
 Ability to provide as much data as needed

Department of Education - Western Province - Development Division 3


Example -
var marks : array [0..9] of integer;

Name of the variable - marks


Data type - integer (whole numbers)

Length - 10 (0 to 9)
marks[0]

marks[1]

marks[2]

marks[3]

marks[4]

marks[5]

marks[6]

marks[7]

marks[8]

marks[9]
45 20 10 50 78 89 55 98 65 32
Name of array  marks
Display values in an array.
In pascal output In pseudocode output
write(marks[0]); 45 Display(marks[0]) 45
write(marks[1]); 20 Display(marks[1]) 20
write(marks[2]); 10 Display(marks[2]) 10
write(marks[3]); 50 Display(marks[3]) 50
write(marks[4]); 78 Display(marks[4]) 78
write(marks[5]); 89 Display(marks[5]) 89
write(marks[6]); 55 Display(marks[6]) 55
write(marks[7]); 98 Display(marks[7]) 98
write(marks[8]); 65 Display(marks[8]) 65
write(marks[9]); 32 Display(marks[9]) 32

Assign values to an array


In pascal In pseudocode
marks[4]

marks[7]
marks[0]

marks[1]

marks[2]

marks[3]

marks[5]

marks[6]

marks[8]

marks[9]

marks[0]:=500; marks[0]=500
marks[1]:=300; marks[1]=300
300 300 800 600 78 89 55 98 65 32
marks[2]:=800; marks[2]=800
marks
marks[3]:=600; marks[3]=600

Department of Education - Western Province - Development Division 4


Add the values in the array and assign them to the same array

marks[0]

marks[1]

marks[2]

marks[3]

marks[4]

marks[5]

marks[6]

marks[7]

marks[8]

marks[9]
marks[6]:= marks[7]+ marks[8]
marks[6]:= 98 + 65
marks[7]:= marks[6]+ marks[5]
marks[7]:= 163 + 89 300 300 800 600 78 89 55 98 65 32
marks[5]:= marks[3]+ marks[0] 300 300 800 600 78 89 163 98 65 32
marks[5]:= 600 + 300
300 300 800 600 78 89 163 252 65 32
300 300 800 600 78 900 163 252 65 32

Printing values in an array When marks[x]


x=……
Program arrayOut (output); x=0 marks[0] 300
var x : integer; x=1 marks[1] 300
Begin x=2 marks[2] 800
For x := 0 to 9 do x=3 marks[3] 600
Writeln(marks[x]); x=4 marks[4] 78
End. x=5 marks[5] 900
x=6 marks[6] 163
x=7 marks[7] 252
x=8 marks[8] 65
x=9 marks[9] 32

Model questions
(01). When analyzing a problem, it is important to identify,
1. Input
2. Inputs and outputs
3. outputs
4. Inputs, outputs and processes

(02). An "algorithm" is,


1. The method of action to be followed to solve a problem.
2. The process of inputs and outputs.
3. The process of showing whether a condition is satisfied or not.
4. The process of beginning and ending.

Department of Education - Western Province - Development Division 5


Y N
(03). This flow chart ……… .. control structure is used If
1. Sequence
2. Selection
OUTPUT A OUTPUT B
3. Repetition
4. Iteration

(04). According to the pseudo code given


A. The value of T is less than 50. Begin
B. The value of X is an odd value less than 50. Set X=1
C. The value of T is an even number less than 15. Set T =1
D. The value of X is an odd value less than 15. While T < 50
1. A is correct 2. A and B are correct Display T
X= X+2
3. C is correct 4. C and D are correct
T=T+X
End While
End
(05). The output is according to the given pascal code
Program abc;
1.1 2. 1 2 3. 1 4. 1 2 3 Var X: integer;
2 2 Begin
3 X:=1;
Repeat
Write(X);
X:=X+1;
Until X=3;
End.
(06). Consider the following pseudo code segment. How many times the word HELLO
appears when the code is execute
Count=0
1.4 Repeat
2. 5 Display(“HELLO”)
3. 6 Count=Count+1
4. 4 UNTIL count>4
Display(“HELLO”)
Count=count-1
End while

(07).Consider the flow chart below. What is the final value of M?


1. 65 2 .70 3. 50 4 .60

Department of Education - Western Province - Development Division 6


(08). What is the output of following program?
Program command(output);
var x , count: integer;
Begin
For x := 0 to 2 do
Write(„A‟);
For x:= 1 to 3 do
Writeln(„B‟) ;
ReadIn;
End.
1 2 3 4
AAA AAAB AAA AAAB
B B BBB B
B B B
B

(09).Which of the following answers includes the correct variable names that can be used
in a Pascal program?

1. Std_no, stdno 2. A15, 10B 3. 10A, std-no 4. std no, Al0

(10). Consider the pseudo-code below, which includes the label A, to obtain a sum of 10
digits.
Begin
total = 0
count = 1
Repeat
Input number
total = total + number
count = count + 1
………A…………
Display total
End.
What is the correct expression for label A?
1. until count =11
2. until count = 10
3. while count = 11
4. while count = 10

Department of Education - Western Province - Development Division 7


(11) Consider the following statements regarding the Pascal programming language.
A - Interpreter reads line by line and translates into machine language,
converting it into machine code each time the program is executed.
B - The compiler translates the entire program into machine language at once
and can run it as many times as needed once the program has been
converted into machine code.
C - An assembly language used to translate programming written into
assembly language into machine language.
The above statements are true,
1. A only.
2. B only.
3. A and C only.
4. A, B and C all.

(12) Which of the following answers shows the values of variables A and B, respectively,
when the following statements are executed in a Pascal program?
A:= 9 MOD 10 ;
B:= 10 DIV 1 ;
1. 10,10
2. 9,1
3. 9, 10
4. 1,1

(13). What is the output of the following Pascal program?


Program print_HOW (input , output);
Var count : integer;
Begin
For count:= 0 to 5 do
Write(„HOW‟);
ReadIn;
End.
1. HOW HOW HOW HOW HOW HOW
2. HOW HOW HOW HOW HOW
3. HOWHOWHOWHOWHOW
4. HOWHOWHOWHOWHOWHOW

(14).The output of the following pseudo code is,


Begin
X=2
For Y = 1 to 3
X = X+ Y
Next Y
Display X
End.

1. 7
2. 10
3. 8
4. 5

Department of Education - Western Province - Development Division 8


Start

X=0
(15). What is the output from this flow chart?
1. Consecutive numbers from 1 to 10
X=X+1
2. Consecutive numbers from 1 to 9 Display X

3. Consecutive numbers from 2 to 10


4. Consecutive numbers from 0 to 10 Yes
X<10
No
(16).What is the output if x = 1 is used instead of x = 0 in this flow End
chart?
1. Consecutive numbers from 2 to 10
2. Consecutive numbers from 2 to 9
3. Consecutive numbers from 1 to 11
4. Consecutive numbers from 1 to 10
(17). What is/are the Control Structure(s)included in this flow chart?
1. Iteration
2. Sequence
3. Selection
4. All of the above

(18). UCC: array [1..10] of string;


Misrepresentation of the array created by the program command
1. Create an array that can store up to 10 items.
2. 10 numaric data can be stored here.
3. Items in the array can be accessed by numbers 0 to 9.
4, can only be used to store data on 5 items in the array.

(19). The number of repetitions performed in the following loop is:


S: = 10;
Repeat
S: = S +5;
Writeln (S);
Until S> 30;
1. 1
2. 2
3. 4
4. 5
(20). Var M: Array [0..4] of integer; The size of the above array is

1. 4. 2. 5. 3. 6. 4. 0.

(21). There are two types of sub - programs . A sub - program which returns an output back
to the main program and a sub – program which does not return an output back to
the main program. The above sub-programs are called respectively is……

1. function and procedure. 2. Procedure and function .


3. function and sub function 4. Procedure and sub function

Department of Education - Western Province - Development Division 9


(22). Is not a feature of a program written in machine language
1. Fast in operation
2. A translation program is essential.
3. Using only 0s and 1s
4. Dependency on machines

(23). Is a special feature of a program written in the Assembly language


1. Fast in operation
2. A translation program is not essential.
3. Using only 0s and 1s
4. Use a translator called Assembler.
(24).Is not a HIGHLEVEL LANGUAGE
1. Fortran
2. Basic
3. pascal
4. BLISS

(25). Is not a feature of a higher level language


1. Easy to understand
2. Should be converted into machine language instruction.
3. Dependency on machines
4. Being faster

(26). An alternative method of computer programming is called……………………..


What Is the word that best fits the space.
1. Paradigm
2. Procedural
3. Declarative
4. Structured

(27) The two basic building blocks of object-oriented programs are……..


1. Data and methods
2. Methods and classes
3. classes and objects
4. Objects and data

(28). An examples of scripting languages are ……………….


1. Fortran and Basic
2. Fortran and Java
3. Java and PHP
4. PHP and JavaScript

Department of Education - Western Province - Development Division 10


(29). Which of the following statement(s) is/are true?
A- Interpreter and Compiler are two translator programs.
B- The interpreter program is converted into machine code each time it is run.
C- Compiler translates programming into machine language at once.
1. A only
2. A and B only
3. A, B and C
4. A and C only

(30). According to the evaluation of pascal expression (5 + 14 MOD 4) is …….


1. 7. 2. 6 3. 5 4. 3

(31). According to the evaluation of pascal expression, NOT (8MOD 2> 5) is


1. 7. 2. 6. 3. True. 4. False.

(32). What is the answer given in evaluating the expression 13-2 * 5 + 4 ^ 3 in the
computer program?
1. 119 3. 15
2. 67 4. 136

(33). What is the value that Display X outputs in the given pseudo code?
Begin
X=1
While X <10
X=X+1
End while
Display X
End
1. 10 2. 11
3. 9 4. 1
(34). What is the value that Display Z outputs in the given pseudo code?
Begin
a=1
b=1
While a = b
a=a+1
b=b+2
Z=a+b
End while
Display Z
End

1. 2 2. 5
3. 3 4. 4

Department of Education - Western Province - Development Division 11


(35). What is the output value of the pseudo code below?
Begin
a = 10
c=2
Repeat
c=c+1
a=a+c
Until ( c > 3 )
Print a
End
1. 13 2. 17
3. 12 4. 10

36). If n is an integer variable, how many times does the following code work?
n=8
m=4
Do while m > 3
n=n-1
Loop

1. execute 3 times.
2. execute 2 times.
3. It never ends.
4. execute one time.

(37) .If n is an integer variable, how many times does the following code work?
n=6
m=2
Do while n > 4
n = n -1
Loop
1. execute 3 times. 2. execute 2 times.
3. It never ends. 4. execute one time.

(38). If m is an integer variable, how many times does the following code work?
m=4
Do while m > 5
m=m-1
Loop
1. execute 2 times. 2. It never starts.
3. It never ends. 4. execute one time.
(39) Which of the following answers does the logical structure of the pseudo-code
above indicate?
1. A sequence
2. A simple choice
3. A Sequence and a repetition
4. A simple choice
Department of Education - Western Province - Development Division 12
(40). What are the answers given by evaluating the following two expressions?
2 ^ 3 + 2> 4 * 3 and 6/2 + 5 <5 * 3 - 2 Start
1. False, true
2. False, false
3. True, false C=0
4. True, True

N
(41) what is the output of this flowchart? C> C=C+
1
Y
1. 4
2. 3 Print C
3. 2
4. 0
(42) If m is an integer variable, how many times does the following
Stop code work?
m=0
Do while m <5
m=m-1
Loop
1. execute 4 times. 2. . execute 5 times..
3. execute 6 times.. 4. execute one time.

(43) What is the output of the given pseudo code?

Begin
a=5 Start
b=50
Do While a<=b
a=b/a M=30
End while
Display a
End
Y
1. 10 IF
2. 5 M=M+100
M<50
3. 1
4. 50
N
(44) Consider the flow chart below. What is the final
value of M? M=M/5
1. 30
2. 6
End
3. 130
4. 35

Department of Education - Western Province - Development Division 13


(45). What control structures does this flow chart have?

A. Sequence
B. Selection
C. Repetition

1. A only
2. A and C only
3. B and C only
4. A and B only

(46). What is the final value of M if given as M = 4 at the beginning?


1. 104 2. 4/5 3. 134 4. 109

(47) consider the following pseudocode .


X=7
While count > 3
Display (“HELLO”)
Count = count - 2
End while

How many times the word "HELLO" display in this ?


1. 7 2. 3 3. 2 4. 5
(48) consider the following pascal program
Program repetition (input, output);
Var X : integer;
Begin
X:= 1;
Repeat
X := X + 1 ;
Write (X) ;
Until X = 3;
End.
What is the output of the above program?
1. 23 2. 2 3. 1,2,3 4. 1
3 2
3

Department of Education - Western Province - Development Division 14


(49) Consider the following Pascal expressions.
A 2*3 B 7 mod 4 C 5 div 2
Select the correct order of outputs of above expressions from the given answers.
1. 6,2,3 2. 2,6,3 3. 6,3,2 4.3,6,2

(50) consider the following expressions.


A (1 > 2) or (4 > 2) B (1 > 2) and (4 > 2) C not (4 > 2) and (1 > 2)

Select the correct order of outputs of above expressions from the given answers.
1. True, False, False
2. True, False, True
3. True, True, False
4. True, True, True

PART 2

(01).
i) The pseudo code written to get the sum of all the even numbers from 1 to 20 is given
below. Fill in the blanks. (5 points).

..............
Sum=0;
N=2;
For ............. = 2 to .............
Sum = .................+N
N = ................+2
Next N
Print ..........................
End.

ii) Draw the relevant flow chart to obtain this output

(02).
(a). When given three numbers, a pseudo-code is given to find the largest number. Fill in the
blanks in this pseudo code.

Begin
Input x,y,z
If (x>y) and (x>z) then
print ……A………
else

Department of Education - Western Province - Development Division 15


if y > z then
print ……B……
………C……….
Print ……D……
………E…………
end if
End

(b). Draw a flow chart related to that pseudo code.

(03) .If given three numbers, consider the flow chart given to find the smallest number.
Fill in the blanks here.

Department of Education - Western Province - Development Division 16


(04) .The following is a pseudo-code used to calculate the total number students in a class
and the average height of the student in the class using the height of each student. Fill in the
blanks in that pseudo-code and draw a flow chart for it.

Begin
total=0
Input “Enter No. of Student” = N
Avg = 0
For Counter = 1 to ……A……
W=0
Input “student weight” as …….B…..
total= ……..C…… + w
Next counter
avg = total/…..D……
Print “Average weight of students”, …….E…….
End.

Where total is the sum of the weights, N is the number of students and avg is the average
value of the students' weight.
(05) .Write the answers using the flow chart below.

Start a) What is the output of this flow chart?


b) Write the output when X = 75 is given here.
X=100 c) What is the return on transfer of yes, No in this flow
chart?

Print x

X=X-10

X>50

stop

Department of Education - Western Province - Development Division 17


(o6).
(a). Below is the pseudo code written to get the sum of all the even numbers from 1 to 100.
Fill in the blanks. (5 points).
X=
TOT=
While X<=
TOT=TOT + 
X=X+
End while
Print TOT

(b). Draw a flow chart that matches the pseudo code below.
Begin
Input X,Y
If X > Y then
MAX=X
Else
MAX=Y
End if
Print MAX
End

(07).
(a). Consider the following marks array. Five students' ICT marks were included and an
algorithm was written to read and print them. Name A, B, C, D here.

0 1 2 3 4
13 22 56 78 54
marks
BEGIN
x=0
while x<=….A….
M=marks*….B….]

Display ….D….
….C….=x+1
end while
END

(b). Draw a suitable flow chart for this.

Department of Education - Western Province - Development Division 18


(08).
(a). Consider the array P below. This flow chart is designed to print the number N as "YES" if
the number is in the array P. Write a pseudo code for this.

0 1 2 3 4
100 300 500 400 800
P

Prepared by
Chanaka Konthasinghe
ISA - Education Zone, Gampaha

Translated by
Shyamali Ranasinghe
ICT Teacher, Ovitigama Boodiraja M.V. , Pugoda
Use this QR code or this link to learn more about this lesson through videos.
[Link]

Department of Education - Western Province - Development Division 19

You might also like