//This program asks the user for the number of tests, creates an array based on that response
//the user enters his scores and then the program lists the scores in ascending order, finds the average
//and then finds the average of the scores with the lowest one dropped
#include <iostream>
#include <iomanip>
using namespace std;

void arrSelectSort (int *[], int); //to sort the scores in ascending order
void showArray(int [], int);//show the test scores in ascending order
void getAverage (int [], int); //to get the average score without dropping the score
void dropScore (int [], int); //to get the average score while dropping the lowest score

int main()
{
int numScores;//the variable for the number of test scores to add!
int *scores;// to dynamically allocate and erray


cout << "How many test scores do you have to enter?" <<endl;
cin >> numScores; // Asking and storing the number of variables

scores = new int[numScores];// Dynamically create an array large enough to hold all of the test scores
////////////////////////////////////////this is what she says is wrong
int *arrPtr[numScores];
///she says you can't use a variable to define the size of an array
//
for(int count= 0; count < numScores; count++)//Inputting the number of test scores
{
cout << "Please enter the score for test #" << (count + 1) <<endl;
cin >> scores[count];
arrPtr[count] = &scores[count];
}


arrSelectSort(arrPtr, numScores);//Sort the scores using a array of pointers
showArray(scores, numScores);//Show the scores in ascending order
getAverage(scores, numScores);//Taking the average
dropScore(scores, numScores);//The average with the lowest score dropped

delete [] scores; //gotta free the memory
scores = 0;
return 0;
}

void arrSelectSort(int *arr[], int size)//num of elements of numScores
{
int startScan, minIndex, index;//putting the test scores in order
int *minElem;

for(startScan= 0; startScan < (size - 1); startScan++)//this is basically out of the book
{
minIndex = startScan;
minElem = arr[startScan];
for(int index = startScan + 1; index < size; index++)
{
if (*(arr[index]) < *(arr[minIndex]))
{
minIndex = index;
}
}
if(startScan!=minIndex)
{
int swap = *(arr[startScan]);
*(arr[startScan]) = *(arr[minIndex]);
*(arr[minIndex]) = swap;
}

}
}

void showArray(int arr[], int size)//Display the array in ascending order
{
cout << "Test Scores"<<endl;
for(int counter = 0; counter < size; counter++)
{
cout << "Test # " << (counter + 1);
cout << "\t\t";
cout << arr[counter] << " ";
cout <<endl;
}
}
void getAverage(int arr[], int size)//Average test scores by adding all the values
//to one variable then dividing it by the number of scores
{
double sum = 0;//almost forgot to make this a double so we can have decimals!
double averagescore = 0;

for(int count = 0; count < size; count++)
{
sum += arr[count];
}
averagescore = sum*(1.0)/size;
cout <<"Average score:\t\t"<<averagescore<<endl;

}
void dropScore(int arr[], int size)// the same as the function above
//except we skip the first value in the array aka the lowest score
{
double sum = 0;
double average;

for(int count = 1; count < size; count++)
{
sum += arr[count];
}
average = sum*(1.0)/(size-1);
cout <<"Average with drop:\t"<< average <<endl;

}

Can someone please help me figure out why this won't work in more sensitive compilers? Works fine in the new dev c++? Thank you.

Recommended Answers

All 2 Replies

You've answered your own question in your code:

//////////////////this is what she says is wrong
    int *arrPtr[numScores];

But you already did it the right way with "scores": scores = new int[numScores];// Dynamically create an array large enough to hold all of the test scores So change the line to something like: int *arrPtr = new int[numScores]; and you should be fine. Don't forget to add a delete[] for this variable!

I don't have a compiler I can use at this moment, but I think there are going to be a few new errors with this code. With this for example: void arrSelectSort(int *arr[], int size) I think you might want to rethink what you're doing with pointers etc...

and what happens if there is only one score?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.