/* Program CNT_OCC2.C ** ** Counts the occurances of number at specified index in an array. ** ** Peter H. Anderson, MSU, Feb 21, '97 ** */ #include <stdio.h> int count_occur2(int a[], int num_elements, int index); 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 index, num_occ; printf("\nArray:\n"); print_array(a, 10); for(index=0; index<10; index++) { num_occ = count_occur2(a, 10, index); printf("The value at index %d was found %d times.\n", index, num_occ); } } int count_occur2(int a[], int num_elements, int index) /* checks array a for number of occurrances of value at specified index */ { int i, count=0, value; value=a[index]; 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"); }