Count the number of times a number appears in an array

前端 未结 6 1308
我在风中等你
我在风中等你 2021-01-20 15:48

I\'m working on a small program that counts the number of times an integer appears in an array. I managed to do this but there is one thing I can\'t overcome.

My cod

6条回答
  •  说谎
    说谎 (楼主)
    2021-01-20 16:27

    You can use a parallel array, this example uses char[20] in order to save some space:

    #include 
    
    int count_occur(int a[], char exists[], int num_elements, int value);
    void print_array(int a[], int num_elements);
    
    int main(void) /* int main(void), please */
    {
        int a[20] = {2, 5, 0, 5, 5, 66, 3, 78, -4, -56, 2, 66, -4, -4, 2, 0, 66, 17, 17, -4};
        char exists[20] = {0}; /* initialize all elements to 0 */
        int num_occ, i;
    
        printf("\nArray:\n");
        print_array(a, 20);
    
        for (i = 0; i < 20; i++)
        {
            num_occ = count_occur(a, exists, 20, a[i]);
            if (num_occ) {
                exists[i] = 1; /* first time, set to 1 */
                printf("The value %d was found %d times.\n", a[i], num_occ);
            }
        }
    }
    
    int count_occur(int a[], char exists[], int num_elements, int value)
    /* checks array a for number of occurrances of value */
    {
        int i, count = 0;
    
        for (i = 0; i < num_elements; i++)
        {
            if (a[i] == value)
            {
                if (exists[i] != 0) return 0;
                ++count; /* it was found */
            }
        }
        return (count);
    }
    
    void print_array(int a[], int num_elements)
    {
        int i;
        for (i = 0; i

    This method is faster, as it skips values already readed and starts iterating from i in count_ocurr:

    #include 
    
    int count_occur(int a[], char map[], int num_elements, int start);
    void print_array(int a[], int num_elements);
    
    int main(void)
    {
        int a[20] = {2, 5, 0, 5, 5, 66, 3, 78, -4, -56, 2, 66, -4, -4, 2, 0, 66, 17, 17, -4};
        char map[20] = {0};
        int num_occ, i;
    
        printf("\nArray:\n");
        print_array(a, 20);
    
        for (i = 0; i < 20; i++)
        {
            if (map[i] == 0) {
                num_occ = count_occur(a, map, 20, i);
                printf("The value %d was found %d times.\n", a[i], num_occ);
            }
        }
    }
    
    int count_occur(int a[], char map[], int num_elements, int start)
    /* checks array a for number of occurrances of value */
    {
        int i, count = 0, value = a[start];
    
        for (i = start; i < num_elements; i++)
        {
            if (a[i] == value)
            {
                map[i] = 1;
                ++count; /* it was found */
            }
        }
        return (count);
    }
    
    void print_array(int a[], int num_elements)
    {
        int i;
        for (i = 0; i< num_elements; i++)
        {
            printf("%d ", a[i]);
        }
        printf("\n");
    }
    

提交回复
热议问题