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 = 0desired_value = inputbox(“Please enter the age that you would like to keep count of”, “Enter Age”)For counter = 0 To 9If age(counter) = desired_value Thenoccurrence = occurrence + 1End IfNextListBox1.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 occurrenceask "Please enter the item that you would like to search for"repeat with loop = 1 to 10if desireditem = age[loop] thenput occurrence + 1 into occurrenceEnd Ifend repeatput “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 = 0desired_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 + 1print ("There were ”, occurrence, “ occurrences of ”, desired_itemJava
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 = 0INPUT PROMPT “Please enter the item that you would like to count”: desired_itemFOR counter = 1 To 10IF age (counter) = desired_item THENLET occurrence = occurrence + 1END IFNEXT counterPRINT "There were ”; occurrence; “ occurrences of “; desired_itemXojo (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 = 0desired_item = val(InputBox("Please enter the item that you would like to count"))For counter = 0 to 9If age(counter)=desired_item Thenoccurrence = occurrence + 1End IfNextshowtotal.text = “There were ” + occurrence + “ occurrences of ” + desired_itemPascal
In this example it is presumed that the array has already been populated.
beginoccurrence:=0;writeln (‘Please enter the item that you would like to search for’)readln(desired_item);For counter := 0 to 9 doif (age[counter] = desired_item) thenoccurrence:=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% := 0PRINT "Please enter the item that you would like to count";INPUT desired_item%FOR counter%:= 0 TO 9 DOIF age% (counter%) = desired_item% THENoccurrence% := occurrence% + 1ENDIFNEXTPRINT “There were”, occurrence%, “ occurrences of ”, desired_item%