fork download
  1. #include <stdio.h>
  2.  
  3. int main(void) {
  4. // **************************************************
  5. // Function: frequency
  6. //
  7. // Description: Calculates the frequency of an item x
  8. // in an array with n elements.
  9. //
  10. // Parameters: theArray - array being checked
  11. // n - the number of elements in the array
  12. // x - the item being counted in the array
  13. //
  14. // Returns: area - area of sector
  15. //
  16. // ***************************************************
  17. int anArray[7] = {5, 7, 23, 8, 23, 67, 23};
  18.  
  19. int frequency (int theArray[], int n, int x)
  20. {
  21. int frequency; /* how many times n is found */
  22.  
  23. frequency = 0; /* initialize count */
  24.  
  25. /* loop through every element in theArray */
  26. for (int i = 0; i < n; ++i)
  27. {
  28. /* TODO - if the element x is found, increment frequency */
  29. if (theArray[i] == x)
  30. {
  31. frequency++;
  32. }
  33.  
  34. }
  35.  
  36. return frequency;
  37. }
  38.  
  39. printf("%i", frequency(anArray, 7, 23));
  40.  
  41. return 0;
  42. }
  43.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
3