so the program needs to dynamically allocate an array large enough to hold a user=defined number of test scores. once all 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 headings. use pointer notation rather than array notation whenever possible.

the problem i am having is making it so that the program doesn't accept negative numbers for test scores.

errors -

Error C4716 'showAverage': must return a value ConsoleApplication9 c:\users\kenny\desktop\kenny_fepc1.cpp 85

Warning C4101 'average': unreferenced local variable ConsoleApplication9 c:\users\kenny\desktop\kenny_fepc1.cpp 12

please help fix. thank you.

here is the code.

#include <iostream>
#include <iomanip>

using namespace std;
void arrSelectSort(double *, int);
void showArrPtr(double *, int);
double showAverage(double, int);
int main()
{
    double *scores, //To dynamically allocate an array
        total = 0.0,
        average;
         //Accumulator
                        //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 double[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[count] <= 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);

    cout << "The test scores in ascending order are: \n";
    showArrPtr(scores, numScores);
    showAverage(total, numScores);

    delete[] scores;
    return 0;
}
void totalaverage(double *, int)
{
}
void arrSelectSort(double *array, int size)
{
    int startScan, minIndex;
    double  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(double *array, int size)
{
    for (int count = 0; count< size; count++)
        cout << array[count] << " ";
    cout << endl;
}
double showAverage(double total, int numScores)
{
    double average;
    //Calculate the average
    average = total / numScores;
    //Display the results.
    cout << fixed << showpoint << setprecision(2);
    cout << "Average Score: " << average << endl;

    system("pause");
}

Recommended Answers

All 2 Replies

I have a question. Why does this look exactly like that post from 7 years ago at https://www.daniweb.com/programming/software-development/threads/186020/help-using-dynamic-memory-allocation-and-pointers

Are you telling me you are still working this code after this many years?

Or is it something you found and need to fix it and claim it is yours? Don't do that. If it's an assignment a teacher or TA would find you lifted the code off the web in one google.

Not to give too much away but why not fix that broken function?

i did change it around since we were supposed to use doubles instead of ints or floats. i already submitted it. plus his had errors which i fixed. so its not completely my own work, but its not completely his either. and i was able to make the program work. it was because i had the array as a double name double instead of void.

#include <iostream>
#include <iomanip>

using namespace std;
void arraySelectSort(double *, int);
void showArrayPtr(double *, int);
void showAverage(double, int);
int main()
{
    double *scores, 
        total = 0.0,
        average = 0.0;


    int numScores;              

    cout << "Please enter the number of test scores you would like to use";
    cin >> numScores;

    scores = new double[numScores];
    if (scores == NULL)
        return 0;

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

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

    arraySelectSort(scores, numScores);

    cout << "The test scores in ascending order are: \n";
    showArrayPtr(scores, numScores);
    showAverage(total, numScores);

    delete[] scores;
    return 0;
}
void totalaverage(double *, int)
{
}
void arraySelectSort(double *array, int size)
{
    int startScan, minIndex;
    double  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 showArrayPtr(double *array, int size)
{
    for (int count = 0; count< size; count++)
        cout << array[count] << " ";
    cout << endl;
}
void showAverage(double total, int numScores)
{
    double average;

    average = total / numScores;

    cout << fixed << showpoint << setprecision(2);
    cout << "Average Score: " << average << endl;

    system("pause");
}
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.