how to exit upon entering negative number

By Robbie

This program asks for 100 inputs and sorts inputs in ascending order also calculates the average of all the inputs. I am wondering how do i make it to exit upon entering negative number instead of asking for 100 inputs, and show average .

#include <iostream>
#include <iomanip>
using namespace std;
bool sortArray(double* anArray, int size);
double averageArray(double* anyArray, int size);
int main()
{
double* anArray;
double total = 0.0;
double average;
signed scores=100;
int count;
cout << "Enter a series of positive numbers Entering a negative number will stop input ";
anArray = new double[scores];
cout << "Enter test scores: ";
for (count = 0; count < scores; count++)
{
    cout << "Test Score " << count + 1 << ": ";
    cin >> anArray[count];
}
sortArray(anArray, scores);
for (count = 0; count < scores; count++)
{
    cout << anArray[count] << "n";
}
cout << averageArray(anArray, scores);
system("pause");
return 0;
}
bool sortArray(double* anArray, int size)
{
bool sort = false;
for (int i = 0; i < size; i++)
{
    for (int j = 0; j < size - 1; j++)
    {
        if (anArray[j] > anArray[j + 1])
        {
            sort = true;
            double temp = anArray[j];
            anArray[j] = anArray[j + 1];
            anArray[j + 1] = temp;
        }
    }
}
return sort;
}
double averageArray(double* anyArray, int size)
{
double count = 0.0;
for (int i = 0; i < size; i++)
{
    count += anyArray[i];
}
return (count / (double) size);
  }

Source: Stack Overflow

    

Share it with your friends!

    Fatal error: Uncaught Exception: 12: REST API is deprecated for versions v2.1 and higher (12) thrown in /home/content/19/9652219/html/wp-content/plugins/seo-facebook-comments/facebook/base_facebook.php on line 1273