0% found this document useful (0 votes)
60 views

Jan Jacobus Spies - Form 4 Unit 2 Arrays Learner Workbook

The document discusses arrays including declaration, populating with values, basic algorithms, and exercises. Arrays allow storing multiple values of the same type. Loops are recommended for entering and displaying array values. Text files can be used to populate arrays by reading values line by line with a counter to track the array index.

Uploaded by

jeanspies18
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)
60 views

Jan Jacobus Spies - Form 4 Unit 2 Arrays Learner Workbook

The document discusses arrays including declaration, populating with values, basic algorithms, and exercises. Arrays allow storing multiple values of the same type. Loops are recommended for entering and displaying array values. Text files can be used to populate arrays by reading values line by line with a counter to track the array index.

Uploaded by

jeanspies18
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/ 11

1|Page

Contents
1.Array Declaration ............................................................................................................................. 3
2. Entering Values & Display Arrays ..................................................................................................... 5
3. Populating Arrays with TextFiles ...................................................................................................... 7
4. Basic Array Algorithms ................................................................................................................. 8
5. Consolidation Exercises ................................................................................................................... 9

2|Page
1.Array Declaration

1.1 Look at the following Array declaration and explain each section:
Range of the array

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

Array name for the array The type of array.

1.2 Below is a diagram showing values allocated to the array:

1 2 3 4 5 6 7 8 9 10
86 5 -7 34 13 99 243 67 45 3

Answer the following questions based on the above array:

a. Write down the statement to display the number 99 in memOutput.

memOutput .lines.add(IntToStr(arrNums[6]));

b. Write down the statement to assign the number 243 to the variable iNumber.

c. Write down the statement to display the 2nd number in the list (array) in memOutput.

d. Write down the statement to calculate the product of the 5th and 10th numbers in the list
(array) and assign to rAnswer.

e. Write down the statements to swap the 1st and 6th values of the array.

f. Assign the value of 100 to the 7th position of the array.

g. Looking at the values assigned above, write down the index of the biggest number in the list.

3|Page
1.3 Answer the following questions. (Should the answer be FALSE, correct the statement, or explain
the reason why)

a. An array can be of more than one data type. (TRUE / FALSE)

b. The following array declaration is valid: (TRUE / FALSE)


arrClassRooms : array[‘A’..’E’] of char;

c. Should the above declaration be valid, how would you address the 2nd char in the array?

d. The following array declaration is valid: (TRUE / FALSE)


arrClassRooms : array[1.1 .. 1.9] of char;

e. Should the above declaration be valid, how would you address the 2nd char in the array?

f. To access TWO values in a given array, one can use the following statement: (TRUE/FALSE)
arrNums[2,7]

g. The values stored in an arrays, is permanently stored. (TRUE / FALSE)

h. Declare arrays for the following data:

 Store five names

 Store 15 real numbers

 Store 200 characters

i. Explain the difference between an array element and an array index.

4|Page
2. Entering Values & Display Arrays

2.1 Why is it recommended that a loop is used when displaying (and entering) the values in an array?

2.2 Consider the following array and variable declarations:


VAR iIndex : integer;
arrMoney : array[1..15] of Real;
rSum : real;

Look at the code segments below and for each of them,


 explain what the output / purpose of the code is, and
 If you predict that the code is incorrect and will result in a syntax, logical or runtime,
state the reason and
 TRY TO CORRECT the mistake in the space provided, if needed.

a. For iIndex := 1 to 15 do
arrMoney := inputbox(‘Money’,’Enter amount’,’’);

b. For iIndex := 1 to 15 do
arrMoney[iIndex] := inputbox(‘Money’,’Enter amount’,’’);

c. For iIndex := 1 to 15 do
memOut.lines.add(FloatToSTr(iIndex));

d. For iIndex := 1 to 15 do
memOut.lines.add(FloatToStr(arrMoney[iIndex]));

e. rSum := 0;
For iIndex := 1 to 25 do
rSum := rSum + arrMoney[iIndex];

f. rSum := 0;
For iIndex := 1 to 5 do
rSum := rSum + arrMoney[iIndex];

5|Page
2.3 The following constant array has been declared:
arrMoney : array[1..5] of real = (34.10, 50, 55.45, 98.60, 345.22);

Explain the difference between the following 2 lines of code (assume iIndex’s value is 2):

arrMoney[iIndex * 2] := rNum;
arrMoney[iIndex] := arrMoney[iIndex] * 2;

6|Page
3. Populating Arrays with TextFiles
3.1 Why is a counter necessary, most of the times, when reading a textfile into an array?

3.2 Consider the following declarations:


VAR arrNames : array[1..100] of string;
iCounter : integer;
inFile : Textfile;

