This is the first standard algorithm (in no particular order) that you have to know, and be able to write a pseudo code version of in the exam.
Put simply this algorithm counts the number of times an item appears in a list. The item could be a name or a number, the list is normally an array.
So for example in the following list:
“A” “B” “B” “C” “B” “A” “A” “B” “C” “B”
The algorithm would ask the user what they are looking for and then should return the results below:
Being looked for Number of occurrences
A 3
B 5
C 2
So you need to be able to write the pseudo code that will do this. My answer is below, see the powerpoints at the bottom for more detail:
1.1 ask for item being searched for
1.2 for each item in the array
1.3 if this item matches the item being searched for
1.4 add one to the count
1.5 end if
1.6 end loop
1.7 print out the number of times the item was found
December 3, 2007 at 12:12 am |
do you have to count multiple things – or always just one?
if multiple, I’d probably think to use an arrayList (key, value pair) of the desired items.
then loop through the list –
check to see if the current item is in the arrayList (key) –
if so, increment the value
move on
but that could very well be an inefficient method.
what do you think?
December 3, 2007 at 12:37 pm |
who is hobby lobby? how did u find the blog =S?
December 3, 2007 at 11:51 pm |
Interesting! I think counting multiple items is beyond the scope of this course, counting one item in a list is work enough!
Thanks for input though.
December 4, 2007 at 4:31 am |
>who is hobby lobby? how did u find the blog =S?
hobbylobby is me.
I found this blog because it’s on the web. it shows up from time to time in my tag surfer panel in wordpress.
is it a private blog?
December 15, 2007 at 12:45 pm |
very interesting, but I don’t agree with you
Idetrorce
September 30, 2008 at 8:25 am |
Well done mr whiteside – better than our teachers version
Ross form liberton