fork download
  1. #include <stdio.h>
  2.  
  3. /* ==================================================
  4.  * Function Prototype
  5.  * ================================================== */
  6. int frequency (int theArray[], int n, int x);
  7.  
  8. /* ==================================================
  9.  * Test Driver (main)
  10.  * ================================================== */
  11.  
  12. // **************************************************
  13. // Function: main
  14. //
  15. // Description:
  16. // Tests the frequency() function with several arrays
  17. // and prints the results, including the provided
  18. // example where x = 23 appears 3 times.
  19. //
  20. // Parameters:
  21. // (none)
  22. //
  23. // Returns:
  24. // 0 on successful execution
  25. //
  26. // Notes:
  27. // - All values are integers as required.
  28. // - Includes edge-case tests (n = 0, x not present).
  29. // **************************************************
  30. int main(void)
  31. {
  32. /* Test 1: Provided example */
  33. int data1[] = {5, 7, 23, 8, 23, 67, 23};
  34. int n1 = 7;
  35. int x1 = 23;
  36. int result1 = frequency(data1, n1, x1);
  37. printf("Test 1: frequency of %d in first %d elements = %d (expected 3)\n",
  38. x1, n1, result1);
  39.  
  40. /* Test 2: Value not present */
  41. int data2[] = {1, 2, 3, 4, 5};
  42. int n2 = 5;
  43. int x2 = 99;
  44. int result2 = frequency(data2, n2, x2);
  45. printf("Test 2: frequency of %d in first %d elements = %d (expected 0)\n",
  46. x2, n2, result2);
  47.  
  48. /* Test 3: Multiple occurrences including first and last positions */
  49. int data3[] = {7, 7, 1, 2, 7, 3, 7, 4};
  50. int n3 = 8;
  51. int x3 = 7;
  52. int result3 = frequency(data3, n3, x3);
  53. printf("Test 3: frequency of %d in first %d elements = %d (expected 4)\n",
  54. x3, n3, result3);
  55.  
  56. /* Test 4: Edge case n = 0 (nothing to scan) */
  57. int data4[] = {10, 10, 10};
  58. int n4 = 0;
  59. int x4 = 10;
  60. int result4 = frequency(data4, n4, x4);
  61. printf("Test 4: frequency of %d in first %d elements = %d (expected 0)\n",
  62. x4, n4, result4);
  63.  
  64. return 0;
  65. }
  66.  
  67. /* ==================================================
  68.  * Function Implementation
  69.  * ================================================== */
  70.  
  71. // **************************************************
  72. // Function: frequency
  73. //
  74. // Description:
  75. // Counts how many times the value x appears in the
  76. // first n elements of theArray.
  77. //
  78. // Parameters:
  79. // theArray[] - array of integers to search
  80. // n - number of elements to examine (from index 0 to n-1)
  81. // x - value to count within theArray
  82. //
  83. // Returns:
  84. // frequency - number of times x appears among the first n elements
  85. //
  86. // Notes:
  87. // - If n <= 0, the function returns 0.
  88. // - Caller is responsible for ensuring n does not exceed the array's length.
  89. // **************************************************
  90. int frequency (int theArray[], int n, int x)
  91. {
  92. int frequency; /* how many times x is found */
  93. int i; /* loop counter */
  94.  
  95. frequency = 0; /* initialize count */
  96.  
  97. /* loop through every element in theArray up to n (exclusive) */
  98. for (i = 0; i < n; i++)
  99. {
  100. /* if the element equals x, increment frequency */
  101. if (theArray[i] == x)
  102. {
  103. frequency++;
  104. }
  105. }
  106.  
  107. return frequency;
  108. }
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
Test 1: frequency of 23 in first 7 elements = 3 (expected 3)
Test 2: frequency of 99 in first 5 elements = 0 (expected 0)
Test 3: frequency of 7 in first 8 elements = 4 (expected 4)
Test 4: frequency of 10 in first 0 elements = 0 (expected 0)