Do the following:
Open the incomplete program in the Question 4 folder - NSC Information Technology - Question 4 - 2020 - Paper 1
Question 4
Do the following:
Open the incomplete program in the Question 4 folder.
Enter your examination number as a comment in the first line of the Question_4_U.pas file.
C... show full transcript
Worked Solution & Example Answer:Do the following:
Open the incomplete program in the Question 4 folder - NSC Information Technology - Question 4 - 2020 - Paper 1
Step 1
Button [4.1.1 - Create harbour containers]
96%
114 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
To implement this part of the code, create a loop that runs 50 times to generate random real numbers:
Set up your loop to iterate 50 times.
Within the loop, generate a random number between 1 and 99 using a random number generator function.
Round this number to one decimal place.
Assign each generated value to the array arrContainers for each index from 1 to 50.
Here is a sample code snippet:
for i := 1 to 50 do
begin
arrContainers[i] := Round(Random(99) + 1 * 10) / 10;
end;
Step 2
Button [4.1.2 - Display harbour containers]
99%
104 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
For the second part, create a nested loop to display the contents of arrContainers:
Set an index for the rows of display and set it initially to zero.
Loop through rows to display data in 5 rows.
For each row, loop through 10 columns and append the values from arrContainers based on the appropriate index.
Format your output to display in the redQ4_1_2 component and ensure proper spacing.
The code will look something like this:
for row := 0 to 4 do
begin
line := '';
for col := 0 to 9 do
begin
line := line + FloatToStr(arrContainers[row * 10 + col]) + ' ';
end;
redQ4_1_2.Lines.Add(line);
end;
Step 3
Button [4.2 - Containers loaded to be shipped]
96%
101 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
In this part, you will need to manage the loading process of containers onto the ship:
Initialize a variable TotalTons to zero.
Loop through arrContainers checking the weight of each container.
If adding the weight of a container keeps the total under or equal to 200 tons, add that weight to TotalTons and store the detail of this container.
If the next container's weight would exceed 200 tons, skip it and proceed to the next.
Display the total weight in the panel pnlQ4 and write it to Tons.txt.
Here’s how you could write the code:
TotalTons := 0;
for index := 1 to 50 do
begin
if (TotalTons + arrContainers[index]) <= 200 then
begin
TotalTons := TotalTons + arrContainers[index];
redQ4_2.Lines.Add(FloatToStr(arrContainers[index]));
end;
end;
// Write to file
AssignFile(TonsFile, 'Tons.txt');
Rewrite(TonsFile);
WriteLn(TonsFile, TotalTons);
CloseFile(TonsFile);