3

I have generated an array of 5 random integers from 1-5. Here is what the array looks like now: myArray[5] = {3, 3, 1, 4, 5}

I have now sorted the array of 5 integers in ascending order, from least to greatest.

myArray[5] = {1, 3, 3, 4, 5}

I now need to count the number of occurrences a specific integer has and make a table of it.

Such as:

Number: Count: 
1:1 
2:0 
3:3 
4:0 
5:1

The farthest I've gotten has been looping through the array. I am having a hard time talling the numbers and creating a count of how many occurances there are.

Not using any maps, or iterations, etc. I am trying to get this count. Here is what I have tried already:

int counts[10];

for (int x = 0; x <= 10; x++){
    int counter = 0;
    for (int j = 0; j < ARRAY_SIZE; j++){
        if (x == myArray[j]){
            counts[x] == counter++;
        }
    }

    cout << "Number: " << x << "Number of Occurances: " << counts[counter]<< "\n";
}

However, my output is incredibly wrong.

FooBar
  • 314
  • 1
  • 3
  • 15
  • Make an array of counts. Use the value in the current element as the index in that array, and increment the count. BTW, there's no need to sort the array for this. – Barmar Feb 14 '15 at 01:49
  • 1
    Actually, since this is C++, you should use a `std::map`. – Barmar Feb 14 '15 at 01:51
  • Hey @Barmar I am still interested in this solution here. I have tried to do what you've explained, but I am getting some strange behaviors. Could you please elaborate more on this solution? – FooBar Feb 14 '15 at 03:15
  • If you intend to use map for storing the number frequency, you don't have to sort the array first – sanjayk79 Feb 14 '15 at 04:02
  • Please add your attempted solution to the question. How else can I tell you what you're doing wrong, so you can learn from your error? – Barmar Feb 14 '15 at 13:07
  • @Barmar I just updated. – FooBar Feb 14 '15 at 14:28
  • `counts[x] == counter++;` --> `counts[x] = counter++;` for starters. There's also no need for the counter variable, e.g.: `counts[x]++`. Debugging catches these kinds of typos. – keyser Feb 14 '15 at 14:36
  • You're writing outside the bounds of the `counts` array. Since it's declared `count[10]`, the indexes run from 0 to 9. But your array goes from 0 to 10. It should be `for (int x = 0; x < 10; x++)` – Barmar Feb 15 '15 at 05:34
  • `counts[counter]` should just be `counter`. – Barmar Feb 15 '15 at 05:42

2 Answers2

6

Use a std::map to map integers to their counts.

std::map<int, int> counts;
for (int i = 0; i < 5; i++) {
    counts[myArray[i]]++; // increment the counter for the current value
}

Now you can print the keys and values in counts. See How to loop through a C++ map of maps? for how to do this.

You can do it with an array instead of a map. The only difference is that it won't automatically expand to handle larger values (unless you use malloc and realloc to make it dynamically sized).

#define MAX_VALUE 9
int counts[MAX_VALUE+1] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
for (int i = 0; i < ARRAY_SIZE; i++) {
    if (myArray[i] <= MAX_VALUE) {
        counts[myArray[i]]++; // increment the counter for the current value
    }
}
for (int j = 0; j <= MAX_VALUE; j++) {
    cout << "Number: " << j << "Number of Occurances: " << counts[j] << endl;
}
Community
  • 1
  • 1
Barmar
  • 596,455
  • 48
  • 393
  • 495
0

Make a hash and initialize with zero.

int hash[10000]={0};
for(int i=0;i<n;i++)
{  
     hash[arr[i]]++;
}

the hash at index arr[i] will hold value which is the count of occurrence of that number. As hash[arr[i]]++ will increment the count at index equal to the value of arr[i]. This way we can check which value occurred how many times by checking hash[arr[i]] where arr[i] is value to be checked.

Ankit Mishra
  • 323
  • 3
  • 13