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

Write a function in C++ to count the number of occurences in an array, Explain i

ID: 3566361 • Letter: W

Question

Write a function in C++ to count the number of occurences in an array, Explain in detail your code to receive full credit.

Let say you have these two arrays :
L1 = [1, 3, 6, 6, 6, 6, 7, 8, 11, 11, 14, 15, 20, 20]

L2 = [10, 20, 20, 20, 30]

and you want to know the number of occurences, for example :

L1.NumberOfOccurrences(6) will return 4

L2.NumberOfOccurrences(20) will return 3

--------------------

int NumberOfOccurrences(genType item)

- Function: Returns the number of times item exists in the list.

- Precondition: List has been initialized, and Item has been initialized

- Postcondition: Function value = 0 if item does not exist in the list, else it returns

the number of times item occurred in the list.

Explanation / Answer

int NumberOfOccurrences(genType item)
{
   //my array is called L, and it's size is n

int count=0; // stores number of times we have seen the value item in the list
  
   int i;
   for(i=0;i<n;i++)   // loop through all items in the list
   {
       if(L[i]==item)
           count++;   //increase count every time you see item
   }
return count; //returns the number of times we saw item, and if item not present then clearly returns 0

}

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