fork download
  1.  
  2.  
  3. // ***************************************************
  4. // Function: frequency
  5. //
  6. // Description: Counts the number of times a specific
  7. // integer appears in an array.
  8. //
  9. // Parameters: theArray - array of integers
  10. // n - number of elements to check
  11. // x - integer to search for
  12. //
  13. // Returns: count - number of times x appears
  14. // in the first n elements
  15. //
  16. // ***************************************************
  17.  
  18. int frequency(int theArray[], int n, int x)
  19. {
  20. int count = 0; // Initialize counter to zero
  21. int i; // Loop index variable
  22.  
  23. // Loop through the first n elements of the array
  24. for (i = 0; i < n; i++)
  25. {
  26. // Check if the current element matches x
  27. if (theArray[i] == x)
  28. {
  29. count++; // Increase count when a match is found
  30. }
  31. }
  32.  
  33. // Return the total number of times x was found
  34. return count;
  35.  
  36. } // frequency
  37.  
  38.  
  39. #include <stdio.h>
  40.  
  41. int main(void)
  42. {
  43. int theArray[] = {5, 7, 23, 8, 23, 67, 23};
  44. int n = 7;
  45. int x = 23;
  46.  
  47. printf("Frequency of %d is %d\n",
  48. x, frequency(theArray, n, x));
  49.  
  50. return 0;
  51. }
  52.  
  53.  
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
Frequency of 23 is 3