Im having problems creating a function that checks how many times a number shows up in the array
The array is already sorted

im trying to loop through the array and theres be a counter to keep track of the number of times it appears and then a second array called trackerArray to place the value of that counter in correspondence to the value in the first array.

for(i=0; i < SIZE;i++){

   if(array[i]==array[current]){ 
       count++;
   }
   else if (array[i]> array[current]){
       trackerArray[i]=count;
       count=0;
       current++;
   }
}

the trackerArray is still filled with zeros after this... what went wrong?

Edited 3 Years Ago by toniann.midori

I think TrackArray should be a 2d array, first dimension is the number from array[] and the second dimension is the count. So all you have to do is iterate through array[]. For each value in array[] search the first dimension of TrackAwway to see if the number is already in TrackArray. If it is, then just increment the second dimension counter. If it's not in TrackArray then add it to the first dimension and set the corresponding second dimension to 1.

This semi-sudo code assumes you write a function called search() that searches TrackArray for the value in array[i] and returns either -1 (not found) or the index in TrackArray where it was found.

for(i = 0; i < SIZE; i++)
{
    int found = search(TrackArray, array[i]);
    if( found < 0)
        Add to TrackArray
    else
        increment TrackArray[i][1];
}
This article has been dead for over six months. Start a new discussion instead.