fork download
  1. #include <stdio.h>
  2.  
  3. // Function prototype
  4. int frequency(int theArray[], int n, int x);
  5.  
  6. // Function definition
  7. /**
  8.  * Function Name: frequency
  9.  *
  10.  * Function Block:
  11.  * Counts the number of times the item x appears among the first n elements of theArray.
  12.  *
  13.  * @param theArray[] The array of integers to search within.
  14.  * @param n The number of elements in the array to consider.
  15.  * @param x The integer value to count within the array.
  16.  * @return The frequency of x in the first n elements of theArray.
  17.  */
  18. int frequency(int theArray[], int n, int x) {
  19. int count = 0; // Initialize count to 0
  20.  
  21. // Loop through the first n elements of the array
  22. for (int i = 0; i < n; i++) {
  23. if (theArray[i] == x) {
  24. count++; // Increment count if the element matches x
  25. }
  26. }
  27.  
  28. return count; // Return the final count
  29. }
  30.  
  31. int main() {
  32. int theArray[] = {5, 7, 23, 8, 23, 67, 23};
  33. int n = 7;
  34. int x = 23;
  35.  
  36. // Call the frequency function and print the result
  37. int result = frequency(theArray, n, x);
  38. printf("The frequency of %d in the first %d elements is: %d\n", x, n, result);
  39.  
  40. return 0;
  41. }
Success #stdin #stdout 0s 5280KB
stdin
5, 7, 23,8, 23,67, 23
stdout
The frequency of 23 in the first 7 elements is: 3