473,324 Members | 2,400 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,324 software developers and data experts.

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

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[b])
{
f=1;
break;
}
}
if(f==0)
{
for(b=a;b<=9;b++)
{
if (A[a]==A[b])
c++;
}
printf("%d=%d\n",A[a],c);
}
}
getch();
}
Jun 27 '08 #1
4 16250
sahil wrote:
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[b])
{
f=1;
break;
}
}
if(f==0)
{
for(b=a;b<=9;b++)
{
if (A[a]==A[b])
c++;
}
printf("%d=%d\n",A[a],c);
}
}
getch();
}
Really: consider comp.lang.c.
Best

Kai-Uwe Bux
Jun 27 '08 #2
"sahil" wrote:
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>
Don't use non-standard <conio.hif you don't need it. This program does
not need it. Replace getch() with getchar().
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[b])
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[b])
c++;
}
printf("%d=%d\n",A[a],c);
}
}
getch();
}

Jun 27 '08 #3
sahil <ja*********@gmail.comwrites:
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[i]. You have
basically two cases: either data[i] 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[i]);
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[i];
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_el ement_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[i]);
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[i];
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.

--
__Pascal Bourguignon__
Jun 27 '08 #4
In article <01dfc185-fb73-472d-ab7f-246ada526127
@u36g2000prf.googlegroups.com>, ja*********@gmail.com says...
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, intp;

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

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

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

std::map<int, intcounts;

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

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

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

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 27 '08 #5

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

Similar topics

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...
6
by: Buck Rogers | last post by:
Hi guys! Love your work! The below program is from K&R2, p22. ================================= #include <stdio.h> /* count digits, white space, others */ main() {
2
by: RadiationX | last post by:
How does this program work? // fig06_07.c -- COP2220 Example -- 040209 (sjd) // Student poll program #include <stdio.h> #include <stdlib.h> #define RESPONSE_SIZE 40 //...
2
by: pchahar | last post by:
Write a program to process a text file. The program will determine how many unique words there are in the text file that begin with each letter of the alphabet. The text file name will be given as a...
4
by: vadrama | last post by:
takes a list of grades and counts the amount A's, B's, C's etc... I am using a while statement to count the total number of grades which works great, but my if and else statements to count how...
5
by: MJK | last post by:
Suppose I have the following function in my program: void ExtractData(Ind *AM) { int i,j; char str; char c; FILE *ext=fopen("test.out","r"); //suppose I have N line each with M digits (in...
6
by: globalrev | last post by:
i ahve a program that takes certain textsnippets out of one file and inserts them into another. problem is it jsut overwrites the first riow every time. i want to insert every new piece of...
4
by: clrockwell | last post by:
Hey all, I could use some help with solving problem. Any suggestions are welcome and very appreciated. I have two arrays, $feeds and $feedDays. If they both have the same count, or $feeds...
1
by: sadpony | last post by:
Ok, so I wrote a program and it is supposed to let the user set the size of an array, input the numbers, find the subscript and value of the highest and lowest values, and then ask the user to input...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.