write a program to count occurence of each element in an array

S

sahil

Hello frends i am learning c language, I want to make a program which
count occurence of each element in an array .I write following code
for it but ity is not giving me desired result.pls help me.
#include<stdio.h>
#include<conio.h>
void main()
{
int A[10],f,a,b,c;
printf("enter 10 elements");
for(a=0;a<=9;a++)
scanf("%d",&A[a]);
for(a=0;a<=9;a++)
{
f=0;
c=0;
for(b=0;b<9;b++)
{
if(A[a]==A)
{
f=1;
break;
}
}
if(f==0)
{
for(b=a;b<=9;b++)
{
if (A[a]==A)
c++;
}
printf("%d=%d\n",A[a],c);
}
}
getch();
}
 
K

Kai-Uwe Bux

sahil said:
Hello frends i am learning c language,

Consider comp.lang.c.
I want to make a program which
count occurence of each element in an array .I write following code
for it but ity is not giving me desired result.pls help me.
#include<stdio.h>
#include<conio.h>
void main()
{
int A[10],f,a,b,c;
printf("enter 10 elements");
for(a=0;a<=9;a++)
scanf("%d",&A[a]);
for(a=0;a<=9;a++)
{
f=0;
c=0;
for(b=0;b<9;b++)
{
if(A[a]==A)
{
f=1;
break;
}
}
if(f==0)
{
for(b=a;b<=9;b++)
{
if (A[a]==A)
c++;
}
printf("%d=%d\n",A[a],c);
}
}
getch();
}


Really: consider comp.lang.c.


Best

Kai-Uwe Bux
 
O

osmium

sahil said:
Hello frends i am learning c language, I want to make a program which
count occurence of each element in an array .

What does it *mean* to count occurrence of each element in an array?

I don't know what it looks like on your end but by the time it gets to me
the parenthesizing is a nightmare. After parenthesizing, and not having the
foggiest notion of what this program should do, I made the observation
embedded later.


/I write following code
for it but ity is not giving me desired result.pls help me.
#include<stdio.h>
#include<conio.h>

void main()

That will not work with a lot of comiplers. use int main()
{
int A[10],f,a,b,c;
printf("enter 10 elements");
for(a=0;a<=9;a++)
scanf("%d",&A[a]);
for(a=0;a<=9;a++)
{
f=0;
c=0;
for(b=0;b<9;b++)
{
if(A[a]==A)


How can that test ever fail? The very first iteration checks to see if A[0]
== A[0] , the loop is exited and the program is exited. End of story.
{
f=1;
break;
}
}
if(f==0)
{
for(b=a;b<=9;b++)
{
if (A[a]==A)
c++;
}
printf("%d=%d\n",A[a],c);
}
}
getch();
}
 
P

Pascal J. Bourguignon

sahil said:
Hello frends i am learning c language, I want to make a program which
count occurence of each element in an array .I write following code
for it but ity is not giving me desired result.pls help me.

So, let's take an example.

You have something like:

typedef int element_type;
typedef struct
unsigned int size;
element_type elements[];
} element_vector;

/* element_vector data={9,{1,2,3,1,2,3,1,2,1}}; */

and you want to get something like:

typedef struct {
element_type element;
unsigned int count;
} unique_element;
typedef struct {
unsigned int size;
unique_element elements[];
} unique_element_vector;

/* unique_element_vector counts[]={3,{{1,4},{2,3},{3,2}}}; */


This will be a simple iterative processus.

Assume that you have processed data[] from 0 to i-1, and therefore
you have a vector counts filled from 0 to number_of_unique_elements-1
with unique elements, and their counts.

The iteration step will consist in processing data. You have
basically two cases: either data is already in counts[], then you
will have to find where, and increment counts[w].count, or it is not,
and then you will have to add it to counts. This is an alternative
processus:

unsigned int w=findInUniqueElements(counts,data.elements);
if(w<counts.size){
/* already in */
counts.elements[w].count++;
}else{
/* insert the first one */
w=counts.size;
counts.size++;
counts.elements[w].element=data.elements;
counts.elements[w].count=1;
}

and finally we increment the index:

i++;


Now the question is what will be the stop condition of this loop.
Obviously, when we've processed all the data, that is, the stop
condition is:

i>=data.size


Finally, what will be the initial conditions? When we've not
processed anything, and therefore the counts vector is empty:

i=0;
counts.size=0;

Now, since you've not specified any limit for the vector sizes, and
we're programming in C, we will have to allocate them space for the
elements dynamically, and therefore use pointers:


typedef int element_type;
typedef struct
unsigned int size;
element_type* elements;
} element_vector;

typedef struct {
element_type element;
unsigned int count;
} unique_element;
typedef struct {
unsigned int size;
unique_element* elements;
} unique_element_vector;


The maximum number of unique elements is actually the number of input
elements, so we can preallocate this space:

counts.elements=malloc(data.size * sizeof(counts.elements[0]));


And finally we can wrap the iterative processus up:

initialization
while(not(stop_condition)){
iteration;
}

which gives the following function:

unique_element_vector* countElements(element_vector* data){
unique_element_vector* counts=check_malloc_result(malloc(sizeof(unique_element_vector)));
unsigned int i=;
counts->size=0;
counts->elements=check_malloc_result(malloc(data.size * sizeof(counts.elements[0])));
while(not(i>=data->size)){
unsigned int w=findInUniqueElements(counts,data->elements);
if(w<counts->size){
/* already in */
counts->elements[w].count++;
}else{
/* insert the first one */
w=counts->size;
counts->size++;
counts->elements[w].element=data->elements;
counts->elements[w].count=1;
}
}
return(counts);
}

Of course, now you have to implement

unsigned int findInUniqueElements(unique_element_vector* counts,element_type element);

but this is a simplier problem.
 
J

Jerry Coffin

Hello frends i am learning c language, I want to make a program which
count occurence of each element in an array .I write following code
for it but ity is not giving me desired result.pls help me.

Since you're posting in comp.lang.c++, I'm going to guess that a
solution in C++ is usable:

#include <iostream>
#include <vector>
#include <map>

typedef std::pair<int, int> p;

// Cheating...
namespace std {
std::eek:stream &operator<<(std::eek:stream &os, p const &data) {
return os << data.first << "\t" << data.second;
}
}

int main() {
std::vector<int> A;
std::cout << "Enter elements";

std::copy(std::istream_iterator<int>(std::cin),
std::istream_iterator<int>(),
std::back_inserter(A));

std::map<int, int> counts;

for (int i=0; i<A.size(); i++)
++counts[A];

std::cout << "Counts:\n";

std::copy(counts.begin(), counts.end(),
std::eek:stream_iterator<p>(std::cout, "\n"));
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top