Vous êtes sur la page 1sur 21

Information Technology in FOCUS for CSEC

SECTION 3 OF SYLLABUS: CHAPTER 36: Implementing the Program – Loop Constructs in Pascal

Activity # 43 - a

TEST YOUR KNOWLEDGE


Chapter 36: Implementing the Program – Loop Construct in Pascal

INSTRUCTION: Answer all questions.

1. What is a loop? (1 mark)

Possible Solution:
In a Pseudocode, a loop is a sequence of instructions that is continually repeated until a certain
condition is reached.

a. List TWO types of loops. (2 marks)

Possible Solution:
1) FOR
2) WHILE DO

b. Explain each. ( 2 marks)

Possible Solution:
FOR Loop construct
This repetition statement is called bounded iteration (iteration is the repetition of a sequence of
computer instructions a specified number of times or until a condition is met). This means that a
program will count to a definite number of times and might also carry out a procedure a certain
number of times. These actions are represented using the FOR construct.

WHILE construct
This construct is called unbounded or indefinite iteration. The WHILE statement is one of the
control flow statements that enables the execution of a sequence of logic multiple times in a
loop, until a specific condition is false.

This statement is generally used when the number of iterations to be executed is not known and
cannot be estimated.

2. Write an algorithm in Pseudocode and Pascal that will prompt the user to read the
amount of rainfall in the month of his birth. Display the total rainfall for that month.
(10 marks)

1
Information Technology in FOCUS for CSEC
SECTION 3 OF SYLLABUS: CHAPTER 36: Implementing the Program – Loop Constructs in Pascal

Possible Solution:

PSEUDOCODE

Start
Set TOT 0
FOR RAIN_FALL 1 TO 31 DO
Write “Enter amount of rainfall”
Read TOT
Set TOT  TOT +1
Set TOT TOT + RAIN_FALL
Write “The total rain fall for the month is”, TOT
ENDFOR
Stop

PASCAL

PROGRAM Rainfall (INPUT, OUTPUT);


USES CRT;
VAR
TOT, RAIN_FALL: integer;

