The question is to write a program that dynamically allocates an array large enough to hold a user defined number of test scores. Once all the scores are entered the array should be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score. The program should display the sorted list of scores and averages with appropriate heading. Use pointer notation rather than array notation whenever possible.
input validation do not accept negative test scores.

This is what i have written.

``````#include <iostream>
#include <iomanip>
using namespace std;

void arrSelectSort(float *[], int);
void showArrPtr(float *[], int);
void showAverage(float , int);

int main()
{
float *scores,				//To dynamically allocate an array
total=0.0,			//Accumulator
average;				//To hold the averge scores
int numScores;				//To hold the number of test scores

//Get the number of test scores.
cout << "How many test scores would you like to process?";
cin >> numScores;

//Dynamically allocate an array large enough to hold that many
//test scores
scores = new float[numScores];

//Get the test score for each test
cout << "Enter the test scores below.\n";
for (int count = 0; count < numScores; count++)
{
cout << "Test score #" << ( count + 1 ) << ": ";
cin >> scores[count];
while (scores <=0)
{
cout << "Zero or negative numbers not accepted.\n";
cout << "Test Score #" << (count + 1) << ": ";
cin >>scores[count];
}
}

//Calculate the total scores
for (int count = 0; count < numScores; count++)
{
total += scores[count];
}

//sort the elements of the array pointers
arrSelectSort ( scores, numScores );

//Will display them in sorted order.
cout << "The test scores in ascending order are: \n";
showArrPtr ( scores, numScores );

showAverage( total, numScores );

//Free memory.
delete [] scores;
scores = 0;

return 0;

}

