#include <stdio.h>
// Function prototype
int frequency(int theArray[], int n, int x);
// Function definition
/**
* Function Name: frequency
*
* Function Block:
* Counts the number of times the item x appears among the first n elements of theArray.
*
* @param theArray[] The array of integers to search within.
* @param n The number of elements in the array to consider.
* @param x The integer value to count within the array.
* @return The frequency of x in the first n elements of theArray.
*/
int frequency(int theArray[], int n, int x) {
int count = 0; // Initialize count to 0
// Loop through the first n elements of the array
for (int i = 0; i < n; i++) {
if (theArray[i] == x) {
count++; // Increment count if the element matches x
}
}
return count; // Return the final count
}
int main() {
int theArray[] = {5, 7, 23, 8, 23, 67, 23};
int n = 7;
int x = 23;
// Call the frequency function and print the result
int result = frequency(theArray, n, x);
printf("The frequency of %d in the first %d elements is: %d\n", x
, n
, result
);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CgovLyBGdW5jdGlvbiBwcm90b3R5cGUKaW50IGZyZXF1ZW5jeShpbnQgdGhlQXJyYXlbXSwgaW50IG4sIGludCB4KTsKCi8vIEZ1bmN0aW9uIGRlZmluaXRpb24KLyoqCiAqIEZ1bmN0aW9uIE5hbWU6IGZyZXF1ZW5jeQogKiAKICogRnVuY3Rpb24gQmxvY2s6CiAqIENvdW50cyB0aGUgbnVtYmVyIG9mIHRpbWVzIHRoZSBpdGVtIHggYXBwZWFycyBhbW9uZyB0aGUgZmlyc3QgbiBlbGVtZW50cyBvZiB0aGVBcnJheS4KICogCiAqIEBwYXJhbSB0aGVBcnJheVtdIFRoZSBhcnJheSBvZiBpbnRlZ2VycyB0byBzZWFyY2ggd2l0aGluLgogKiBAcGFyYW0gbiBUaGUgbnVtYmVyIG9mIGVsZW1lbnRzIGluIHRoZSBhcnJheSB0byBjb25zaWRlci4KICogQHBhcmFtIHggVGhlIGludGVnZXIgdmFsdWUgdG8gY291bnQgd2l0aGluIHRoZSBhcnJheS4KICogQHJldHVybiBUaGUgZnJlcXVlbmN5IG9mIHggaW4gdGhlIGZpcnN0IG4gZWxlbWVudHMgb2YgdGhlQXJyYXku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