The content of the textfile is:

Peter
Sarah
Joseph
Ntsako

a. Add the necessary code to the given code segment below to read all the names in the textfile to
the array.
AssignFile(inFile, ‘names.txt’);

Reset(inFile);

While eof(inFile) = false do

Begin

Readln(inFile, sName);

End;

CloseFile(inFile);

b. What will iCounter’s value be once the end of the file is reached?

c. What will happen if there are more than 100 names in the textfile?

d. Write down the solution, or add the necessary code to the code segment above to solve the
problem.

e. Why are we not using a FOR loop when reading the textfile into arrays (since we mostly always
use FOR loops when populating arrays)?

7|Page
4. Basic Array Algorithms
Use the following declarations for the questions that follow (you may assume that the array has 20
elements):
VAR arrMoney : array[1..20] of Real;
iIndex : integer;
rSum, rFind, rAve, rHigh, rLow : real;

4.1 Linear Searching - Write down the pseudocode to find and display all values greater than 100.

4.2 Find a specific value (linear search). Determine if the value 200 is stored in the array.

4.3 Calculate the sum and average of all the values in the array.

4.4 Determine the highest and lowest values in the array.

8|Page
5. Consolidation Exercises
(Remember to provide a reason if False)

5.1 What could be the possible reason for the following error message when working with Arrays:

5.2 The following is a valid Array declaration (TRUE / FALSE):


arrWords : array[0..4] of string;

5.3 An array index cannot be negative (TRUE / FALSE).

5.4 One array (arrNames) can be assigned to another array (arrFirstNames) by using an assignment
statement (assuming the arrays have the same data type and size): (True / False)
arrFirstNames := arrNames;

5.5 Arrays cannot be displayed in reverse order. (True / False)

5.6 What does the term linear search mean?

5.7 Use a While loop to display the content of any array (instead of a For loop):

9|Page
5.8 Correct the following code by explaining what the problem is / how to fix the error. Use the given
variables below:
VAR arrWords, arrSent: array[1..50] of string
arrChars: [1..50] of integer;
iCount : integer; refers to the number of elements used in the array
inFile : Textfile;
sWord, sSent, sLine, sSearch : string;
iPlace : integer;
bFound : Boolean;

Textfile layout:
Word*Sentence*NumberOfChars
Cat*The cat jumps over the moon*27

a. Display array (already populated)


RedOut.clear;
RedOut.lines.add(‘List of Words’);
For iindex := 1 to iCount do
Begin
RedOut.lines.add(arrWords[iindex]);
Inc(iCount);
End;

b. Display array (already populated)


RedOut.clear;
RedOut.lines.add(‘List of Words’);
For iindex := 1 to 50 do
Begin
RedOut.lines.add(arrnames[iindex]);
End;

c. Populating array with textfile

If fileexists(’data.txt’) = false
Then showmessage(‘ no file’)
Else
begin
Assignfile(infile,’data.txt’);
Reset(infile);
End;

iCount :=0;
While (eof(infile) = false) and
(iCount > 50) do
Begin
Readln(sline);
Inc(iCount);
ArrWords[iCount] :=
copy(sline,1,pos(‘*’,sline)-1);
delete(sline,1,pos(‘*’,sline)-1);
delete(sline,1,pos(‘*’,sline)-1);
// to delete the sent
arrChars[iCount] := strtoint(sline);
End;

10 | P a g e
d. Populating arrays with textfile
Assignfile(infile,’data.txt’);
Rewrite(infile);
While eof(infile) = false do
Begin
Writeln(infile,sline);
Iplace := pos(‘*’,sline);
sWord := copy(sline,1,iplace-1);
Delete(sline,1,iplace);
sSEnt := copy(sline,1,iplace-1);
Delete(sline,1,iplace);
Red.lines.add(sWord+#9+sSent+
#9+sline);
End;

e. Populating arrays with textfiles:

Assignfile(infile,’data.txt’);
iCount :=1;
While eof(infile) = false do and
Begin
readln(infile,sline);
iplace := pos(‘*’,sline);
sWord := copy(sline,1,iplace-1);
Delete(sline,1,iplace);
sSent := copy(sline,1,iplace-1);
Delete(sline,1,iplace);
Inc(iCount);
For iindex := 1 to ilong do
Arrnames[iindex] := sname;
End;// while

f. Search and add to an array:

For iIndex := 1 to icount do


begin
If uppercase(ssearch) =
uppercase(arrWord[iloop])
Then bfound := true
Else bfound := false;
End; //for

If bfound = false
Then begin
arrWords[icount+1] := ssearch;
inc(icount);
end;

11 | P a g e

You might also like