Wow, I am having huge problems passing values between functions and wrapping my head around pointers.
For some reason when I tried to return values from functions they would not pass and when I tried putting pointers in the place, passing them kept giving me compiler errors. Now I have no errors but my values are not passing correctly.
Any help I could get with this code would be great!
#include<iostream>
#include<iomanip>
using namespace std;
//function prototypes
void displayScores(int, int[]);
void sortString(int, int[]);
double averageScore(int, int, int[]);
int dropLowest(int, int, int[]);
int main()
{
int count = 0,
arraySize = 0,
*array,
lowest = 0,
*lowPtr = &lowest;
double average = 0.0;
cout << "How many test scores would you like to enter? ";
cin >> arraySize;
//alocate memory
array = new int[arraySize];
//collect data
for (count = 0; count < arraySize; count++)
{
cout << "Test " << (count + 1) << " score : ";
cin >> array[count];
//verify input
if (array[count] < 0)
{
cout << "Please enter a score greater or equal to zero!\n";
cout << "Enter the " << (count + 1) << " test score : ";
cin >> array[count];
}
}
//sort the array
sortString(arraySize, array);
//display scores
displayScores(arraySize, array);
//droplowest score
dropLowest(arraySize, *lowPtr, array);
// display lowest
cout << lowPtr << "Is the lowest score\n" ;
//calculate average
averageScore(arraySize, *lowPtr, array);
//display scores
displayScores(arraySize, array);
cout << "The average is " << average << endl;
//delete memory
delete [] array;
array = 0;
return 0;
}
void displayScores(int arraySize, int array[])
{
for (int i = 0; i < arraySize; i++)
{
cout << array[i] << endl;
}
}
void sortString(int arraySize, int array[])
{
int swapScore = 0,
highest = 0;
for (int startIndex = 0; startIndex < (arraySize - 1); startIndex++)
{
highest = array[startIndex];
swapScore = array[startIndex];
for (int count = startIndex + 1; count < arraySize; count++)
{
if (array[count] > highest)
{
highest = array[count];
swapScore = array[startIndex];
array[count] = swapScore;
array[startIndex] = highest;
}
}
}
}
double averageScore(int arraySize, int lowPtr, int array[])
{
double total = 0;
double average = 0.0;
for (int i = 0; i < arraySize; i++)
{
total += array[i];
}
total -= lowPtr;
average = (total / (arraySize - 1));
cout << fixed << showpoint << setprecision(2);
cout << "the total is " << total << endl;
cout << "The average is " << average << endl;
return average;
}
int dropLowest(int arraySize, int lowPtr, int array[])
{
lowPtr = array[0];
for (int i = 1; i < arraySize; i++)
{
if (array[i] < lowPtr)
lowPtr = array[i];
}
return lowPtr;
}
Thanks guys!
Jay