Count the number of times a number appears in an array

前端 未结 6 1310
我在风中等你
我在风中等你 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:17

    #include 
    #include 
    #include 
    
    int count_occur(int a[], int num_elements, int value, bool selected[]);
    void print_array(int a[], int num_elements);
    
    int main(void){
        int a[] = {2, 5, 0, 5, 5, 66, 3, 78, -4, -56, 2, 66, -4, -4, 2, 0, 66, 17, 17, -4};
        int size = sizeof(a)/sizeof(*a);
        bool ba[size];
        memset(ba, 0, sizeof ba);
        int num_occ, i;
    
        printf("\nArray:\n");
        print_array(a, size);
    
        for (i = 0; i

    Little improvement

    int count_occur(int a[], int num_elements, int index, bool selected[]);
    
    num_occ = count_occur(a, 20, i, ba);
    
    int count_occur(int a[], int num_elements, int index, bool ba[]){
        int i, count = 0;
        for (i = index; i

提交回复
热议问题