how to find multiple occurrences in an array

im not sure how to go about in doing. ive tried implementing some techniques ive seen but to no luck , they havent worked. Can anyone guide me to the right direction?

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream> 
#include <iomanip>
using namespace std;
int main()
{
   cout << fixed << setprecision(1);
   float userMin;
   float userMax;
   const int SIZE = 21;
   float arr[SIZE];
   const int SIZE2 = 21;
   float arr2[SIZE2];
   int count = 0;
   cout << "enter min ";
   cin >> userMin;
   cout << "enter max ";
   cin >> userMax;
   float inc = (userMax - userMin) / (SIZE-1);
   float x = inc*count + userMin;
   for (; x <= userMax;)
   {
      for (int e = 0; e < SIZE; e++)
      {
         arr[e] = x;
         arr2[e] = (0.0572*cos(4.667*x) + 0.0218*cos(12.22*x));
         cout << setw(15) << setprecision(1)<< arr[e]
            << setw(15) << setprecision(3) << arr2[e]
            << endl;
         count++;
         x = userMin + inc*count;
      }
   }
   int teco = 1;
   int mode1 = 1;
   int ltnum = arr2[0];
   for (int index = 1; index < SIZE; index++)
   {
      if (ltnum == arr2[index])
      {
         ++teco;
      }
      else
      {
         ltnum = arr2[index];
         if (teco > mode1)
         {
            mode1 = teco;
         }
         teco = 0;
      }
   }
       
   return 0;
}
Last edited on
What are you trying to accomplish?
the user inputs lets say -2 for userMin and 2 for userMax and the program gets the various numbers inbetween and calculates them in the formula and stores them in arr2. im trying to find multiple occurrences of each value to get the mode.
Last edited on
I would recommend using a std::map<int, int> where the key is the number and the value is the number of times it has occurred.

http://www.cplusplus.com/reference/map/map/
http://en.cppreference.com/w/cpp/container/map
Last edited on
ok ill look into it. Is there an easier method , im not familiar with map or that type of coding
Edit: also, if multiple values are repeated the same number of times, such as in my program , how would i do go about this?
Last edited on
std::map is by far the easiest solution in C++.

When multiple values appear equally as common, they are all the mode. The mode of a group of numbers doesn't always have to be one number.
well i tried a different method since im clueless with std::map, but it gives me the wrong output. would you mind checking it? Also, if there are multiples for mode, how can i display those values only?

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream> 
#include <iomanip>
using namespace std;
int main()
{
   cout << fixed << setprecision(1);
   float userMin;
   float userMax;
   const int SIZE = 21;
   float arr[SIZE];
   const int SIZE2 = 21;
   float arr2[SIZE2];
   int count = 0;
   cout << "enter min ";
   cin >> userMin;
   cout << "enter max ";
   cin >> userMax;
   float inc = (userMax - userMin) / (SIZE-1);
   float x = inc*count + userMin;
   for (; x <= userMax;)
   {
      for (int e = 0; e < SIZE; e++)
      {
         arr[e] = x;
         arr2[e] = (0.0572*cos(4.667*x) + 0.0218*cos(12.22*x));
         cout << setw(15) << setprecision(1)<< arr[e]
            << setw(15) << setprecision(3) << arr2[e]
            << endl;
         count++;
         x = userMin + inc*count;
      }
   }
   int e;
   for ( e = 0; e < SIZE; e++)
   {
      for (int j = 0; j < SIZE; j++)
      {
         if (arr2[e] == arr2[j])
         {
            c3++;
         }
         
      }
      cout << arr2[e] << "occurs: " << c3 << endl;

   }
       
   return 0;
}
Last edited on
Topic archived. No new replies allowed.