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

Discussion in 'C++' started by sahil, May 27, 2008.

  1. sahil

    sahil Guest

    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();
    }
     
    sahil, May 27, 2008
    #1
    1. Advertising

  2. sahil

    Kai-Uwe Bux Guest

    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)
    > {
    > 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
     
    Kai-Uwe Bux, May 27, 2008
    #2
    1. Advertising

  3. sahil

    osmium Guest

    "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.h> if 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)


    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();
    > }
     
    osmium, May 27, 2008
    #3
  4. sahil <> writes:

    > 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.

    --
    __Pascal Bourguignon__
     
    Pascal J. Bourguignon, May 27, 2008
    #4
  5. sahil

    Jerry Coffin Guest

    In article <01dfc185-fb73-472d-ab7f-246ada526127
    @u36g2000prf.googlegroups.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, 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;
    }

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, May 28, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    509
  2. Replies:
    1
    Views:
    494
    Walter Roberson
    Aug 12, 2005
  3. Pat Maddox
    Replies:
    6
    Views:
    175
    Marcin Mielżyński
    Jan 20, 2006
  4. Yaþar Arabacý

    Count each unique element in list of lists

    Yaþar Arabacý, Nov 8, 2013, in forum: Python
    Replies:
    1
    Views:
    108
  5. Chris Angelico
    Replies:
    0
    Views:
    95
    Chris Angelico
    Nov 8, 2013
Loading...

Share This Page