I can't figure out what I did wrong here. I'm pretty sure the sortArray function is correct because my teacher gave me that, but my function to calculate the average is not working? Help appreciated!
double calcQuizAverage (int quizArray[], int numOfQuizzes)
{
int tempArray[12];
int i = 0;
int avg, sum;
for (i = 0; i < 12; i++)
tempArray[i] = quizArray[i];
sortArray(tempArray,12);
sum += tempArray[i];
if (numOfQuizzes> 2)
avg = (sum -(tempArray[0] + tempArray[1])) / (10 * (numOfQuizzes - 2)) * 100;
else
avg = sum / (10 * numOfQuizzes) * 100;
return avg;
}
void sortArray(int array[], int size)
{
int i, j, min;
int temp; // Temporary storage - same data type as array elements
// Loop through unsorted elements of the array
for (i = 0; i < size - 1; i++)
{
// Initially assume first unsorted element is smallest
min = i;
// Search other unsorted elements for a smaller value
for (j = i+1; j < size; j++)
{
if (array[j] < array[min])
min = j;
}
// Swap smallest element with first unsorted element
temp = array[i];
array[i] = array[min];
array[min] = temp;
}
}
Why are you sorting your array inside the calcQuizAverage() function?
Why the magic number 12, wouldn't numOfQuizzes be a better choice?
Why the "temporary" array? Why not just iterate through the array you've passed into the function and sum the elements?
It doesn't appear that you know that when a control statement is without braces only one line is part of that statement. I suggest you always use braces with your control statements, even when not technically required, until you are more familiar with the language.
your "sum" only gets one number : tempArray[i] or tempArray[0].
you need a loop to get all the numbers from your array.
like jlb says you don't need to sort your array.
i'm not sure of what your doing in the lines 15 to 20,
to calculate an average, you only need the complete sum and the number of quizz
example: 66+87+70+87 (result of the quizz)= 310(sum) / 4(number of quizz) = 77.5 (the average)
avg should not be an int, it will not gives you the decimals.