2020年12月23日星期三

Find the frequency of each element in array in C

My task is to find frequency of each element, and elements are between 0 and 100. -1 for the end of input.

I have no problem with finding frequency, but there is some problem with my code, and I don't know what it is. Probably with do-while loop. I am beginner, and I hope you could help.

int main() {    int n = 0, i = 0, a[100], c;    int b[101] = {0};      do {      scanf("%d", &c);      if (c == -1)        break;      else if (c < 0 || c > 100)        printf("\nNumbers have to be between 0 i 100!\n");      else {        a[i] = c;        i++;        n++;      }    } while (c != -1);      n--;    for (i = 0; i < n; i++)      b[a[i]]++;    for (i = 0; i < n; i++) {      if (b[i] != 0)        printf("Count of %d is %d.\n", i, b[i]);    }  }  
https://stackoverflow.com/questions/65433243/find-the-frequency-of-each-element-in-array-in-c December 24, 2020 at 10:26AM

没有评论:

发表评论