Home > Back-end >  How to find how many times a specific element is repeated in an array?
How to find how many times a specific element is repeated in an array?

Time:04-23

I have generated an array that has 10 integers. I have to find how many times an element has been repeated, but I always get 0 as an output. Here in this code, countcolor variables is my count values. For example countcolor1 is my count value for the integer 1. Here is what I have so far:

#include <stdio.h>
#include <time.h>

int i;

double entropy_calculator(int bucket[10]);

int main() {
    srand(time(NULL));
    int countings;

    int bucket[10];
    for (i = 0; i < 10;   i) {
        bucket[i] = 1   rand() % 10;
        printf("%d \n", bucket[i]);
    }
    countings = entropy_calculator(bucket);
    return 0;
}

double entropy_calculator(int bucket[10]) {
    int x;
    int countcolor1 = 0, countcolor2 = 0, countcolor3 = 0,
        countcolor4 = 0, countcolor5 = 0, countcolor6 = 0;

    for (x = 0; x <= 10;   x) {
        if (bucket[10] == 1)
            countcolor1  ;

        if (bucket[10] == 2)
            countcolor2  ;

        if (bucket[10] == 3)
            countcolor3  ;

        if (bucket[10] == 4)
            countcolor4  ;

        if (bucket[10] == 5)
            countcolor5  ;

        if (bucket[10] == 6)
            countcolor6  ;
    }
    printf("%d,%d,%d,%d,%d,%d",
           countcolor1, countcolor2, countcolor3,
           countcolor4, countcolor5, countcolor6);
}

CodePudding user response:

