Finding average of inputs using vector

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;
}
Last edited on
I'm particularly having trouble checking whether or not the average is a whole integer (it's a double)

if this is an absolute condition or you KNOW the values are small enough to always fit clean as a double, you can do the fairly simple check of
if( (int)(doublevar) == intvar)
but that is not a great approach general purpose, its usually better to check with something a little smarter
eg
if( abs(doublevar - intvar) < 0.00000001)
or something like that. You can get the exact values if you want to research it, or wing it, since its homework, is probably good enough?
Last edited on
yes good call, this fixed it
if (fmod(mean,1) >= 0.001)
after rewriting almost from scratch I've perfected it
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
#include <iostream>
#include<vector>
#include<cmath>
using namespace std;

int main()
{
    double mean;
    double input;
    double sum;

    vector<double>numbers;
    vector<double>average_vector;

    do {
        cout<<"Please enter an integer (2 or greater, -1 to finish):";
        cin>>input;
        if (input >= 2)
        {
            numbers.push_back(input);
            sum = sum + input;
            mean = sum/numbers.size();
            average_vector.push_back(mean);
            cout << "The new arithmetic mean is " << mean <<"\n";

            if (fmod(mean,1) >= 0.001)
            {
                cout<<"Sorry, your input was rejected and will be removed! \n\n";
                numbers.pop_back();
                average_vector.pop_back();
                sum = sum - input;

            } else{ cout<< "Your input has been accepted!\n"<<endl;}
        }

        else if (input < 2 && input != -1)
        {
            cout << "\nThat is not a valid number! Please try again." << endl;
        }

    } while (input!= -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) : "<<average_vector[j]<<"\n";
    }
    return 0;
}
Last edited on
here's another program I wrote tonight that correlates with these concepts

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
#include <iostream>
#include<vector>
#include<iomanip>
using namespace std;

int main()
{
    vector<int>Home_score;
    vector<int>Away_score;
    int quarter =1;
    int width = 5;
    int homescore =0;
    int awayscore =0;
    int awaytotal=0;
    int hometotal=0;

        while(quarter<=4 || hometotal == awaytotal)
        {
            if(quarter>4 && hometotal == awaytotal)
            {
                cout<<"It's a tie! Let's play another quarter.\n";
            }
            cout << quarter << "Q away team score: \n";
            cout<<"Please enter a number (0 or greater): ";
            cin >> awayscore;
            while (awayscore <= 0)
            {
                cout<<"That is not a valid number! Please try again.\n";
                cout<<"Please enter a number (0 or greater): ";
                cin>>awayscore;
            }
            Away_score.push_back(awayscore);
            awaytotal = awaytotal + awayscore;
            cout << quarter << "Q home team score: \n";
            cout<<"Please enter a number (0 or greater): ";
            cin >> homescore;
            while (homescore <= 0)
            {
                cout<<"That is not a valid number! Please try again.\n";
                cout<<"Please enter a number (0 or greater): ";
                cin>>homescore;
            }
            Home_score.push_back(homescore);
            hometotal = hometotal + homescore;
            quarter++;
        }
        cout<<endl;
        int counter;
        counter = 1;
        cout<<setw(width)<<"";
        do
        {
            cout<<setw(width-1)<<counter<<"Q";
            counter++;
        }while(counter < quarter);
    cout<<setw(width+2)<<"TOTAL\n";
    cout<<setw(width)<<"AWAY";
    for (int i = 0; i < Away_score.size(); i++)
    {
        cout << setw(width)<<Away_score[ i ];
    }
    cout<<setw(width+1)<<awaytotal<<"\n";
    cout<<setw(width)<<"HOME";
    for (int j = 0; j < Home_score.size(); j++)
    {
        cout << setw(width)<<Home_score[ j ];
    }
    cout<<setw(width+1)<<hometotal<<"\n";
    return 0;
}
Last edited on
Topic archived. No new replies allowed.