QUESTION 4: PROBLEM-SOLVING PROGRAMMING
SCENARIO
Tons of cargo are loaded into containers and stored in the harbour for shipment - NSC Information Technology - Question 4 - 2020 - Paper 1
Question 4
QUESTION 4: PROBLEM-SOLVING PROGRAMMING
SCENARIO
Tons of cargo are loaded into containers and stored in the harbour for shipment.
Do the following:
Open the inco... show full transcript
Worked Solution & Example Answer:QUESTION 4: PROBLEM-SOLVING PROGRAMMING
SCENARIO
Tons of cargo are loaded into containers and stored in the harbour for shipment - 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 create the harbour containers, we will use a loop:
Loop 50 times to generate random real numbers:
Randomly generate values between 1 and 99 (inclusive) and round them to one decimal place.
Store these values in the array arrContainers.
Example code snippet:
for i := 0 to 49 do
begin
arrContainers[i] := Round(Random * 98 + 1, 1);
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
To display the weights of the containers:
Set an index variable to 0.
Use nested loops to iterate through rows and columns (5 rows and 10 columns).
Construct a string to hold all weights for display using a loop to access the arrContainers array with the index.
Display the constructed string in the rich edit component redQ4_1_2.
Example code snippet:
sLine := '';
for row := 0 to 4 do
begin
for col := 0 to 9 do
begin
sLine := sLine + FloatToStr(arrContainers[index]) + ' ';
Inc(index);
end;
end;
// Display sLine in the rich edit
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
For loading the containers onto the ship:
Initialize TotalTons as 0 and create a string variable for display.
Loop through the arrContainers array:
Check if adding the current container's weight exceeds 200 tons.
If not, add the weight to TotalTons and record the container's weight.
Increment the index to continue looping.
Display the total weight loaded onto the ship.
Write the weights loaded to a text file Tons.txt.
Example code snippet:
TotalTons := 0;
for index := 0 to 49 do
begin
if (TotalTons + arrContainers[index]) <= 200 then
begin
TotalTons := TotalTons + arrContainers[index];
// Add weight to display string
end;
end;
// Write to file
Rewrite(TonsFile);
WriteLn(TonsFile, TotalTons);
Close(TonsFile);