Notes on your code

  • The return type of entropy_calculator is double but you do not return anything. If you do not wish to return anything, set the return type to void.

  • In main, you attempt to assign the return value of entropy_calculator to an int called countings. If you are going to return some double value, countings should be a double.

  • Undefined behavior. According to the C Standard, behavior of a program is undefined in the event that an array subscript is out of range. bucket is an array of 10 integers. The valid indices of an array with N elements is in general 0, 1, 2, ..., N - 1; in other words, the first element is assigned the index 0, the second element is assigned the index 1, ..., the Nth element is assigned the index N - 1. Thus, the valid indices into the bucket array is any integer in the closed interval [0, 10 - 1] = [0, 9]. In your entropy_calculator function, you are attempting to access the element of bucket with index 10; the only valid indices are one of 0, 1, ..., 9.

  • In entropy_calculator, suppose we change all the 10s to a 9 so that the loop runs from x = 0, x = 1, .. . , x = 9 and checks of the form ([bucket[10] == j) for some j in [1, 6] are replaced with ([bucket[9] == j). All of these six checks are simply checking if the 10th element of bucket is one of 1, 2, ..., or 6. You have disregarded the other 9 randomly-generated numbers in bucket so you are never involving them in the count. You are also disregarding the other possible randomly-generated values, namely, 7, 8, 9, and 10 as you are currently only comparing the 10th element of bucket against 1, 2, ..., and 6.

Solution

I assume your task is to

  • generate 10 random integers in [1, 10] and store them in an array of ints, say, bucket.
  • create a function that accepts as an argument an array of 10 ints (i.e. a pointer to an int) and prints the number of occurrences of 1s, 2s, ..., 10s in the array.

To make the program slightly more general, we define the macro MAX_LEN and make it represent the number 10.

In main, first, we initialize the random number generator by setting the seed to the current time. Second, we define an array of MAX_LEN ints called bucket. Third, we fill each element of bucket with a pseudo-random integer in [1, MAX_LEN]. Lastly, we call the functionentropy_calculator, passing bucket as the sole argument, and then return 0.

In the function, entropy_calculator, we define an array of MAX_LEN ints called counts, with each element initialized to zero. We create our own internal mapping such that the nth element of counts represents the number of ns found in bucket, for each n in {1, 2, ..., MAX_LEN}. Equivalently, for each n in {1, 2, ..., MAX_LEN}, the number of ns found in bucket is represented by the element of counts with index n - 1. We then loop over the elements of the bucket array and, using our mapping, increment the corresponding element in the counts array. We then print out all the elements of counts.

  • For example, for some i in the set of valid indices of the arrays in our program, i.e. {0, 1, ..., MAX_LEN - 1}, if we find that bucket[i] is 5, then we want to increment the 5th element of counts (since the nth element of counts counts the number of ns generated) which is counts[5 - 1] or, more generally, counts[bucket[i] - 1].

Program

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX_LEN 10

void entropy_calculator(int bucket[]);

int main(void) {

    /* initialize random number generator */
    srand((unsigned) time(NULL));

    /* will contain MAX_LEN random ints in [1, MAX_LEN] */
    int bucket[MAX_LEN];

    /* generate pseudo-random ints in [1, MAX_LEN], storing them in bucket */
    for (int i = 0; i < MAX_LEN; i  ) {

        bucket[i] = 1   rand() % MAX_LEN;
        printf("%d\n", bucket[i]);

    }

    entropy_calculator(bucket);

    return 0;

}

/****************************************************************************
 * entropy_calculator: given an array of MAX_LEN integers in [1, MAX_LEN],  *         *
 *                     prints the number of occurrences of each integer in  *
 *                     [1, MAX_LEN] in the supplied array                   *
 ****************************************************************************/

void entropy_calculator(int bucket[]) {

    int counts[MAX_LEN] = {0};    /* initialize counts to all 0s */
    int i;                        /* loop variable */

    for (i = 0; i < MAX_LEN; i  )
        counts[bucket[i] - 1]  ;

    /* printing all elements of counts */
    for (i = 0; i < MAX_LEN; i  ) {

        if (i % 4 == 0) printf("\n");
        printf(" -*: %d", i   1, counts[i]);

    }
    printf("\n");

}

Example Session

3
9
2
6
10
9
3
8
3
1

  1*: 1  2*: 1  3*: 3  4*: 0
  5*: 0  6*: 1  7*: 0  8*: 1
  9*: 2 10*: 1

Simplified version


If the task is to simply generate MAX_LEN (macro representing the value 10) random integers in [1, MAX_LEN] and to count how many 1s, 2s, ..., (MAX_LEN - 1), MAX_LENs are generated, then it can simply be done as follows.

We create an array of MAX_LEN integers, called counts. The valid indices associated with counts is 0, 1, ..., MAX_LEN - 1. We form our own internal mapping such that the element of counts with index n - 1 represents the number of randomly-generated ns for n in {1, 2, ..., MAX_LEN}.

As we generate a random integer in [1, MAX_LEN], we assign it to cur, and we increment the element of counts with index cur - 1 as this element represents the number of occurrences of the number cur.

Program

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX_LEN 10

int main(void) {

    srand((unsigned) time(NULL));    /* initialize random number generator */
    int counts[MAX_LEN] = {0};       /* initialize counts to all 0s */
    int i, cur;

    for (i = 0; i < MAX_LEN; i  ) {

        cur = 1   rand() % MAX_LEN;  /* pseudo-random int in [1, MAX_LEN] */
        printf("%d\n", cur);
        counts[cur - 1]  ;

    }

    /* printing all elements of counts */
    for (i = 0; i < MAX_LEN; i  ) {

        if (i % 4 == 0) printf("\n");
        printf(" -*: %d", i   1, counts[i]);

    }
    printf("\n");

    return 0;

}

Example Session

8
4
6
2
4
1
10
9
2
10

  1*: 1  2*: 2  3*: 0  4*: 2
  5*: 0  6*: 1  7*: 0  8*: 1
  9*: 1 10*: 2

CodePudding user response:

#include<stdio.h>
#include<time.h>
int i;
double entropy_calculator(int bucket[10]);
int main()
{

    srand(time(NULL));
    int countings;
    int bucket[10];
    for(i=0; i<10;   i)
    {
        bucket[i] = 1   rand() % 10;
        printf("%d ", bucket[i]);
    }
    printf("\n");
    countings = entropy_calculator(bucket);
    return 0;
}

double entropy_calculator(int bucket[10])
{

    int x;
    int countcolor1=0, countcolor2=0, countcolor3=0, countcolor4=0, countcolor5=0, countcolor6=0;
    for(x=0; x<10;   x)
    {
        if (bucket[9]==1)

            countcolor1  ;

        
        if (bucket[9]==2)

            countcolor2  ;


        if (bucket[9]==3)

            countcolor3  ;


        if (bucket[9]==4)

            countcolor4  ;


        if (bucket[9]==5)

            countcolor5  ;


        if (bucket[9]==6)

            countcolor6  ;
             

    }
    printf("%d,%d,%d,%d,%d,%d",countcolor1,countcolor2,countcolor3,countcolor4,countcolor5,countcolor6);


}
  • Related