Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question7 a) Write a function called numOccurrences which accepts an array of in

ID: 3910048 • Letter: Q

Question

Question7 a) Write a function called numOccurrences which accepts an array of integers, its size, and a value, and returns the number of times that value appears in the array. Assume that all input values are valid (i.e no error checking is needed on the array or its size). Comments and proper indentation are required for full marks. For example, if an array of size 10 contained 1, 2, 4, 2, 1, 2, 4, 3, 5, 4 ), then the value 2 occurs 3 times. (Also, the value 1 occurs 2 times, the value 5 occurs I time, and the value 7 occurs 0 times.) 4 b) Write a void function called mostCommon which accepts an array of integers and its size, and "returns" the element that occurs most often, and the number of times that it appears in the array. If there is a tie for the most common element, any of the most common elements may be returned. Your solution must make use of (i.e. call) the function in part (a) for full marks. Again, you may assume that all input values are valid (i.e. no error checking is needed on the array or its size). Comments and proper indentation are required for full marks. For example, for the array above, the most frequently occurring values are 2 and 4, and each occurs 3 times. Thus mostCommon would "return" either 2 or 4 as the most common element, and 3 as the number of times it appears.

Explanation / Answer

Answer a)
=========

int numOccurences(int a[], int size, int value){
int count = 0;
for(int i = 0; i < size; i++){
if(a[i] == value) //current element is same as value
count++;
}

return count;
}

==========

Answer b)
//returns the most commonly occuring value and its count in the result[] array
void mostCommon(int a[], int size, int result[]){

//assume the first element is the most common
int mostComNum = a[0];
int mostComOccurs = numOccurences(a, size, a[0]);

//check with rest of the array elements
for(int i = 1; i < size; i++){
if(a[i] == mostComNum) //already processed this number
continue;

if(numOccurences(a, size, a[i]) > mostComOccurs){
mostComNum = a[i];
mostComOccurs = numOccurences(a, size, a[i]);
}
}

//update the array of return values by storing commonly occuring number and its no. of occurences
result[0] = mostComNum;
result[1] = mostComOccurs;
}


===========

please do rate the answer if helpful. Thank you

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote