/* Program CNT_OCC1.C ** ** Counts the occurances of a specified number in an array. ** ** Peter H. Anderson, MSU, Feb 21, '97 ** */ #include <stdio.h> int count_occur(int a[], int num_elements, int value); void print_array(int a[], int num_elements); void main(void) { int a[10] = {1, 2, 0, 0, 4, 5, 6, 9, 9, 17}; int num_occ, value; printf("\nArray:\n"); print_array(a, 10); for(value=0; value<25; value++) { num_occ = count_occur(a, 10, value); printf("The value %d was found %d times.\n", value, num_occ); } } int count_occur(int a[], 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) { ++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"); }