Counting Occurrance of Numbers

This is a discussion on Counting Occurrance of Numbers within the C Programming forums, part of the General Programming Boards category; Can anyone explain to me how to count the number of occurrance of any number? Say for example I inputed ...

  1. #1
    dat
    dat is offline
    Registered User
    Join Date
    Nov 2001
    Posts
    28

    Counting Occurrance of Numbers

    Can anyone explain to me how to count the number of occurrance of any number? Say for example I inputed this into my program:

    1 -3 -3 1 2 4 6 1

    Then my program should print this with the result in descending order:

    6 occurs 1
    4 occurs 1
    2 occurs 1
    1 occurs 3
    -3 occurs 2

    Any ideas?
    Dat
    http://hoteden.com <-- not an porn site damnit!

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Store the numbers in an array. Create a linked list, consisting of structs which contain the a number and the frequency of the number in the array, and ofcourse a pointer to the next node in the list.

    There are several strategies to creating the list:

    1. Read a number of the array, check if it occurs in the linked list. If yes, then increase frequency, else create a new node.

    2. Read a number of the array, count how many times it occurs in the array and create a node for the number.

    Finally sort the list based on frequency and print it out.

  3. #3
    dat
    dat is offline
    Registered User
    Join Date
    Nov 2001
    Posts
    28
    I'm only up to pointers and arrays in my book, so this problem can be solved w/o link lists.
    Dat
    http://hoteden.com <-- not an porn site damnit!

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    In that case you could use a 2D-array instead of the linked list. In the first column store the number and in the second the frequency.

  5. #5
    dat
    dat is offline
    Registered User
    Join Date
    Nov 2001
    Posts
    28
    Originally posted by Shiro
    In that case you could use a 2D-array instead of the linked list. In the first column store the number and in the second the frequency.
    Yeah I thought of a 2D array too but it doesn't work. If I input these numbers:

    1 2 3 4 1 2 3 4

    Then my array should be 2 (rows) by 8 (columes). I test the first element (the number 1) to see if it occurs again (only one more time, so 2 in total); so a[0][0] has the value 1, and a[1][0] should have the value 2. But later when my program goes to the 5th element of my array (the number 1 again) it'll do the counting again and assign a[4][1] the value 1. I'm not sure how to solve this.
    Dat
    http://hoteden.com <-- not an porn site damnit!

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    The problem is that you have the source and destination arrays being the same. To solve this problem you could use two arrays, one for the source and one for the destination. To give an impression of what I mean, here an example:

    #define NR_OF_INPUTS 8

    int source [NR_OF_INPUTS];
    int dest [NR_OF_INPUTS][NR_OF_INPUTS];

    Let source contain the following elements:

    source = [1, 2, 3, 7, 2, 1, 2, 5]

    Then the dest array would have the following elements:

    dest = [(1, 2), (2, 3), (3, 1), (7, 1), (5, 1)]

    In C it would look like this:

    dest [0][0] = 1; /* The number */
    dest [0][1] = 2; /* It's frequency in source */
    dest [1][0] = 2;
    dest [1][1] = 3;
    etc.

    So first fill the source-array. And then fill the dest-array. You could start with the first element and if you get to an element which already exists in the dest-array, you can just increment the frequency by one. Finally you can sort the dest-array by sorting the second column, which contains the frequencies. And don't forget to replace both number and frequency when replacements need to be done.

  7. #7
    dat
    dat is offline
    Registered User
    Join Date
    Nov 2001
    Posts
    28
    Originally posted by Salem
    Given that your output seems to be sorted, I would suggest that sorting the array before counting would be the easy thing to do

    If you know it's sorted, you only have to spot when the number changes. If you see a 6 followed by 4, you know there are no more 6's in the array, and thus you can just print how many you've seen so far.
    Good point
    Dat
    http://hoteden.com <-- not an porn site damnit!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting Numbers in Array, not counting last number!
    By metaljester in forum C++ Programming
    Replies: 11
    Last Post: 10-18-2006, 12:25 PM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 06:20 AM
  3. Counting occurrence of numbers in C
    By stabule in forum C Programming
    Replies: 1
    Last Post: 11-13-2005, 01:55 AM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. counting even numbers
    By slamit93 in forum C++ Programming
    Replies: 5
    Last Post: 04-24-2002, 02:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21