void arrSelectSort(float *array[], int size)
{
int startScan, minIndex;
int *minElem;

for (startScan = 0; startScan < ( size - 1 ); startScan++)
{
minIndex = startScan;
minElem = array[startScan];
for (int index = startScan + 1; index < size; index++)
{
if ( *(array[index] ) < *minElem)
{
minElem = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minElem;
}
}

void showArrPtr(float *array, int size)
{
for (float count=0; count< size; count++)
cout << *(array[count]) << " ";
cout << endl;
}

void showAverage(float total, int numScores)
{
float average;

//Calculate the average
average = total / numScores;

//Display the results.
cout << fixed << showpoint << setprecision(2);
cout << "Average Score: " << average << endl;
}``````

I have fixed most of the errors and i believe almost everything is correct. but there are a couple of errors that tell me that cannot convert parameter 1 from float to *float[] for arrselectsort and showarrptr and more errors in other areas for the similar things like float to int. I'm sure that you can figure out that im not very good at writing code, and this chapter of pointers for some reason its confusing the hell out of me. So any pointers as to how to fix my program is greatly appreciated.

mvmalderen commented: In your previous thread I provided you with a link on how to correctly use code tags, now you're still not able to use them correctly, I wonder whether the [B]#[/B]-button is even too difficult for you. -3

You're over-thinking the problem.

``````void arrSelectSort(float *, int);

int main()
{
float *scores,  //To dynamically allocate an array
int numScores;  //To hold the number of test scores

//sort the elements of the array pointers
arrSelectSort ( scores, numScores );

return 0;
}

void arrSelectSort(float *array, int size) …``````

pltndragon,
In your program, function arguments - array of float pointers is unnecessary. Use float pointer.

``````void arrSelectSort(float *, int);
void showArrPtr(float *, int);``````

Array index must be unsigned int.

``````void showArrPtr(float *array, int size){
for (float count=0; count< size; count++)
cout << *(array[count]) …``````

## All 6 Replies

You're over-thinking the problem.

``````void arrSelectSort(float *, int);

int main()
{
float *scores,  //To dynamically allocate an array
int numScores;  //To hold the number of test scores

//sort the elements of the array pointers
arrSelectSort ( scores, numScores );

return 0;
}

void arrSelectSort(float *array, int size)
{
// do stuff
}``````

The declaration of your parameter types just matches the declaration of the variables you intend to pass.

Further, if you used array in main, then you can just as easily use array in the function as well.

I have made some changes, i think im getting closer to the answer. i didn't really understand what the other person ment by array[i] or for me to try and move what the program is supposed to do to voidarrSelectSort, i tried it but i ended up confusing myself and with a lot more problems. So this is what i have come up with so far. The couple of errors that i get are expected constant expression, cannot allocate constant of size zero, arrPtr unknow size and cannot convert parameter 2 from `int*` to int, I thought that by asking the size first and then inputing the array, then i would be ok in defining it later for arrPtr, But it didn't work, please advice how to repair or did i go from ok to worse.

``````#include <iostream>
using namespace std;

//Function prototype
int *arrAllocator(int);
void arrSelectSort(int *[], int);
void showArray(int [], int);
void showArrPtr(int *[], int);

int main()
{
int *test; //To point to the numbers
int count;// Loop counter

cout << "\nEnter the number of test you have: " << endl;

//Get an array of numbers chosen by user.
if (test == NULL)
return 0;

//Get number values from user.
cout << "Enter the grade number recieved on the test below.\n";

for (count = 0; count < numGrades; count++)
{
cout << "Test #" << (count + 1) << ": ";
cin >> test[count];
while (test[count] < 0)
{
cout << "No negative numbers are allowed for test score.\n";
cout << "Test #" << (count +1) << ": ";
cin >> test[count];
}
}

//An array of pointers to int.

for (int count = 0; count < numGrades; count++)
arrPtr[count] = &test[count];

// Sort the elements of the array of the pointer.
arrSelectSort(arrPtr, test);

//Display the donations using the array of pointers.
cout << "The test scores sorted in ascending order are: \n";

return 0;
}

//The function returns a pointer to an array.

{
int *array;

//Invalid if zero or negative.
return NULL;

//Dynamically allocate the array.

//Return a pointer to the array
return array;
}

void arrSelectSort(int *array[], int size)
{
int startScan, minIndex;
int *minElem;

for (startScan = 0; startScan < (size -1); startScan++)
{
minIndex = startScan;
minElem = array[startScan];
for(int index = startScan + 1; index < size; index++)
{
if(*(array[index]) <*minElem)
{
minElem = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minElem;
}
}

void showArray(int array[], int size)
{
for (int count = 0; count < size; count++)
cout << array[count] << " ";
cout << endl;
}

void showArrPtr(int *array[], int size)
{
for (int count = 0; count < size; count++)
cout << *(array[count]) << " ";
cout << endl;
}
``````

pltndragon,
In your program, function arguments - array of float pointers is unnecessary. Use float pointer.

``````void arrSelectSort(float *, int);
void showArrPtr(float *, int);``````

Array index must be unsigned int.

``````void showArrPtr(float *array, int size){
for (float count=0; count< size; count++)
cout << *(array[count]) << " ";
cout << endl;
}``````

``````#include <iostream>
#include <iomanip>
using namespace std;

void arrSelectSort(float *, int);
void showArrPtr(float *, int);
void showAverage(float, int);

int main()
{
float *scores,				//To dynamically allocate an array
total=0.0,			//Accumulator
average;				//To hold the averge scores
int numScores;				//To hold the number of test scores
//Get the number of test scores.
cout << "How many test scores would you like to process?";
cin >> numScores;

//Dynamically allocate an array large enough to hold that many
//test scores
scores = new float[numScores];
if(scores==NULL)
return 0;

//Get the test score for each test
cout << "Enter the test scores below.\n";
for (int count = 0; count < numScores; count++)
{
cout << "Test score #" << ( count + 1 ) << ": ";
cin >> scores[count];
while (scores <=0)
{
cout << "Zero or negative numbers not accepted.\n";
cout << "Test Score #" << (count + 1) << ": ";
cin >>scores[count];
}
}

//Calculate the total scores
for (int count = 0; count < numScores; count++)
{
total += scores[count];
}

//sort the elements of the array pointers
arrSelectSort ( scores, numScores );

//Will display them in sorted order.
cout << "The test scores in ascending order are: \n";
showArrPtr ( scores, numScores );

showAverage( total, numScores );

//Free memory.
delete [] scores;
return 0;
}
void arrSelectSort(float *array, int size)
{
int startScan, minIndex;
float  minElem;

for (startScan = 0; startScan < ( size - 1 ); startScan++)
{
minIndex = startScan;
minElem = array[startScan];
for (int index = startScan + 1; index < size; index++)
{
if ( array[index]  < minElem)
{
minElem = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minElem;
}
}

void showArrPtr(float *array, int size)
{
for (int count=0; count< size; count++)
cout << array[count] << " ";
cout << endl;
}

void showAverage(float total, int numScores)
{
float average;

//Calculate the average
average = total / numScores;

//Display the results.
cout << fixed << showpoint << setprecision(2);
cout << "Average Score: " << average << endl;
}``````

Wow i didn't know that removing all those brackets would get rid of so many errors. In my book it has the *array[] together so i thought i was doing it correctly. The only thing i changed was defining average under int main, i deleted it because i think i had also done it further down, and that fixed all those problems.

thank you adatapost, you make this seam really easy.

Ok, so now my question is why doesn't the while statement make sure no negative numbers are allowed?

Be a part of the DaniWeb community

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