In article <11**********************@o13g2000cwo.googlegroups .com>,
"utab" <um********@gmail.com> wrote:
Dear all,
I would like to find the occurence of numbers in an array(I solved this
problem before now I could not see the solution) but not with iterators
or vectors I want to apply them later, I tried sth like this
#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::endl;
int main(){
int a[]={1,1,3,4,5,5,6,7,8,9,7,4,1}; // 13 elements
int count;
int p,f; //previous and following element in the array
int b[13];
for(int i=0;i!=11;i++){ // upto the 13-2=11
count = 0;
if(i==0){
for (int j=i;j!=12;j++){ // upto 13-1=12
if(a[j]==a[i])
++count;
}
cout << a[i] << "\t" << count << endl;
}
else{
p=i; // preceeding
f=i+1; // following
if(a[f]!=a[p]){ // compare following and preceding elements
for (int k=f;k!=12;k++){
if(a[k]==a[f])
++count;
}
cout << a[f] << "\t" << count << endl;
}
}
}
return 0;
}
Try this:
int main() {
const int a_size = 13;
int a[]={1,1,3,4,5,5,6,7,8,9,7,4,1}; // 13 elements
int counted[a_size];
int counted_size = 0;
for ( int i = 0; i < a_size; ++i ) {
// check for repeat number
bool repeat = false;
for ( int j = 0; j < counted_size && !repeat; ++j ) {
if ( a[i] == counted[j] ) repeat = true;
}
if ( !repeat ) {
// put a[i] in list of already counted numbers
counted[counted_size] = a[i];
++counted_size;
// count the occurances of a[i]
int count = 1;
for ( int j = i + 1; j < a_size; ++j ) {
if ( a[i] == a[j] )
++count;
}
std::cout << a[i] << "\t" << count << '\n';
}
}
}
But here's how I would do it. I like to separate the output from the
computation so 'value_count' is the function that actually counts how
many of each number are in the set, whereas 'main' outputs the result.
typedef struct {
int value;
int count_of;
} assoc;
int value_count( const int* set, int size, assoc** out ) {
int out_cap = 2;
int out_size = 0;
*out = (assoc*)malloc( sizeof( assoc ) * out_cap );
if ( !out ) throw std::bad_alloc();
while ( size-- ) {
bool repeat = false;
for ( int j = 0; j < out_size && !repeat; ++j ) {
if ( *set == (*out)[j].value ) {
++(*out)[j].count_of;
repeat = true;
}
}
if ( !repeat ) {
if ( out_size == out_cap ) {
out_cap = static_cast<int>( out_cap * 1.5 );
*out = (assoc*)realloc( *out, sizeof( assoc ) * out_cap );
if ( !out ) throw std::bad_alloc();
}
(*out)[out_size].value = *set;
(*out)[out_size].count_of = 1;
++out_size;
}
++set;
}
return out_size;
}
int main() {
int a[]={1,1,3,4,5,5,6,7,8,9,7,4,1}; // 13 elements
assoc* result = 0;
int out_size = value_count( a, 13, &result );
for ( int i = 0; i < out_size; ++i )
printf( "%d\t%d\n", result[i].value, result[i].count_of );
free( result );
}
the output is:
1 3
3 1
4 2
5 2
6 1
7 2
8 1
9 1
Of course, this would be trivial (and more flexable) in c++
rather than c.
struct map_value_outputer {
ostream& os;
map_value_outputer( ostream& o ): os( o ) { }
template < typename X, typename Y >
void operator()( const pair< X, Y >& v ) {
os << v.first << "\t" << v.second << '\n';
}
};
int main() {
int a[]={1,1,3,4,5,5,6,7,8,9,7,4,1}; // 13 elements
map<int, int> result;
for ( int* p = a; p != a + 13; ++p )
++result[*p];
for_each( result.begin(), result.end(), map_value_outputer( cout ) );
}
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.