Help With Counting Occurance with Arrays

I need help on counting the occurrence of a number in my program.

Here's My Code:
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
55
56
57
	
void sort(int num[], int& count)
{
   //cout << " Using Sort!";
   for(int i =0; i <= count; i++)
   {
      for(int j = i+1; j <= count; j++)
      {
         int temp;
         
         if(num[i] < num[j])
         {
            temp = num[i];
            num[i] = num[j];
            num[j] = temp;
            
            
         }
      }
   }
   cout << "N   Count\n";
   for(int i = 0; i<= count; i++)
   {
      
      if(num[i] == num[i+1])
      {
      }
      else if(num[i]==-1337)
      {
      }
      else 
      {
//This Part!!! //
         cout << "\n" << num[i]<< "    " << Count(num, count) << endl; 
      }
      
      
   } 
   system("PAUSE"); 
}
int Count(int num[], int& count)
{
    int count_num = 1;
    for(int i =0; i <= count; i++)
   {
      if(num[i] == num[i+1])
         {
            count_num++;
         }
      else
         break;
      
   }
   return count_num; 
}



How Can I continuously call the Count(); Function again after the first number?

Last edited on
What are you trying to do?
I didn't understand.
What the function Count is supposed to do?
What the second part of function sort is supposed to do?
Please explain and it would be better if you post a runnable code (including main).
Moreover, some little corrections to avoid array bounds overflow,
given that the variable count represents the array dimension:
line 5 should be for(int i =0; i < count; i++),
line 7 should be for(int j = i+1; j < count; j++),
line 22 should be for(int i = 0; i < count; i++),
line 25 should be if((i < (count - 1)) && (num[i] == num[i+1])),
line 44 should be for(int i =0; i < (count - 1); i++)
the function Count is supposed to count how many times a number occurs.
the second part of sort is to output the numbers to screen.
Due to low response rate yesterday, I modified it.
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>                                                             
#include <fstream>                                                              
using namespace std;                                                            
const int SIZE = 50;                                                                       

void input(int a[], int size);                                                 
void sort(int num[], int& count);                                              
//int Count(int num[], int& count);                
                                            


int main()
{
   ifstream fin;                                                                
   int num[SIZE], count = 0, i=0, numUsed=0, nums = -1337;                      
   char ans, file_name[16];                                                     
   cout << "This Program will read in an array of numbers and outputs from largest to smallest and its occurence!\n";
   cout << "\nDo You want to provide the integers provided to the program? (Type 'Y' or 'y')\n";
   cin  >> ans;                                                                 
   if(ans =='Y' || ans == 'y')                                                  
   {
      input(num, SIZE);                                                          
   }
   else                                                                         
   {
      cout << "You Chose input integers from a file!\n";
      cout << "Please Enter the name of the Input File?(Maximun of 15 characters)ex. test.txt\n";
      cin  >> file_name;                                                        
   
      fin.open(file_name);                                                     
      if(fin.fail())                                                           
      {
         cout << "Input File Opening Failed.\n";                                
         system("PAUSE");                                                       
         exit(1);                                                               
      }
      while (fin >> nums)                                                       
      {
         num[i] = nums;                                                         
         i++;                                                                  
         numUsed ++;                                                            
      }
      sort(num, numUsed);                                                       
      fin.clear();                                                              
      fin.close();                                                              
   system("PAUSE");
   return 0;                                                                   
}}
void input(int num[], int size)                                                 
{
   int count=0, k=0;                                                            
   cout << "You Chose to Provide integers provided to the program!\n";
   cout << "Enter up to 50 whole number integers, Enter -1337 to End! \n";
   
   while(count < 50)
   {
      cin >> num[k];                                                            
      if(num[k] == -1337)                                                       
         break;                                                                 
      k++;                                                                      
      count ++;                                                                 
   }
   sort(num, count);                                                            
}
void sort(int num[], int& count)                                               
{
   //cout << " Using Sort!";
    
   for(int i =0; i <= count-1; i++)                                             
   {
      for(int j = i+1; j <= count-1; j++)                                       
      {
         int temp;                                                                   
         if(num[i] < num[j])                                                    
         {                                                                     
            temp = num[i];                                                      
            num[i] = num[j];
            num[j] = temp;
            
            
         }
      }
   }
   int number, occurence =0, count2 = 0, i;                                     
   cout << "N\tCount\n";                                                        
   for(i=0; i<=count-1; i++)                                                    
   {
      //number = num[i];                                                       
      for(int j=0; j<= count-1; j++)                                           
      {
         if(num[i] == num[j])                                                   
         {
            count2++;                                                           
            occurence = j;                                                       
         }
      }
      if(num[i] == -1337)                                                       
      {                                                                         
      }
      else
      {
         cout << endl << num[i] << "\t" << count2;                              
         i= occurence;                                                         
         count2 = 0;                                                            
      }
   }
  
   cout << endl;                                                                
   system("PAUSE");                                                             
}

void sort(int num[], int& count) should be void sort(int num[], int count) as you don't modify count.
I tested it and it seems to work. Do you have problems?

However, here is a simpler version of function sort, using standard functions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void sort(int num[], int count)                                               
{
	// sort desc
	std::sort(num, num + count, std::greater<int>());
	// create a vector to store unique numbers
	std::vector<int> unique_numbers(count);
	// copy only unique numbers
	auto it = std::unique_copy(num, num + count, unique_numbers.begin());
	// remove unused elements
	unique_numbers.erase(it, unique_numbers.end());
	// show output
	for (auto it = unique_numbers.begin(); it != unique_numbers.end(); ++it)
	{
		int number = *it;
		std::cout << std::endl << number << "\t" << std::count(num, num + count, number);
	}
	std::cout << std::endl;
	system("PAUSE");                                                             
}
Topic archived. No new replies allowed.