Photo AI

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 icon

Question 4

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

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

Answer

To create the harbour containers, we will use a loop:

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

Answer

To display the weights of the containers:

  1. Set an index variable to 0.
  2. Use nested loops to iterate through rows and columns (5 rows and 10 columns).
  3. Construct a string to hold all weights for display using a loop to access the arrContainers array with the index.
  4. 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

Answer

For loading the containers onto the ship:

  1. Initialize TotalTons as 0 and create a string variable for display.
  2. 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.
  3. Display the total weight loaded onto the ship.
  4. 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);

Join the NSC students using SimpleStudy...

97% of Students

Report Improved Results

98% of Students

Recommend to friends

100,000+

Students Supported

1 Million+

Questions answered

;