How would you go about resolving this output value?

Solution 1:

I try to start my answers with a brief code review:

#include <iostream>
#include <iomanip>

using namespace std;  // Bad practice; avoid

void showArray(double* array, int size);
double averageArray(double* array, int size);
void orderArray(double* array, int size);

int main()
{
    double* scores = nullptr;
    int counter;
    double numberOfScores;

    cout << "\nHow many test scores will you enter? ";
    cin >> numberOfScores;

    // This is not input validation, I can enter two consecutive bad values,
    // and the second one will be accepted.
    if (numberOfScores < 0) {
        // Weird formatting, this blank line
        cout << "The number cannot be negative.\n"
             << "Enter another number: ";
        cin >> numberOfScores;
    }

    // The homework, as presented, doesn't say you have to treat 0 differently.
    if (numberOfScores == 0) {

        cout << "You must enter a number greater than zero.\n"
             << "Enter another number: ";
        cin >> numberOfScores;
    }

    scores = new double[numberOfScores];

    // Declare your loop counter in the loop
    for (counter = 0; counter < numberOfScores; counter++) {

        cout << "Enter test score " << (counter + 1) << ": ";
        cin >> *(scores + counter);
        if (*(scores + counter) < 0) {

            cout << "Negative scores are not allowed. " << endl
                 << "Enter another score for this test : ";
            cin >> *(scores + counter);
        }
    }
    orderArray(scores, counter);  // Why not use numberOfScores?

    cout << "\nThe test scores in ascending order, and their average, are: " << endl
         << endl;
    cout << " Score" << endl;
    cout << " -----" << endl
         << endl;

    showArray(scores, counter);  // Same as above.

    cout << "\nAverage Score: "
         << " " << averageArray(scores, counter) << endl
         << endl;
    cout << "Press any key to continue...";

    delete[] scores;
    scores = nullptr;

    system("pause>0");  // Meh, I suppose if you're on VS
}

void orderArray(double* array, int size)
{
    int counterx;
    int minIndex;
    int minValue;  // Unnecessary, and also the culprit

    // This looks like selection sort
    for (counterx = 0; counterx < (size - 1); counterx++) {

        minIndex = counterx;
        minValue = *(array + counterx);

        for (int index = counterx + 1; index < size; index++) {

            if (*(array + index) < minValue) {

                minValue = *(array + index);
                minIndex = index;
            }
        }

        *(array + minIndex) = *(array + counterx);
        *(array + counterx) = minValue;
    }
}

double averageArray(double* array, int size)
{

    int x;
    double total{};

    for (x = 0; x < size; x++) {

        total += *(array + x);
    }

    double average = total / size;
    return average;
}

void showArray(double* array, int size)
{

    for (int i = 0; i < size; i++) {

        cout << "     " << *(array + i) << endl;
    }
}

When you are sorting your array, you keep track of the minValue as an int and not a double. That's why your average of the sample input is incorrect. 45.1 is truncated to 45 for your calculations. You don't need to keep track of the minValue at all. Knowing where the minimum is, and where it needs to go is sufficient.

But as I pointed out, there are some other serious problems with your code, namely, your [lack of] input validation. Currently, if I enter two consecutive bad numbers, the second one will be accepted no matter what. You need a loop that will not exit until a good value is entered. It appears that you are allowed to assume that it's always a number at least, and not frisbee or any other non-numeric value.

Below is an example of what your program could look like if your professor decides to teach you C++. It requires that you compile to the C++17 standard. I don't know what compiler you're using, but it appears to be Visual Studio Community. I'm not very familiar with that IDE, but I imagine it's easy enough to set in the project settings.

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

// Assumes a number is always entered
double positive_value_prompt(const std::string& prompt) {
  double num;

  std::cout << prompt;
  do {
    std::cin >> num;
    if (num <= 0) {
      std::cerr << "Value must be positive.\n";
    }
  } while (num <= 0);

  return num;
}

int main() {
  // Declare variables when you need them.
  double numberOfScores =
      positive_value_prompt("How many test scores will you enter? ");
  std::vector<double> scores;

  for (int counter = 0; counter < numberOfScores; counter++) {
    scores.push_back(positive_value_prompt("Enter test score: "));
  }

  std::sort(scores.begin(), scores.end());
  for (const auto& i : scores) {
    std::cout << i << ' ';
  }
  std::cout << '\n';
  std::cout << "\nAverage Score: "
            << std::reduce(
                   scores.begin(), scores.end(), 0.0,
                   [size = scores.size()](auto mean, const auto& val) mutable {
                     return mean += val / size;
                   })
            << '\n';
}

And here's an example of selection sort where you don't have to worry about the minimum value. It requires that you compile to C++20. You can see the code running here.

#include <iostream>
#include <random>
#include <vector>

void selection_sort(std::vector<int>& vec) {
  for (int i = 0; i < std::ssize(vec); ++i) {
    int minIdx = i;
    for (int j = i + 1; j < std::ssize(vec); ++j) {
      if (vec[j] < vec[minIdx]) {
        minIdx = j;
      }
    }
    int tmp = vec[i];
    vec[i] = vec[minIdx];
    vec[minIdx] = tmp;
  }
}

void print(const std::vector<int>& v) {
  for (const auto& i : v) {
    std::cout << i << ' ';
  }
  std::cout << '\n';
}

int main() {
  std::mt19937 prng(std::random_device{}());
  std::uniform_int_distribution<int> dist(1, 1000);
  std::vector<int> v;

  for (int i = 0; i < 10; ++i) {
    v.push_back(dist(prng));
  }
  print(v);

  selection_sort(v);
  print(v);
}

I opted not to give your code the 'light touch' treatment because than I would have done your homework for you, and that's just not something I do. However, the logic shown should still be able to guide you toward a working solution.