not sure what to do

I need to count the number of times each number appears in an array. I have no idea what to do in order to do this can i change the sort function to do this? or do i need another function to go through the array after it is sorted?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void sort(int numlist[], int size)
{
   int out, in, temp;

   for (out = 0; out < size-1; out++)     //for each element index
      for (in = out+1; in < size; in++)   //compare with one element index down
         if (numlist[out] > numlist[in])  //if out element index is greater than
         {                                //in element index
            temp = numlist[in];           //swap them
            numlist[in] = numlist[out];
            numlist[out] = temp;
         } 

} 
I would use a std::map to map the number to the number of times is appears.
i tried adding another function and another array but now the output is totally wrong

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void sort(int numlist[], int size)
{
   int out, in, temp;

   for (out = 0; out < size-1; out++)     // for each element index
      for (in = out+1; in < size; in++)   // compare with one element index down
         if (numlist[out] > numlist[in])  // if out element index is greater than
         {                                // in element index
            temp = numlist[in];           // swap them
            numlist[in] = numlist[out];
            numlist[out] = temp;
         } 

} 
//
void count_distinct(int numlist[], int size, int distinct[])
{
   int out, in, temp = 0;

   for (out = 0; out < size-1; out++)     // for each element index
      for (in = out+1; in < size; in++)   // compare with one element index down
         if (numlist[out] = numlist[in])  // if out element index equals
         {                                // in element index
            temp++;                       // increase temp
            distinct[in] = temp;
            temp = 0;
         } 

} 
Last edited on
Topic archived. No new replies allowed.