Counting occurances

Hi Guys,

Looking for a little help. This is a problem from an older Savitch C++ book, circa 2005 and it's giving me no end of troubles. Also note, I am not doing this as a homework assignment,it's just a hobby, so any help would be appreciated.

My issue is with the function called "track_and_count(int a[], int number_used)" where i am attempting to count the number of integers I have input and print them to the screen. So, for example, if I input:

-3
2
4
1
2
4

The output should be something like:

-3 1
1 1
2 2
4 2

I have tried many different things, but I am unable to get the code working correctly, I am close I think, but also very frustrated, as I can either count the number of occurances, or the number of times the integer appears, but not both.

If you have any ideas how to make my function work better, please let me know.

Thanks,

Mike

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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <string>
#include <cctype>//library to use 'toupper', 'tolower'....
#include <vector>
#include <fstream>
using namespace std;
using std::ifstream;
using std::ofstream;
using std::endl;
using std::ios;

void input(istream& input, int d[], int size, int& number_used);
void print(const int a[], int number_used);
void sort_array(int a[], int number_used);
void track_and_count(int a[], int number_used);


const int MAX=50;

int main()
{
 char choice, input_file[20];         
 int file_or_key, number_used, data[MAX]; 
 ifstream in;
         
 do
 {
  cout<<"Do you want to enter the data manualy or from a file?\n";
  cout<<"Press '1' for keyboard, or '2' file\n";
  cin>>file_or_key;
  if(file_or_key==1)
  {
   cout<<"Enter any number of integers less than fifty\n";                 
   input(cin,data,MAX,number_used);      
   cout<<"You have entered the following data\n";
   print(data,number_used);   
   track_and_count(data,number_used);
     
//   cout<<"After sorting you have the following\n";
//   sort_array(data,number_used);
   print(data, number_used); 
  }
  else if(file_or_key==2)
  {
   cout<<"Enter the name of the file you wish to open\n";
   cin>>input_file;
   in.open(input_file);
   if(in.fail())
   {
    cout<<"file failed to open\n";
    exit(1);                                                  
   }
   //code goes here
         
  }
  else
  {
   cout<<"Incorrect option\n";      
   exit(1);
  }
   
  
  cout<<"Again?\n";
  cin>>choice;        
 }while(choice=='y' || choice=='Y');         
  exit(1);
  
 _getch();
}
void track_and_count(int a[], int number_used)
{
 int temp, counter=0, j=0,n=0;
 bool appeared;
 
 while(j<number_used)
 { 
   appeared=false;                  
   temp=a[j];
   for(int i=j+1;i<number_used;i++)
   {
    if(temp==a[i]) 
    {
  /////////////////////////////////////
  //problem here!!                 
     appeared=true;    
                            
     counter++;   
  //problem here!!   
  //problem here!!   
  //problem here!!   
  //problem here!!   
  /////////////////////////////////////             
    }                  
   }
   if(!appeared)
    cout<<"Temp is "<<temp<<" and occurs "<<counter+1<<" times "<<endl; 
   j++;
   counter=0;
 }


}
void input(istream& input, int d[], int size, int& number_used)//input array 
{
 int next, index=0; 
 input>>next;          
 while(next>=-999 && index<size)
 {
  d[index]=next;
  index++;
  input>>next;      
 }        
 number_used=index;   
}
void print(const int a[], int number_used)//print array
{
 for(int i=0;i<number_used;i++)
 {
  cout<<a[i]<<",";               
 }   
 cout<<endl;                  
}
void sort_array(int a[], int number_used)//bubble sort
{
 bool swapped=true;
 int temp;
 while(swapped)
 {
  swapped=false;
  
  for(int i=0;i<number_used;i++)
  {
   if(a[i+1]<a[i])
   {
    swap(a[i+1],a[i]);      
    temp=a[i];
    a[i]=a[i+1];
    a[i+1]=temp;
    swapped=true;      
   }                            
  }            
 }                             
}

I haven't actually looked at your code, but looking at the problem this is what I would do. Create an std::map associating an int to an int. Go through your input, and use an expression like this:
 
++(map[i])

So you increment the number of occurrences (and this will work even if the word hasn't been inserted into the map yet because it will create a new key-value pair in the map and set the value to 0, so using ++ will put it at 1 on the first encounter) and then you can just print out the map.
Last edited on
Topic archived. No new replies allowed.