I am trying to average the negative numbers in the list and average the positive numbers in the list as well as the average of the whole list. This is what i have so far I know it is not much but i'm not sure what my next step should be...
//This will read in a list of n values, where n is not known ahead of time. The number of values read into the array will be saved in n.
vector<int> readList()
{
std::vector<int> result;
ifstream inFile;
inFile.open("setA.txt");
for (int x; inFile >> x; )
{
result.push_back(x);
}
return result;
}
//array is a one-dimensional array of integers and n is the number of elements in that array that contain valid data values. Both of these are input parameters to the function. The function must calculate 1) the average of the n integers in array, storing the result in ave; 2) the average of the positive numbers (> 0), storing the result in avePos, and 3) the average of the negative numbers (< 0), storing the result in aveNeg.
void avgs (std::vector<int> &array, int &ave, int &avePos, int &aveNeg)
{
int sum = 0, pos_sum = 0, neg_sum = 0, pos_count = 0, neg_count = 0;
for (auto i : array)
{
sum += i;
if (i > 0) { pos_sum += i; ++pos_count; }
if (i < 0) { neg_sum += i; ++neg_count; }
}
if(pos_sum) avePos = pos_sum / pos_count;
if(neg_sum) aveNeg = neg_sum / neg_count;
}