Software Development - Problem Solving
Software Development - Problem Solving
TERM 3 Week 4
TOPIC Software Development – Problem Solving
AIMS OF LESSON Develop problem solving skills
RESOURCES Paper based resources Digital resources DBE Textbooks
Gr 12 – Chapter 3 Links on the WCED ePortal: DataFiles
The e-resources below has the actual URL to the websites
INTRODUCTION Reinforce problem solving principals
CONCEPTS AND Two-dimensional array Decision making
SKILLS Loops / nested loops Random numbers
Sting manipulation
ACTIVITIES / Open the incomplete project RainfallApp_p
ASSESSMENT This incomplete project has no functionality.
Scenario: Rainfall
You have been contacted to help with the capture and calculations of rainfall for SA Weather.
You have been supplied with an incomplete program, RainfallApp_p, with the following arrays.
arrRainfall: A two-dimensional array that contains the data for 2016 – 2019 rainfall per month.
arrRainfall: array [1 .. 4, 1 .. 12] of Double = ((3.80, 42.86, 39.70, 5.49,
19.52, 8.60, 25.24, 3.40, 14.59, 74.53, 47.30, 34.85),
(24.38, 0.49, 21.87, 1.79, 3.22, 7.37, 13.73, 1.56, 4.15, 31.74, 22.39,
10.25), (5.55, 21.43, 57.54, 14.71, 9.30, 14.22, 2.02, 3.21, 0.00, 29.47,
1.12, 8.63), (18.63, 15.94, 71.63, 6.83, 21.15, 17.26, 38.06, 2.75, 31.54,
34.21, 10.73, 12.33));
arrMonths: An array that contains the months of the year.
arrMonths: array [1 .. 12] of string = (
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct',
'Nov', 'Dec');
arrYears: An array that contains the years.
arrYears: array [1 .. 4] of Integer = (2019, 2018, 2017, 2016);
1 Button [Display]
Add code for this button to do the following:
• Display the rainfall in neat columns
• Use the arrMonths and arrYears for column and row headings
INFORMATION TECHNOLOGY: GRADE 12
Example output
Example output
3 Button [Graph]
A bar graph is a visual way to see how much it rained in a specific month.
Add code to this button to do the following:
• Use speMonth to determine the month
• Add a X on the redOutput for every 5mm of rain next to the year.
Example Output before the new year is added
CONSOLIDATION In this problem, you worked with a two-dimensional array and used problem solving
techniques.
VALUES Thank you for working through this document! Stick to your hard work. You are
creating an opportunity to better yourself. Your perseverance will bring dividends.
E-RESOURCES Data Files: https://wcedeportal.co.za/eresource/135351
Term 3 Week 3 – Solutions – PlantIO_P
1 btnDisplay
Add code for this button to do the following:
• Display the array with column and row heading on redOutput component.
• The output should be formatted with a line between every second row.
procedure TForm1.btnDisplayClick(Sender: TObject);
var
I: Integer;
J: Integer;
sLine: String;
begin
redOutput.Clear;
sLine := #9;
for I := 1 to 20 do
sLine := sLine + intToStr(I) + #9;
redOutput.Lines.Add(sLine);
for I := 1 to 6 do
begin
sLine := intToStr(I) + #9;
for J := 1 to 20 do
begin
sLine := sLine + arrPlant[I, J] + #9;
end;
redOutput.Lines.Add(sLine);
if I mod 2 = 0 then
begin
redOutput.Lines.Add('');
INFORMATION TECHNOLOGY: GRADE 12
end;
end;
end;
2 btnAdd
Add code for this button to do the following:
• Get the selected plant from the radio group
• Get the position the plant should be planted from the input box. You can expect the input in the
following format: <Row><Space><Column>
Example: 5 17
• You should check if the space is available for a plant. Display a message to indicate the space is
available and add the selected plant to the array at that index. Display an error message when
the space in not available.
procedure TForm1.btnAddClick(Sender: TObject);
var
sInput: String;
iIndex1, iIndex2: Integer;
begin
sInput := edtPos.Text;
iIndex1 := StrToInt(sInput[1]);
iIndex2 := StrToInt(Copy(sInput, 3));
if (arrPlant[iIndex1, iIndex2] = ' ') then
begin
arrPlant[iIndex1, iIndex2] := rdgAddCrop.Items[rdgAddCrop.ItemIndex][1];
ShowMessage(rdgAddCrop.Items[rdgAddCrop.ItemIndex] + ' was added to '
+ sInput);
end
else
begin
ShowMessage('Sorry, no space');
end;
redOutput.Clear;
btnDisplay.Click;
end;
3 btnAmount
Add code for this button to do the following
• Get the selected plant from the combo box
• Run through the two-dimensional array and count how many of the selected plants are in the
garden.
• Display the amount on the redOutput component. The output should be in the following format
<plant>: <amount>
Example if carrots were selected: Carrots: 21
function TForm1.countPlant(sPlant: String): Integer;
var
I: Integer;
J: Integer;
iTotal: Integer;
begin
iTotal := 0;
if sPlant = 'E' then
sPlant := ' ';
INFORMATION TECHNOLOGY: GRADE 12
for I := 1 to 6 do
begin
for J := 1 to 20 do
begin
if arrPlant[I, J] = sPlant then
begin
Inc(iTotal);
end;
end;
end;
Result := iTotal;
end;
begin
redOutput.Clear;
redOutput.Lines.Add('Average rainfall per term');
redOutput.Lines.Add('');
redOutput.Lines.Add(format('%15s%12s%12s%12s', ['Term 1', 'Term 2', 'Term
3',
'Term4']));
for I := 1 to 4 do
begin
dTerm1 := 0;
dTerm2 := 0;
dTerm3 := 0;
dTerm4 := 0;
for J := 1 to 12 do
Begin
case J of
1 .. 3:
dTerm1 := dTerm1 + arrRainfall[I, J];
4 .. 6:
dTerm2 := dTerm2 + arrRainfall[I, J];
7 .. 9:
dTerm3 := dTerm3 + arrRainfall[I, J];
10 .. 12:
dTerm4 := dTerm4 + arrRainfall[I, J];
end;
End;
redOutput.Lines.Add(format('%d:%10.2fmm%10.2fmm%10.2fmm%10.2fmm',
[arrYears[I], dTerm1, dTerm2, dTerm3, dTerm4]));
end;
end;
3 Button [Graph]
A bar graph is a visual way to see how much it rained in a specific month.
Add code to this button to do the following:
• Use speMonth to determine the month
• Add a X on the redOutput for every 5mm of rain next to the year.
begin
redOutput.Clear;
iMonth := speMonth.Value;
redOutput.Lines.Add('Rainfall graph for: '+ arrMonths[iMonth]);
redOutput.Lines.Add('');
for I := 1 to 4 do
begin
dTemp := arrRainfall[I, iMonth];
sLine := intToStr(arrYears[I]) + #9;
begin
for I := 4 downto 2 do
begin
for J := 1 to 12 do
begin
arrRainfall[I, J] := arrRainfall[I - 1, J];
end;
end;
for I := 1 to 4 do
arrYears[I] := arrYears[I] + 1;
sLine := edtRainfall.Text;
iCounter := 1;
while iCounter <= 12 do
begin
if iCounter < 12 then
begin
iPos := Pos(',', sLine);
end;
INFORMATION TECHNOLOGY: GRADE 12
dRainfall := strToFloat(copy(sLine, 1, iPos - 1));
Delete(sLine, 1, iPos);
arrRainfall[1, iCounter] := dRainfall;
Inc(iCounter);
end;
ShowMessage('New rainfall added');
end;