Problem counting the repeated number

This function check a set of values from a text file and show it on the output.


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
void MatchNumber(int b){


  vector<Rect> rects;

  ifstream theFile("CheckNumber.txt");

  double x1,y1,x2,y2;

  while(theFile >> x1 >> y1 >> x2 >> y2 ){

    rects.push_back(Rect(x1,y1, x2,y2));

  }
  
  int num=0;

int freq[101] = {0};

  int adj_count = 0;
  
  int neighbour=0;

    for (int x = 0; x < rects.size(); ++x) {
 if (rects[b].isAdjacent(rects[x])) {

if (x==b) {
  continue; 
}
      adj_count++;
 
 }
    }

freq[num]++; 

    cout<<"The common number is = "<<adj_count<<endl;
  
    cout<< "The "<<adj_count<<" repeated = "<< freq[num]<<" times"<<endl; 
    cout<<endl;
}


I want to calculate how many times the number is repeated. So I have used freq[num] in that function. But I am getting the output like this-


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
The common number is = 5
The 5 repeated = 1 times

The common number is = 6
The 6 repeated = 1 times

The common number is = 4
The 4 repeated = 1 times

The common number is = 5
The 5 repeated = 1 times

The common number is = 5
The 5 repeated = 1 times

The common number is = 8
The 8 repeated = 1 times

The common number is = 9
The 9 repeated = 1 times

The common number is = 6
The 6 repeated = 1 times

The common number is = 6
The 6 repeated = 1 times

The common number is = 8
The 8 repeated = 1 times


So the value of freq[num] only shows 1 instead of calculating the number of repetition.
How can I fix it?
Last edited on
Well you only change freq[] at line 35, outside of any loops, so naturally it will only be incremented that one time. Did you mean to have it in the loop or something?
@zhuge : I want the result something like-

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
The common number is = 5

The common number is = 6

The common number is = 4

The common number is = 5

The common number is = 5

The common number is = 8

The common number is = 9

The common number is = 6

The common number is = 6

The common number is = 8

The 4 repeated = 1 times
The 5 repeated = 3 times
The 6 repeated = 3 times
The 8 repeated = 2 times
The 9 repeated = 1 times


Any suggestion?
Last edited on
~ bump~
Topic archived. No new replies allowed.