Mode function trouble

so I ran this program by my professor and neither of us can figure out whats wrong with it.

It goes through the sort and average functions just fine but when it gets to the mode it always just spits out the first number in the array.

It also keeps spitting out a fatal error every time it try's to compile all the data


#include "stdafx.h"
#include <iostream>

using namespace std;

void sort(double *movieArray, int arraySize)
{
bool swap;
int i;
double temp;

do
{
swap = false;
for (i = 0; i < arraySize; i++)
{

if (movieArray[i] > movieArray[i+1])
{
temp = movieArray[i];
movieArray[i] = movieArray[i+1];
movieArray[i+1] = temp;
swap = true;
}
}
}while (swap);

for (i = 0; i < arraySize; i++)
{
cout << "Score " << (i + 1) << ": " << movieArray[i+1] << endl;
}
}

void averageScore(double *movieArray, int arraySize)
{
double storage = 0, average;
int i;

for (i = 0; i < arraySize; i++)
{
//cout << "debug a" << endl;
storage = storage + movieArray[i+1];
}
//cout << "debug b" << endl;
average = storage / arraySize;

cout << "\nThe average number of movies seen is: " << average << endl;
}

void findMode (double *movieArray, int arraySize)
{
int count = 0, countTemp = 0; // initialize the mode counter to contain all zeroes
int largestCount = 0, mode = 0;
int i, j;

cout << "debug a" << endl;

for (i = 0; i < arraySize - 1; i++)
{ cout << "debug i" << endl;
for (j = i+1; j < arraySize; j++)
{ cout << "debug j" << endl;

if (movieArray[i] == movieArray[j])
{
countTemp++; // increment corresponding count
//cout << count[i] << endl;
}
if ( largestCount < countTemp)
{
largestCount = countTemp;
mode = i;
}
countTemp = 0;
}
}
cout << "Exit from the loop" << endl;
// mode = movieArray[0];
// largestCount = count[0]; // find largest count, and hence the mode
//for (i = 0; i < arraySize; i++)
//{
// //if (count[i] > largestCount)
// //{
// // largestCount = count[i];
// // mode = movieArray[i];
// //}
//}
cout << "The Mode is: " << movieArray[mode] << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
double *movie, average;
int numStudents, count;

cout << "How man students were surveyed?: ";
cin >> numStudents;

movie = new double[numStudents];

cout << "How many movies did this student watch?:" << endl;
for (count = 0; count < numStudents; count++)
{
cout << "Student " << (count + 1) << ": ";
cin >> movie[count];
if (movie[count] < 0)
{
movie[count] *= -1;
}
}

sort (movie, numStudents);
averageScore (movie, numStudents);
findMode (movie, numStudents);


delete [] movie;

return 0;
}


Please help
Firstly, let's take a look at a simple test case. Take 1 student through your sort routine. The array element movieArray[i+1] is probably not what you're looking for, as that array element does not exist.
Topic archived. No new replies allowed.