Frequency Output

Hey everyone. I'm working on a project and made a lot of attempts to try to get this right, but I'm having a lot of trouble despite numerous examples online.

I need to take an array (the array is full of up to 25 int numbers that the user inputs) and output the values that occur more than once.
Ie: 1 2 3 3 4 5 5
3 occurs 2 times
5 occurs 2 times

Should I be trying to make a 2D array to do this or should I just do a loop and test for duplicates and put those numbers in their own array and make a counter? I have the list (Sortlist[i]) being sorted via bubblesort but then I hit a roadblock. I can only use arrays to do this, no vectors etc. Thanks for any guidance.
awk '{ for( i=1; i<=NF; ++i ) n[$i]++; } END { for (i in n) printf "%d occurs %d times\n", i, n[$i]; }' < /tmp/input.txt

/tmp/input.txt
1 2 3 3 4 5 5

result
2 occurs 1 times
3 occurs 2 times
4 occurs 2 times
5 occurs 1 times
1 occurs 1 times


edit: doing it in C++ would require more work, but picking the right data structure makes it easier. i'd use an stl map<int,unsigned>
Last edited on
Hey thanks for the reply, but we can't use maps. We're basically limited to bouncing between arrays and using counters =/
is there a limit on how big the int can be? if it's not a big number you could do:

int count[MAXNUM-1];

initialize this sucker to zeros, loop through your array and index right in and increment.

if you don't care about speed (since you only have 25), you could also create a struct with three members - a value, a count, and a pointer to the next struct. in the loop, you do a linear search to update the counts or add a new record if it's a new number

either way, you should keep it simple and be able to do it in one pass
Last edited on
The maximum number of integers (n) is 25, but the actual size of the number could be infinite (and should be programmed as such). I'm not too familiar with pointers yet.
if you've sorted the array, and all you care about are repeats, then loop over the array and check the item at i-1 vs i, if they are the same, you have something that's occurring more than once - reset your counter and continue counting until it changes
I finished the frequency (also finding the mode) earlier and it compiled and ran great using g++, but now it's terribly broken in MS Visual 2010 and hardly runs. Any ideas what might cause this?
Last edited on
In case anyone feels like looking at it, this is the code that won't run now in Visual 2010. Wrote it separate from the main program so I need to implant it into the original program and change around the name of the array and variables and such.
#include <iostream>
using namespace std;

int main() {
   // i is the number of values entered
   int i = 0;
   int values[25];
   while (i < 25) {
     cout << "Input a value: ";
     cin >> values[i];
     if (values[i] == -999) {
       break;
     }
     ++i;
   }

   int freqArray[9999];
   // Count the frequency
   for (int j = 0; j < i; j++) {
       if (!freqArray[values[j]]) {
         freqArray[values[j]] = 1;
       } else {
         freqArray[values[j]] += 1;
       }
   }
   bool seenAlready[25];
   int mode = 0;
   for (int j = 0; j < i; j++) {
     if (freqArray[values[j]] > 1) {
       if (!seenAlready[values[j]]) {
         seenAlready[values[j]] = true;
         if(freqArray[values[j]] > mode){
             mode = freqArray[values[j]];
         }
         cout << "number " << values[j] << " occurs " << freqArray[values[j]] << " times " << endl;

   
       }
     }
   }
   bool seenAlready2[25];
   if (mode == 1) {
     cout << "There is no mode." << endl;
   } else {
     cout << "The mode is " << mode << "." << endl;
     int modearray[25];
     cout << "The modes are: ";
     for(int j = 0; j < i; j++) {
       if(freqArray[values[j]] == mode) {
            if (!seenAlready2[values[j]]) {
             seenAlready2[values[j]] = true;
                 cout << values[j];
        }
       }
     }
   }
} 
Bump to see if anyone might know about the compiling issue.
You're not initializing any of your arrays. This will most likely cause problems for freqArray, since most (at least 9974) of its elements will contain garbage.
Last edited on
Oh wow you're right and that fixed the entire thing. Now I just have to put it back in the original program and change the array names and such and hope I don't somehow goof that up. Thanks.
Topic archived. No new replies allowed.