Good evening everyone, I was trying to write a function that returns the number that appears most frequently. Although the code seems to work, it is not working as I intend it to work. What I'm trying to do is to have the user enter a number students, then the program would ask how many movies each student watched in a month. The program then calculates the median, the mode, and the average. here is what I have for my mode function.
int modeFunction(int * prt, int size)
{
int * frequency;
int * prtCopy;
int largest;
int mode = 0;
int often;
int count;
//finding the largest entry
largest = *prt;
for(count = 0; count < size; count++)
{
if(*(prt + count) > largest)
largest = *(prt + count);
}
//Allocating memory for the new array using the largest
frequency = new int[largest + 1];
prtCopy = new int[largest + 1];
//intializing the frequnecy and prtCopy array
for(count = 0; count <= largest + 1; count++)
{
*(frequency + count) = 0;
*(prtCopy + count) = 0;
}
//Copying values from prt to prtCopy
for(count = 0; count < size; count++)
*(prtCopy + count) = *(prt + count);
//getting the most frequent number
for(count = 0; count <= largest + 1; count++)
{
frequency[prtCopy[count]]++;
}
The code compiles but never gives me the correct results. For example if I enter number of students " 3 " and then student 1: 33, student 2: 12 and student 3 : 16 the function returns 0. I'm a beginner by the way. Thank you for any help