Implementation (algorithm specification)Examples for finding maximum

Algorithms are created to allow users to tell a computer how to solve a problem. Understanding how to construct five of the most common algorithms is a critical skill for budding software developers.

Part ofComputing ScienceRevise: Software design and development

Examples for finding maximum

Visual Studio 2010 (Similar to VB5, VB6 and Subsequent Visual Basic.NET languages)

In this example it is presumed that the array/list has already been populated.

A list box is used to output the maximum value. It is possible to use a different object for output.

maximum = testscore[0]
For counter = 1 To 9
If testscore[counter] > maximum Then
maximum = testscore[counter]
End If
Next
ListBox1.Items.Add("The maximum value was " & maximum)

LiveCode

Remember - Live code will index from 1 rather than 0!

In this example it is presumed that the array/list has already been populated.

put testscore[1] into maximum
repeat with loop = 2 to 10
if testscore[loop] > maximum then
put testscore [loop] into maximum
End if
end repeat
put “The maximum value was" & &maximum & & into field "output"

Python – Version 3.x

In this example it is presumed that the array/list has already been populated.

maximum = testscore[0]
for counter in range (1, 10):
if testscore[counter] > maximum:
maximum = testscore[counter]
print ("The maximum value was ", maximum)

Java

In this example it is presumed that the array has already been populated.

maximum=testscore[0];
for(int counter=1;counter˂10;i++)
{
if(testscore[counter]>maximum)
{
maximum=testscore[counter];
}
}
System.out.println("The maximum value was " + maximum);

True BASIC

Remember – True BASIC will index from 1 rather than 0!

In this example it is presumed that the array has already been populated.

LET maximum = testscore(1)
FOR counter = 2 to 10
IF testscore(counter) > maximum THEN
maximum = testscore (counter)
END IF
NEXT counter
PRINT "The maximum value was "; maximum

Xojo (Formerly REALbasic)

In this example it is presumed that the array has already been populated.

A static text box called ‘maxtext’ has been used to output the maximum value.

maximum = score(0)
For counter = 1 to 9
If score(counter)>maximum Then
maximum = score(counter)
End If
Next
maxtext.text = “The maximum value was ” + maximum

Pascal

In this example it is presumed that the array has already been populated.

begin
maximum:=score[0];
For counter := 1 to 9 do
begin
if score[counter] > maximum then
maximum:=score[counter];
end;
writeln(output, 'The maximum value was ', maximum);
end.

Comal

In this example it is presumed that the array has already been populated.

maximum% := score% (0)
FOR counter%:= 1 TO 9 DO
IF score% (counter%) > maximum% THEN
maximum% := score% (counter%)
ENDIF
NEXT
PRINT “The maximum value was ”, maximum%