fork download
  1. /* Count the occurrences of a value in an array */
  2.  
  3. #include <stdio.h>
  4.  
  5. int main(void)
  6. {
  7. int myarray[] = {4,9,7,6,6,6,6,2,1,5};
  8. int count = 0;
  9. int to_find = 6;
  10.  
  11. for (int i = 0; i < 10; i++){
  12. printf ("myarray[%d] = %d\n",
  13. i, myarray[i]);
  14. if (myarray[i] == to_find){
  15. count++;
  16. printf (" found %d incremented count to %d\n",
  17. to_find, count);
  18. }
  19. }
  20. printf("# of 6s found is: %d\n", count);
  21.  
  22. return 0;
  23. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
myarray[0] = 4
myarray[1] = 9
myarray[2] = 7
myarray[3] = 6
 found 6 incremented count to 1
myarray[4] = 6
 found 6 incremented count to 2
myarray[5] = 6
 found 6 incremented count to 3
myarray[6] = 6
 found 6 incremented count to 4
myarray[7] = 2
myarray[8] = 1
myarray[9] = 5
# of 6s found is: 4