Jan Jacobus Spies - Form 4 Unit 2 Arrays Learner Workbook
Jan Jacobus Spies - Form 4 Unit 2 Arrays Learner Workbook
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
1 2 3 4 5 6 7 8 9 10
86 5 -7 34 13 99 243 67 45 3
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.
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)
c. Should the above declaration be valid, how would you address the 2nd char in the array?
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]
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?
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?
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);
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.
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.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.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
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;
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
If bfound = false
Then begin
arrWords[icount+1] := ssearch;
inc(icount);
end;
11 | P a g e