BEGIN
TOT:= 0;
FOR RAIN_FALL: = 1 TO 31 DO
BEGIN
Writeln (‘Enter amount of rainfall’);
Readln (TOT);
TOT:=TOT +1;
TOT:= TOT + RAIN_FALL;
Writeln (‘The total rain fall for the month is: ', TOT);
Readln;
END;
END.

3. Write an algorithm in Pseudocode and Pascal that will accept 47 schools in a schools’ challenge
quiz. Print the number of schools that scored 10. (10 marks)

2
Information Technology in FOCUS for CSEC
SECTION 3 OF SYLLABUS: CHAPTER 36: Implementing the Program – Loop Constructs in Pascal

Possible Solution:
PSEUDOCODE
Start
Set SCHOOL  0
Set COUNT  0
FOR COUNT  1 TO 47 DO
Write “Enter school’s score”
Read SCORE
IF SCORE = 10 THEN
SCHOOL  SCHOOL + 1
Write “the total number of schools that scores 10 are”, SCHOOL
ELSE
Write “No school scored 10”
END
ENDFOR

PASCAL
PROGRAM Schools_Challenge_Quiz (INPUT, OUTPUT);
USES CRT;
VAR
SCHOOL, SCORE, COUNT: integer;
BEGIN
SCHOOL:= 0;
COUNT:=0;
FOR COUNT : = 1 TO 47 DO
BEGIN
Writeln (‘Enter School Score’);
Readln (SCORE);
IF SCORE = 10 THEN
BEGIN
SCHOOL:= SCHOOL +1;
Writeln (‘The total number of schools that scored 10 are: ', SCHOOL);
END
ELSE
BEGIN
Writeln (‘No school scored 10’);
END;
Readln;
END;
END;
END.
TOTAL 25 MARKS

3
Information Technology in FOCUS for CSEC
SECTION 3 OF SYLLABUS: CHAPTER 36: Implementing the Program – Loop Constructs in Pascal

Activity # 44 - b

TEST YOUR KNOWLEDGE


Chapter 36: Implementing the Program – Loop Construct in Pascal

INSTRUCTION: Answer all questions.

1. Write an algorithm in PASCAL which will accept the gender of students at CSS and
output the number of male students as well as female students. The algorithm should
terminate when you enter “Stop”. (5 marks)

Possible Solution:

PSEUDOCODE

Start
MALE_COUNT  0
FEMALE_COUNT  0
Write “Enter student gender”
Read GENDER
WHILE GENDER <> “STOP”
IF GENDER  MALE THEN
MALE_COUNT  MALE_COUNT + 1
ELSE
FEMALE_COUNT FEMALE_COUNT +1
Write MALE_COUNT, “ is the number of Males”
Write FEMALE_COUNT, “is the number of FEMALES”
Write “Enter Gender or STOP to terminate the program”
Read GENDER
ENDIF
ENDWHILE

PASCAL

PROGRAM Students_Total_Gender (OUTPUT, INPUT);


USES CRT;
VAR
GENDER, MALE, FEMALE: string;
MALE_COUNT, FEMALE_COUNT: integer;
BEGIN

4
Information Technology in FOCUS for CSEC
SECTION 3 OF SYLLABUS: CHAPTER 36: Implementing the Program – Loop Constructs in Pascal

CLRSCR;
MALE_COUNT: =0 ;
FEMALE_COUNT:=0;
Write (‘Please enter Student Gender: ');
Readln (GENDER);
Writeln;
WHILE GENDER <> ‘STOP’ DO
BEGIN
IF GENDER = MALE THEN
BEGIN
MALE_COUNT := MALE_COUNT + 1;
Writeln (MALE_COUNT, ‘is the number of Males’);
END
ELSE
BEGIN
FEMALE_COUNT := FEMALE_COUNT + 1;
Writeln (FEMALE_COUNT, ‘is the number of Females’);
END;
BEGIN
Writeln (‘Enter GENDER or STOP to stop the program’) ;
Readln (GENDER);
END;
END;
END.

2. Write an algorithm in PASCAL that takes the age of a student and adds it to the ages of
other students to get a total. The algorithm should terminate at 0. (5 marks)

Possible Solution:
PSEUDOCODE

Start
Set AGE 0
Set TOT  0
Write “Enter Students Age”
Read AGE
WHILE AGE <> 0 DO
TOT  TOT + AGE
Write TOT, “is the total number of ages in the class”
Write “Enter Age or 0 to stop the program”
Read AGE

5
Information Technology in FOCUS for CSEC
SECTION 3 OF SYLLABUS: CHAPTER 36: Implementing the Program – Loop Constructs in Pascal

ENDWHILE
PASCAL

PROGRAM Students_Total_Age (OUTPUT, INPUT);


USES CRT;
VAR
AGE, TOT : integer;

BEGIN
CLRSCR;
TOT: =0 ;
AGE :=0;
Write (‘Please enter Student Age: ');
Readln (AGE);
Writeln;
WHILE AGE <> 0 DO
BEGIN
TOT: =TOT + AGE;
Writeln;
Writeln (TOT, ‘ is the total number of ages in the class’);
Writeln;
Writeln (‘Enter Age or 0 to stop the program’) ;
Readln (AGE);
END;
END.

3. Write an algorithm that will terminate by 999, that accepts the price of an item,
calculates a 20% discount and shows the result. (5 marks)

Possible Solution:

PSEUDOCODE

Start
Set TOT 0
Set PRIC 0
Write “Enter Price and Item”
Read PRICE, ITEM
WHILE PRICE <> 999 DO
Set DISCOUNT PRICE * 20/100

6
Information Technology in FOCUS for CSEC
SECTION 3 OF SYLLABUS: CHAPTER 36: Implementing the Program – Loop Constructs in Pascal

Set TOT TOT + DISCOUNT


Write “The Quantity and Total is”, TOT
Write “Enter price and Item or 999 to stop program”
Read PRICE, ITEM
ENDWHILE
Stop
PASCAL

PROGRAM SHOPPING_DISCOUNT (OUTPUT, INPUT);


USES CRT;
VAR
PRICE, TOT, DISCOUNT : Real;
ITEM : String;
BEGIN
CLRSCR;
TOT: =0 ;
DISCOUNT:=0 ;
PRICE:=0;
Write (‘Please enter Price $: ');
Readln (PRICE);

Write (‘Please enter Item: ');


Readln (ITEM);
Writeln;
WHILE PRICE <> 999 DO
BEGIN
DISCOUNT:= PRICE * (20/100);
TOT: =TOT + DISCOUNT;
Writeln (ITEM , ‘ Discount is ‘, TOT :0:2);
Writeln;
Writeln (‘Enter Price and Item or 999 to stop the program') ;
Readln (PRICE, ITEM);
END;
END.

TOTAL 15 MARKS

7
Information Technology in FOCUS for CSEC
SECTION 3 OF SYLLABUS: CHAPTER 36: Implementing the Program – Loop Constructs in Pascal

Activity # 45 - c
TEST YOUR KNOWLEDGE
Chapter 36: Implementing the Program – Loop Construct in Pascal

INSTRUCTION: Answer all questions.


Solving the following in PSEUDOCODE and PASCAL:

1. Find the lowest number in a series of 41 numbers. (10 marks)

Possible Solution:
PSEUDOCODE

Start
Set MIN 999
FOR Num 1 TO 41 DO
Write “Enter Integer”
Read Num
IF Num < MIN THEN
Set MIN Num
ENDIF
ENDFOR
Write “Lowest number is”, MIN
Stop
PASCAL

PROGRAM minimum_for (OUTPUT, INPUT);


USES CRT;
VAR
Num, MIN: Integer;
BEGIN
CLRSCR;
MIN: =999;
FOR Num: = 1 TO 41 DO
BEGIN
Write (‘Please enter an integer: ');
Readln (Num);
IF Num <MIN THEN
MIN: = Num;
END;
Writeln;
Writeln (‘the lowest number is: ‘, MIN);
Readln;
END.

8
Information Technology in FOCUS for CSEC
SECTION 3 OF SYLLABUS: CHAPTER 36: Implementing the Program – Loop Constructs in Pascal

2. Write an algorithm to find the lowest in a series of integers and then terminate the
program with 0. (10 marks)

Possible Solution:
PSEUDOCODE

Start
Set MIN 999
Write “Enter Integer”
Read Num
WHILE Num <> 0 DO
IF Num < MIN THEN
Set MIN Num
ENDIF
Write “Enter integer or 0 to stop”
Read Num
ENDWHILE
Write “Lowest number is”, MIN
Stop

PASCAL

PROGRAM finding_minimum (OUPUT, INPUT);


USES CRT;
VAR
Num, MIN: integer;
BEGIN
CLRSCR;
MIN: =999 ;
Write (' Please enter a number: ');
Readln (Num);
WHILE Num <> 0 DO
BEGIN
IF Num <MIN THEN
MIN: = Num;
Writeln;
Writeln (' The lowest Number is ‘, MIN);
Writeln;
Writeln (' enter an integer or 0 to stop ');
Readln (Num);
END;
END.
TOTAL 20 MARKS

9
Information Technology in FOCUS for CSEC
SECTION 3 OF SYLLABUS: CHAPTER 36: Implementing the Program – Loop Constructs in Pascal

Activity # 46 - d

TEST YOUR KNOWLEDGE


Chapter 36: Implementing the Program – Loop Construct in Pascal

INSTRUCTION: Answer all questions.


Solve by writing the following in Narrative, Pseudocode and then Pascal:

A variable is given a value of 10. Repeat the following and compute the number until the
number reaches 20 or greater. Ensure to add 2 to the number and print the appropriate result.
(15 marks)

Possible Solution:
PSEUDOCODE

Start
Set NUMBER 10
REPEAT
Set NUMBER NUMBER + 2
Write “The Next Number is: ”, NUMBER
UNTIL Set Counter > 20
Stop

PASCAL

PROGRAM repeat_until_Lesson (OUPUT);


USES CRT;
VAR NUMBER: integer;

BEGIN
CLRSCR;
NUMBER: = 10;
REPEAT
NUMBER: = NUMBER + 2;
Writeln ('The Next Number is:’, NUMBER);
UNTIL NUMBER > 20;
Readln;
END.

10
Information Technology in FOCUS for CSEC
SECTION 3 OF SYLLABUS: CHAPTER 36: Implementing the Program – Loop Constructs in Pascal

Activity # 47 - e

TEST YOUR KNOWLEDGE


Chapter 36: Implementing the Program – Loop Construct in Pascal

INSTRUCTION: Answer all questions.

1. Correctly explain the term Array? ( 2 marks)

Possible Solution:
An array is a collection or group of data that are all of the same data type, having the same
name. An array is used to store more than one value in a variable. A perfect example of an array
is a filing cabinet. The filing cabinet stores a group or a collection of files in one place.

a. List the TWO dimensions of an Array. (2 marks)

Possible Solution:
A. One Dimension
B. Two Dimension

b. Explain each. (2 marks)


Possible Solution:

A one-dimensional array is called a vector. Its elements are arranged sequentially in the
memory of the computer. An element of a vector is specified by the variable name followed by
the index of the element enclosed in parentheses.

A two-dimensional array is really nothing more than an array of arrays (a three-dimensional


array is an array of arrays of arrays). Think of your dinner. You could have a one-dimensional list
of everything you eat:

(lettuce, tomatoes, salad dressing, steak, mashed potatoes, string beans, cake, ice cream,
coffee)

Or

you could have a two-dimensional list of three courses, each containing three things you eat:

(lettuce, tomatoes, salad dressing) and (steak, mashed potatoes, string beans) and (cake, ice
cream, coffee)

2. Explain the purpose of the following terms:

11
Information Technology in FOCUS for CSEC
SECTION 3 OF SYLLABUS: CHAPTER 36: Implementing the Program – Loop Constructs in Pascal

a. Index range (1 mark)

Possible Solution:
[Index range]: This represents the size of the array , which starts at 1- example [1..10].

b. Data type (1 mark)

Possible Solution:
Data Type: Arrays carry the same data type. For example an array may be an integer only, real
only, character only and / or a string only.

c. Subscript (1 mark)

Possible Solution:
An array uses what is called storage locations. These storage locations store variables with the
same data type. Now, each storage location uses the name of the array and a subscript or
index.

3. Write THREE examples of Arrays being declared with the following data types:
a. INTEGER (1 mark)

b. CHAR (1 mark)
c. REAL (1 mark)

Possible Solution:

DATA TYPE ARRAY DECLARATION - Examples


Integer PERCENTAGE: Array [1..10] of Integer;

Real AMT: Array[1..20] of Real;

Character LETTER: Array[1..27] of char;

string NAME: Array[1..15] of string;

TOTAL 12 MARKS

12
Information Technology in FOCUS for CSEC
SECTION 3 OF SYLLABUS: CHAPTER 36: Implementing the Program – Loop Constructs in Pascal

Activity # 48 - f
TEST YOUR KNOWLEDGE
Chapter 36: Implementing the Program – Loop Construct in Pascal

INSTRUCTION: Answer all questions.

1. Write an array of 55 addresses. (3 marks)


Possible Solution:

ADDRESS: Array [1..55] of String;

2. Write an array of 60 whole numbers. (3 marks)


Possible Solution:

WHOLE_NUMBER: Array [1..60] of Integer;

3. Write an array of 14 odd numbers. (3 marks)


Possible Solution:

NUMBER: Array [1..14] of integer;

4. Write an array of consonants in the alphabet. (3 marks)


Possible Solution:

CONSONANT: Array [1..21] of char;

5. Write an array of the vowels in the alphabet. (3 marks)


Possible Solution:

VOWEL: Array [1..5] of char;

6. Write segments of codes for the following data in an arrays:


a. 12,23,54,65,6,78,6 (5 marks)
Possible Solution:

PROGRAM Segment_Code;
USES CRT;
VAR
NUMBER: Array [1..7] of Integer;
BEGIN
CLRSCR;
NUMBER [1]:=12;

13
Information Technology in FOCUS for CSEC
SECTION 3 OF SYLLABUS: CHAPTER 36: Implementing the Program – Loop Constructs in Pascal

NUMBER [2]:=23;
NUMBER [3]:=54;
NUMBER [4]:=65;
NUMBER [5]:=6;
NUMBER [6]:=78;
NUMBER [7]:=6;

………………
END.

b. 12.4,54.6,6.90,23.54 (5 marks)

Possible Solution:

PROGRAM Segment_Code;
USES CRT;
VAR
NUMBER: Array [1..4] of Real;
BEGIN
CLRSCR;
NUMBER [1]:=12.5;
NUMBER [2]:=54.6;
NUMBER [3]:=6.90;
NUMBER [4]:=23.54;

………………
END.

7. What does the statement “Writing to an array” mean? (1 mark)

Possible Solution:
Writing to an array is the method that is used to assign values manually to an array in Pascal.

TOTAL 26 MARKS

14
Information Technology in FOCUS for CSEC
SECTION 3 OF SYLLABUS: CHAPTER 36: Implementing the Program – Loop Constructs in Pascal

Activity # 49 - g

TEST YOUR KNOWLEDGE


Chapter 36: Implementing the Program – Loop Construct in Pascal

INSTRUCTION: Answer all questions.

1. Explain the term traversing array (2 marks)

Possible Solution:
Traversing array : this is the accessing of each element in an array one by one. This is done using
the FOR loop, READ / READLN statement and WRITE/WRITELN statement.

2. Write TWO examples of a statement that is used to access elements in an array.


(2 marks)
Possible Solution:

Statement #1
FOR counter: = 1 TO 8 DO
Readln (Num_Array [counter]);

Statement # 2 (an alternative)


MARK: Array [1..6] of Integer;
BEGIN
CLRSCR;
MARK [1]:=79;
MARK [2]:=68;
MARK [3]:=75;
MARK [4]:=90;
MARK [5]:=56;
MARK [6]:=98;

3. What construct is used in aid of storing data in an array? (2 marks)

Possible Solution:
FOR LOOP construct e.g.
FOR school: = 1 TO 10 DO
Readln (Num_Array [school]);

15
Information Technology in FOCUS for CSEC
SECTION 3 OF SYLLABUS: CHAPTER 36: Implementing the Program – Loop Constructs in Pascal

4. Write a program that stores the prices of 10 items. The program should display the 10
prices. (10 marks)

Possible Solution:

PROGRAM ARRAY_STORAGE (INPUT, OUTPUT);


USES CRT;
VAR
Num_Array: array [1..10] of Integer;
counter: Integer;
BEGIN
CLRSCR;
Writeln ('...Please enter numbers into the array ...’);
Writeln;
BEGIN
FOR counter: = 1 TO 10 DO
Readln (Num_Array [counter]);
END;
END.

TOTAL 16 MARKS

16
Information Technology in FOCUS for CSEC
SECTION 3 OF SYLLABUS: CHAPTER 36: Implementing the Program – Loop Constructs in Pascal

Activity # 50 - h

TEST YOUR KNOWLEDGE


Chapter 36: Implementing the Program – Loop Construct in Pascal

INSTRUCTION: Answer all questions.

1. Write the syntax for displaying elements in an array. (4 marks)

Possible Solution:

Write arrayName [index] OR Writeln arrayName [index]


e.g.
Writeln (‘Element number 1 in the array is: ', MARK [1]);
Writeln (‘Element number 3 in the array is: ‘MARK [3]);

2. Write a program that stores the names of eight (8) vehicle prices. The program should
display the price of the first, fourth, and last vehicle. (8 marks)

Possible Solution:

PROGRAM Vehicle_Prices (OUTPUT);


USES CRT;
VAR
VEH_PRICE: Array [1...8] of Real;
BEGIN
CLRSCR;
VEH_PRICE[1]:=79000.50;
VEH_PRICE[2]:=6804.0.7;
VEH_PRICE[3]:=75890.23;
VEH_PRICE[4]:=90000.25;
VEH_PRICE[5]:=56780.67;
VEH_PRICE[6]:=98124.89;
VEH_PRICE[7]:=569876.59;
VEH_PRICE[8]:=981232.51;
BEGIN
Writeln (‘The first vehicle Price is : $ ' , VEH_PRICE[1] :0:2);
Writeln;
Writeln (‘The Second vehicle Price is : $ ‘ , VEH_PRICE[2] :0:2);

17
Information Technology in FOCUS for CSEC
SECTION 3 OF SYLLABUS: CHAPTER 36: Implementing the Program – Loop Constructs in Pascal

Writeln;
Writeln (‘The Third vehicle Price is : $ ‘ , VEH_PRICE [3] :0:2);
Writeln;
Writeln (‘The Fourth vehicle Price is : $ ' , VEH_PRICE [4] :0:2);
Writeln;
Writeln (‘The Eight vehicle Price is : $ ' , VEH_PRICE [8] :0:2);
END;
Readln;
END.

3. ComputerLearningEZ.com wants the names of all the programmers, database analysts and
webmasters. All this data must be stored in an array called staff. The company’s staff is 45
in total.

Possible Solution:

PROGRAM EMPLOYEE_DATABASE (INPUT, OUTPUT);


USES CRT;
VAR
STAFF: array [1..45] of String;
EMPLOYEE: Integer;
BEGIN
CLRSCR;
Writeln ('...Please enter names of all programming employees…’);
Writeln;
BEGIN
FOR EMPLOYEE: = 1 TO 45 DO
Readln (STAFF [EMPLOYEE]);
END;
END.

18
Information Technology in FOCUS for CSEC
SECTION 3 OF SYLLABUS: CHAPTER 36: Implementing the Program – Loop Constructs in Pascal

4. Write a program that will create an array called staff and allow the users of the program
to enter the names of each employee in that array. (4 marks)

Possible Solution in pseudocode:

Start
Set EMPLOYEE 0
Set STAFF: array [1..X] of sting
Write “Enter size of the array”
Read X
Write “Enter name of all employee”
FOR EMPLOYEE = 1 TO DO
Read STAFF [EMPLOYEE]
Endfor
NB: for practise attempt this question in PASCAL.
TOTAL 20 MARKS

Activity # 51 - i

TEST YOUR KNOWLEDGE


Chapter 36: Implementing the Program – Loop Construct in Pascal

INSTRUCTION: Answer all the questions.

1. Develop a program that will search for the number 33 in an array of 100 numbers. The
program should print the index number and what value is found. (8 marks)

Possible Solution:

PROGRAM Number_search (INPUT, OUTPUT);


USES CRT;
VAR
counter: Integer;
searchNUMBER: integer;
found: String;
NUMBER: array [1..100] of integer;
BEGIN
Writeln (' Please Enter Numbers ');
FOR counter:=1 TO 100 DO
BEGIN
Readln (NUMBER [counter]);
END;
counter:=0;

19
Information Technology in FOCUS for CSEC
SECTION 3 OF SYLLABUS: CHAPTER 36: Implementing the Program – Loop Constructs in Pascal

found:='NO';
Writeln;
Writeln ('Now enter the number you want to searching for');
Readln (searchNUMBER);
REPEAT
counter:=counter+1;
IF NUMBER [counter] = searchNUMBER THEN
found:='YES';
UNTIL ((counter = 100) OR (found = 'YES'));
IF found = 'YES' THEN
Writeln (' Employee position found at: ' , counter)
ELSE
Writeln (' Employee position not found');
Readln;
END.

2. Write a program for a company that will search for five employees’ social security
numbers (1334, 4354, 5465, 123, and 5676) in an array of 5 numbers. The program
should print the subscript number and the value that is found. (8 marks)

Possible Solution:

PROGRAM Social_Security (INPUT, OUTPUT);


USES CRT;
VAR
COUNTER: Integer;
search_SS_NUMBER: Integer;
FOUND: String;
SS_NUMBER: array [1..5] of Integer;
BEGIN
CLRSCR;

SS_NUMBER [1]:= 1334;


SS_NUMBER [2]:= 4354;
SS_NUMBER [3]:=5465;
SS_NUMBER [4]:= 123;
SS_NUMBER [5]:= 5676;

BEGIN
FOR COUNTER:=1 TO 5 DO
END;

COUNTER:=0;

20
Information Technology in FOCUS for CSEC
SECTION 3 OF SYLLABUS: CHAPTER 36: Implementing the Program – Loop Constructs in Pascal

FOUND:='NO';
Writeln;
Writeln ('Now enter Social Security Number you want to searching for');
Readln (search_SS_NUMBER);

REPEAT
COUNTER:= COUNTER+1;
IF SS_NUMBER [COUNTER] = search_SS_NUMBER THEN
FOUND := 'YES';
UNTIL ((COUNTER = 5) OR (FOUND = 'YES'));
IF FOUND = 'YES' THEN
Writeln (' Social Security Number position found at: ' , COUNTER)

ELSE

Writeln (' Social Security Number position not found');


Readln;
END.

TOTAL 16 MARKS

21