Standard Algorithm 1 – Counting Occurrences

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

 

counting-occurrences.jpg


Advertisements

Tags:

6 Responses to “Standard Algorithm 1 – Counting Occurrences”

  1. hobbylobby Says:

    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?

  2. natalie Says:

    who is hobby lobby? how did u find the blog =S?

  3. mrwhiteside Says:

    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.

  4. hobbylobby Says:

    >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?

  5. Idetrorce Says:

    very interesting, but I don’t agree with you
    Idetrorce

  6. Ross Says:

    Well done mr whiteside – better than our teachers version
    Ross form liberton

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: