473,394 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

occurence problem

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;
}

the output is

1 2
3 1
4 2
5 2
6 1
7 2
8 1
9 1
7 1
4 1
the problem is that lets say if 4 follows 4 then no problem but if 4 4
6 4 then problem I could not find the way to fix that(I only compare
the preceeding and following so if the same element appears and
different from the previous one then it again counts that element. SO I
KNOW MY PROBLEM but NO WAY).

thx.

Feb 16 '06 #1
6 1538

"utab" <um********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Dear all,

...


Here is my solution:
-------------------

void occur(int *set, int size) {
int naj = set[0];
for (int i=0 ; i<size ; i++)
if (naj < set[i])
naj = set[i];
int *done = (int*)malloc(naj*sizeof(int));
for (int i=0 ; i<size ; i++) {
if (done[set[i]] == 1) continue;
int count = 0;
for (int j=0 ; j<size ; j++) {
if (set[i] == set[j])
count++;
}
printf("%d = %d\n",set[i],count);
done[set[i]] = 1;
}
}
Feb 16 '06 #2

utab 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
[]
the problem is that lets say if 4 follows 4 then no problem but if 4 4
6 4 then problem I could not find the way to fix that(I only compare
the preceeding and following so if the same element appears and
different from the previous one then it again counts that element. SO I
KNOW MY PROBLEM but NO WAY).


Use a counter array if the element range is small, as [0, 0x100) for
bytes. Use a std::map<> for larger ranges.

#include <iostream>
#include <map>

void count(unsigned char* p, unsigned len)
{
unsigned c[0x100] = {};
while(len--)
++c[*p++];
for(unsigned i = 0; i != 0x100; ++i)
if(c[i])
std::cout << i << ": " << c[i] << '\n';
}

template<class T>
void count(T const* p, unsigned len)
{
std::map<T, unsigned> c;
while(len--)
++c[*p++];
for(typename std::map<T, unsigned>::const_iterator i(c.begin()),
j(c.end()); i != j; ++i)
std::cout << i->first << ": " << i->second << '\n';
}
int main()
{
unsigned char a[] = { 0, 1, 2, 3, 1, 2, 1, 2, 4, 5, 6, 7, 8, 1 };
count(a, sizeof(a));
unsigned b[] = { 0, 1, 2, 3, 1, 2, 1, 2, 4, 5, 6, 7, 8, 1 };
count(b, sizeof(b) / sizeof(*b));
}

Feb 16 '06 #3

Tosha wrote:
"utab" <um********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Dear all,

...


Here is my solution:
-------------------

void occur(int *set, int size) {
int naj = set[0];
for (int i=0 ; i<size ; i++)
if (naj < set[i])
naj = set[i];
int *done = (int*)malloc(naj*sizeof(int));
for (int i=0 ; i<size ; i++) {
if (done[set[i]] == 1) continue;
int count = 0;
for (int j=0 ; j<size ; j++) {
if (set[i] == set[j])
count++;
}
printf("%d = %d\n",set[i],count);
done[set[i]] = 1;
}
}


Invoke the following calls and observe effects:

// case 1
occur(0, 0);

// case 2
int b[] = { 0, 0x7fffffff };
occur(b, 2);

Feb 16 '06 #4

"Maxim Yegorushkin" <ma***************@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...

Tosha wrote:
"utab" <um********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
> Dear all,
>
> ...
>
Here is my solution:
-------------------

void occur(int *set, int size) {
int naj = set[0];
for (int i=0 ; i<size ; i++)
if (naj < set[i])
naj = set[i];
int *done = (int*)malloc(naj*sizeof(int));
for (int i=0 ; i<size ; i++) {
if (done[set[i]] == 1) continue;
int count = 0;
for (int j=0 ; j<size ; j++) {
if (set[i] == set[j])
count++;
}
printf("%d = %d\n",set[i],count);
done[set[i]] = 1;
}
}


Invoke the following calls and observe effects:

// case 1
occur(0, 0);


Well, i supose it's shouldn't be a problem to put zero check at beggining of
function.
if (set==0)
return;

// case 2
int b[] = { 0, 0x7fffffff };
occur(b, 2);


Yes, it doesn't work for numbers larger than 2^30 or somethink like that.
Feb 16 '06 #5

"utab" <um********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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>
...


Try this... there should be no limits unless your set element is above
(2^64)-1
#include <cstdio>
#include <cstdlib>

typedef unsigned __int64 _int;

template <class type>
void occur(type *set, size_t size,char *printf_format) {
if (set==0) return;
type *check = (type*)malloc(size*sizeof(type));
size_t new_size = size;
for (size_t i=0 ; i<size ; i++) check[i] = set[i];
for (size_t i=0 ; i<new_size ; i++) {
for (size_t j=i+1; j<new_size ; j++) {
if (check[i] == check[j]) {
for (size_t k=j ; k<new_size-1 ; k++)
check[k] = check[k+1];
new_size--;
check = (type*)realloc(check,new_size*sizeof(type));
}
}
}
for (size_t i=0 ; i<new_size ; i++) {
type count = 0;
for (size_t j=0 ; j<size ; j++) {
if (check[i] == set[j])
count++;
}
printf(printf_format,check[i],count);
}
}

int main()
{
_int
a[]={18446744073709551615,1,1,3,4,5,5,6,7,8,9,7,4,1,1 8446744073709551615};
occur<_int>(a,15,"%I64u = %I64u\n");
}
Feb 16 '06 #6
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.
Feb 16 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tesla | last post by:
Hey guys, I have a string like "lalala: djkahsd : dajkdassd : adasd :" Is there a function to find the position of the "I"th occurence of a character/string? Like lets say I want to find out...
1
by: Roland | last post by:
hi all having designed my schema to use repeating groups of elements, I found that some applications (eg. Microsoft InfoPath) refused to recognised that element as a repeating element. I...
0
by: Armin Wagenknecht | last post by:
Hello, i am modelling a TopicMap, and I have the following problem: I want to use two scopes in the topicmap for one occurence which has to satsify BOTH scopes!! Example: I have the...
20
by: truetype | last post by:
Greetings! I consider to create an application using .NET Framework. It would be used by any kind user, mostly those who do not know anyting about programming and ..NET Framework. But have...
1
by: Gary Cobden | last post by:
Hi I have a routine that uses VBA to open a hidden occurence of Excel, and do background computations. However, in the event that the routine terminates abnormally, I have not been able to...
10
by: Sean Berry | last post by:
I need to find the second to last occurence of a "." in a string. Basically I am taking a URL like http://this.is.mydomin.com/path/to/file.txt and want to extract /path/to/file.txt I...
3
by: das | last post by:
Hi all, How can I get a row that has only one occurence in a table? Not through 'distinct' because this gets a single row that might have multiple occurences, I want to get only rows that have...
3
by: utab | last post by:
Dear all, I tried to solve the occurence problem: to find the distinct occurences of a word in an input. I know that I could use map and other STD lib functions. I tried to do it the hard way. I...
2
by: gauravguleria | last post by:
wht's the program which takes two strings using command line arguments and finds the occurence of second string in the first string.The program should return the starting position of first...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.