Frequency of Array Elements

This is a discussion on Frequency of Array Elements within the C Programming forums, part of the General Programming Boards category; hi eveyone, i want calculate the frequency of array element. so please i want small program of it & want ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Satara,MH,India
    Posts
    6

    Exclamation Frequency of Array Elements

    hi eveyone,

    i want calculate the frequency of array element. so please i want small program of it & want easy to explain hope you all friand help me in making best program.i also write some steps of it but still some problems. so i want your program & compare with my program....so please replay fast

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,720
    What have you tried?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2011
    Location
    saskatoon
    Posts
    3
    Post your code and it will be much easier to help you.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Location
    Satara,MH,India
    Posts
    6
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int a[5],freq=1,i,j,k,n=5;
    clrscr();
    printf("Enter Elements=\n");
    for(i=0;i<5;i++)
    scanf("%d",&a[i]);
    for(i=0;i<5;i++)
    {
    for(j=0;j<5;j++)
    if(a[i]==a[j])
    {
    freq++;
    }
    printf("Elements\tFrequency\n");
    .
    .
    .
    No idea guys

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    26
    This is wrong:

    Code:
    for(i=0;i<5;i++)
    {
    for(j=0;j<5;j++)
    if(a[i]==a[j])
    {
    freq++;
    }
    For every element in the array, you're cycling through the whole array again. Think about what freq would be if your array contained '11234' and why it's not giving you the correct answer.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Imagine if you were doing this by hand. You might have a tally column like this:

    Number Number of Times in the Array
    ==========================
    0 3
    1 5
    2 4
    3 1
    4 3

    etc.

    When you were done tallying up the totals, you could just refer to that list you made above ^^^^.

    Now, image that instead of a tally list, you used an int array, and used array[0] to tally up all the zero's, and
    array[1] to tally up all the one's, and
    array[2] to tally up all the two's,

    etc.

    I call that a "distribution array", but "frequency" or tally is also good. Note how easily it works in a loop, where:
    Code:
    for(i = 0; i < SIZE; i++) {
       array[yourArray[i]]++;
    Now that's pretty slick! Remember how that was done - this is a nice tool to keep in your programming toolbox.

  7. #7
    Registered User
    Join Date
    Sep 2011
    Location
    Satara,MH,India
    Posts
    6
    Thanks Guys But I Write New Program with new logic....
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int a[20],fr=0,n
    clrscr();
    printf("How Many Elements..\n");
    scanf("%d",&n);
    printf("Enter Elements..\n");
    for(i=0;i<n;i++)
    {
    scanf("%d",&a[i]);
    }
    printf("Which Elements Frequency do you want..?\n");
    scanf("%d",&op);
    for(i=0;i<n;i++)
    {
    if(a[i]==op)
    {
    fr++;
    }
    }
    printf("%d's Frequency is %d",op,fr);
    getch();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-07-2011, 10:22 PM
  2. Frequency Array
    By kiai_viper in forum C Programming
    Replies: 6
    Last Post: 07-21-2007, 01:05 AM
  3. ascii frequency array
    By Jamsan in forum C++ Programming
    Replies: 0
    Last Post: 03-26-2003, 10:54 AM
  4. Sort indexes of frequency array?
    By davidol in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2002, 09:04 AM
  5. # of Elements in Array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 04-22-2002, 01:43 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21