In a loop, ask the user for integers
If the user enters -1, end user input
Use an input validation loop to make sure the user input is 2 or greater Append the validated integer to the vector
Use the push_back() vector member function
Calculate and display the resulting arithmetic mean (average) of the vector WITH the new integer
Use the size() member function
If the mean is NOT a whole number:
Display a message rejecting the integer and remove the integer from the vector
Use the pop_back() member function
If the mean is a whole number:
Display a message accepting the integer and keep the integer in the vector
Once the user is done (user enters -1), calculate and display the mean of the first one element, first two
elements, and so on, until the mean of the entire vector is calculated and displayed
o Each mean should be a whole number
I'm trying to write a program for this, however I'm having a lot of trouble. I've tried it about 50 different ways. any suggestions?
I'm particularly having trouble checking whether or not the average is a whole integer (it's a double)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
#include <iostream>
#include<vector>
#include <iomanip>
#include<cmath>
using namespace std;
int main() {
double average = 0;
double sum =0;
double counter;
vector<double>input;
vector<double>sum1;
vector<double>avgsize;
vector<int>size;
vector<double>numbers;
vector<double>averagevector;
counter = 0;
do {
cout<<"Please enter an integer (2 or greater, -1 to finish):";
cin>>input[counter];
if (input[counter] >= 2)
{
cout<<showpoint<<setprecision(5);
numbers.push_back(input[counter]);
sum = sum + input[counter];
sum1.push_back(sum);
cout<<"counter is "<<counter<<" \n";
average = sum1[counter] / numbers.size();
averagevector.push_back(average);
cout << "The new arithmetic mean is " << averagevector[counter] << ".\n";
counter++;
if (fmod(averagevector[counter],1) != 0)
{
counter--;
cout<<"Sorry, your input was rejected and will be removed! \n";
numbers.pop_back();
averagevector.pop_back();
sum1.pop_back();
} else{ cout<< "Your input has been accepted! fmod"<<endl;}
}
else if (input[counter] < 2 && input[counter] != -1)
{
cout << "That is not a valid number! Please try again." << endl;
numbers.pop_back();
}
} while (input[counter] != -1);
int limit;
limit = numbers.size();
cout<<"\nVector Elements:\n";
for(int i=0; i < limit; ++i)
{
cout<<"numbers["<<i<<"] = "<<numbers[i]<<"\n";
}
cout<<"\n";
for(int j=0; j < limit; ++j)
{
cout<<"Mean of the first "<<(j + 1)<<" element(s) : "<<averagevector[j]<<"\n";
}
cout<<"goodbye"<<endl;
return 0;
}
|