print values of array by popularity

This is a discussion on print values of array by popularity within the C Programming forums, part of the General Programming Boards category; Hello everyone. Given an array with integer values in the range [0, 100], print the values by popularity (number of ...

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    2

    print values of array by popularity

    Hello everyone.

    Given an array with integer values in the range [0, 100], print the values by popularity (number of time it appears in the array).

    example:
    array: 60, 60, 70, 80, 80, 80, 80, 100;

    output: 80 80 80 60 60 70 100.

    complexity restriction: should be linear.
    cant use advance data structure like lists or hashmaps, only arrays.
    structs are not allowed.

    my idea:
    to build counter array of buckets of size 101, and count each value.
    then i need to sort the counter array(its still linear), but how i can keep track that the value of 80 appeared 3 time?
    I mean i need to sort the values of the counter with the indexes as well.

    Thanks in advance
    Last edited by shishkabab; 01-11-2014 at 12:05 PM.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Use a struct for your counter array elements consisting of two ints: one for the count, one initialized to the original position number.
    Code:
    struct {
        int count;  // init to zero
        int value;  // init to the original position (0 to 100)
    } counts[101];
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    2
    Quote Originally Posted by oogabooga View Post
    Use a struct for your counter array elements consisting of two ints: one for the count, one initialized to the original position number.
    Code:
    struct {
        int count;  // init to zero
        int value;  // init to the original position (0 to 100)
    } counts[101];
    structs are not allowed

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Sorry, my mistake.
    How about "parallel arrays" then?
    Code:
    int values[101]; // init with 0 to 100
    int counts[101] = {0};
    Then when you sort counts, move the values elements around with the counts elements.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print out all input values
    By DecoratorFawn82 in forum C++ Programming
    Replies: 7
    Last Post: 03-21-2013, 12:42 PM
  2. Replies: 1
    Last Post: 02-23-2012, 05:26 PM
  3. Replies: 3
    Last Post: 10-21-2010, 01:39 PM
  4. print values in a bintree
    By mengqing in forum C++ Programming
    Replies: 4
    Last Post: 05-13-2008, 04:07 AM
  5. forgotten how to print values :(
    By darfader in forum C Programming
    Replies: 3
    Last Post: 08-12-2003, 10:47 AM

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