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

Chapter 36: Implementing the Program – Loop Construct in Pascal

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”
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’);
TOT:=TOT +1;
TOT:= TOT + RAIN_FALL;
Writeln (‘The total rain fall for the month is: ', TOT);
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”
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’);
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;
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

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”
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”
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: ');
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’) ;
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”
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”

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: ');
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’) ;
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”
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”
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 \$: ');

Write (‘Please enter 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') ;
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
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”
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: ');
IF Num <MIN THEN
MIN: = Num;
END;
Writeln;
Writeln (‘the lowest number is: ‘, MIN);
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”
WHILE Num <> 0 DO
IF Num < MIN THEN
Set MIN Num
ENDIF
Write “Enter integer or 0 to stop”
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: ');
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 ');
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

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;
END.

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

Activity # 47 - e

Chapter 36: Implementing the Program – Loop Construct in Pascal

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;

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
Chapter 36: Implementing the Program – Loop Construct in Pascal

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

Possible Solution:

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

Possible Solution:

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

Possible Solution:

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

Possible Solution:

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

Possible Solution:

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 :=12;

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

NUMBER :=23;
NUMBER :=54;
NUMBER :=65;
NUMBER :=6;
NUMBER :=78;
NUMBER :=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 :=12.5;
NUMBER :=54.6;
NUMBER :=6.90;
NUMBER :=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

Chapter 36: Implementing the Program – Loop Construct in Pascal

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

Statement # 2 (an alternative)

MARK: Array [1..6] of Integer;
BEGIN
CLRSCR;
MARK :=79;
MARK :=68;
MARK :=75;
MARK :=90;
MARK :=56;
MARK :=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

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
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

Chapter 36: Implementing the Program – Loop Construct in Pascal

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 );
Writeln (‘Element number 3 in the array is: ‘MARK );

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:=79000.50;
VEH_PRICE:=6804.0.7;
VEH_PRICE:=75890.23;
VEH_PRICE:=90000.25;
VEH_PRICE:=56780.67;
VEH_PRICE:=98124.89;
VEH_PRICE:=569876.59;
VEH_PRICE:=981232.51;
BEGIN
Writeln (‘The first vehicle Price is : \$ ' , VEH_PRICE :0:2);
Writeln;
Writeln (‘The Second vehicle Price is : \$ ‘ , VEH_PRICE :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  :0:2);
Writeln;
Writeln (‘The Fourth vehicle Price is : \$ ' , VEH_PRICE  :0:2);
Writeln;
Writeln (‘The Eight vehicle Price is : \$ ' , VEH_PRICE  :0:2);
END;
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
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”
Write “Enter name of all employee”
FOR EMPLOYEE = 1 TO DO
Endfor
NB: for practise attempt this question in PASCAL.
TOTAL 20 MARKS

Activity # 51 - i

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
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');
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
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 := 1334;

SS_NUMBER := 4354;
SS_NUMBER :=5465;
SS_NUMBER := 123;
SS_NUMBER := 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');

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