Implementation (algorithm specification)Examples of count occurrences

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 ScienceSoftware design and development

Examples of count occurrences

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

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

A list box is used to output the number of occurrences found. It is possible to use a different object for output.

occurrence = 0
desired_value = inputbox(“Please enter the age that you would like to keep count of”, “Enter Age”)
For counter = 0 To 9
If age(counter) = desired_value Then
occurrence = occurrence + 1
End If
Next
ListBox1.Items.Add ("There were " & occurrence & “occurrences of ” & desired_value)

LiveCode

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

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

put 0 into occurrence
ask "Please enter the item that you would like to search for"
repeat with loop = 1 to 10
if desireditem = age[loop] then
put occurrence + 1 into occurrence
End If
end repeat
put “There were" & & occurrence & & ”occurrences of” & & desired_item into field "output"

Python – Version 3.x

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

occurrence = 0
desired_item = int(input(“Please enter the item that you would like to count”))
for counter in range (10):
If age[counter]= desired_item:
occurrence = occurrence + 1
print ("There were ”, occurrence, “ occurrences of ”, desired_item

Java

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

int occurrence=0;
input=JOptionPane.showInputDialog("Please enter the item that you would like to count");
desired_item=Integer.parseInt(input);
for(int counter=0;counter˂10;counter++)
{
if(age[counter]=desired_item)
{
occurrence++;
}
}
System.out.println("There were " + occurrence + “occurrences of” + desired_item);

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 occurrence = 0
INPUT PROMPT “Please enter the item that you would like to count”: desired_item
FOR counter = 1 To 10
IF age (counter) = desired_item THEN
LET occurrence = occurrence + 1
END IF
NEXT counter
PRINT "There were ”; occurrence; “ occurrences of “; desired_item

Xojo (Formerly REALbasic)

This example presumes that you have access to a pre-designed method for creating an InputBox.

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

A static text box called ‘showtotal’ has been used to output the minimum value.

occurrence = 0
desired_item = val(InputBox("Please enter the item that you would like to count"))
For counter = 0 to 9
If age(counter)=desired_item Then
occurrence = occurrence + 1
End If
Next
showtotal.text = “There were ” + occurrence + “ occurrences of ” + desired_item

Pascal

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

begin
occurrence:=0;
writeln (‘Please enter the item that you would like to search for’)
readln(desired_item);
For counter := 0 to 9 do
if (age[counter] = desired_item) then
occurrence:=occurrence + 1;
end;
writeln(output, 'There were ’, occurrence, ‘ occurrences of ’, desired_item);
end.

Comal

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

occurrence% := 0
PRINT "Please enter the item that you would like to count";
INPUT desired_item%
FOR counter%:= 0 TO 9 DO
IF age% (counter%) = desired_item% THEN
occurrence% := occurrence% + 1
ENDIF
NEXT
PRINT “There were”, occurrence%, “ occurrences of ”, desired_item%