Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find average of input to vector c++

I am trying to write a program where the user inputs as many numbers as they want and then the program returns the average of the numbers. So far the program only outputs the last number entered.

#include <vector>
#include <iostream>
#include <numeric>


using namespace std;

int main()
{  
    vector<float> v;
    int input;

    cout << " Please enter numbers you want to find the mean of:" <<endl;
    while (cin >> input);
    v.push_back(input);

float average = accumulate( v.begin(), v.end(), 0.0/ v.size());
cout << "The average is" << average << endl;

return 0;  

}
like image 978
alfiej12 Avatar asked Feb 18 '15 01:02

alfiej12


People also ask

How do you calculate average in C?

scanf("%f", &num[i]); And, the sum of each entered element is computed. sum += num[i]; Once the for loop is completed, the average is calculated and printed on the screen.


2 Answers

In C++17 and higher, you should prefer std::reduce over std::accumulate. Both calculate the sum, but std::reduce has higher numerical stability and, less importantly, is potentially faster because it might be parallelized.

#include <vector>
#include <numeric>
#include <iostream>

float average(std::vector<float> const& v){
    if(v.empty()){
        return 0;
    }

    auto const count = static_cast<float>(v.size());
    return std::reduce(v.begin(), v.end()) / count;
}

int main(){
    std::vector<float> v{8, 4, 2, 7, 5};
    auto const a = average(v);
    std::cout << "average: " << a << "\n";
}

Note that static_cast suppresses a compile warning here, that std::size_t (the type of v.size()) with 8 byte size can theoretically express values for which the precision of a 4 byte float is not sufficient.


Your code has two bug. First get rid of semi-colon after while

while (cin >> input); // <-- bug
v.push_back(input);   // not in loop

// fixed loop
while (cin >> input){
    v.push_back(input);
}

Second your average calculation is wrong. The third argument of std::accumulate is the initial value of the sum, which is 0 by default, so you don't need to pass it.

Your actual bug is, that you divided the 0 by the element count. What you wanted, was to divide the value sum by the element count.

float average = accumulate(v.begin(), v.end(), 0.0 / v.size());
//                                             ^^^^^^^^^^^^^^

// fixed code
float average = accumulate(v.begin(), v.end(), 0.0) / v.size();

// without explicit init value
float average = accumulate(v.begin(), v.end()) / v.size();

// C++17 version with higher numerical stability
float average = reduce(v.begin(), v.end()) / v.size();

Also, the element of container data type should match the container type or if your really want to read integers and add to a float vector, you should use static_cast. The bulky syntax not only suppresses any compiler warnings, but also makes it clear that this is not an oversight, but an intentional difference in data types.

vector<float> v; // float vector
int input;       // integer input value
// ...
v.push_back(input); // add int value to float container

// an intentional type conversion should catch the eye
v.push_back(static_cast<float>(input));
like image 189
P0W Avatar answered Sep 18 '22 11:09

P0W


There are quite a few bugs in your code, have you actually debugged it? here's a working version:

#include <vector>                                                               
#include <iostream>                                                             
#include <numeric>                                                              


using namespace std;                                                            

int main()                                                                      
{                                                                               
    vector<float> v;                                                            
    float input;                                                                

    cout << " Please enter numbers you want to find the mean of:" <<endl;       
    while (cin >> input)                                                        
        v.push_back(input);                                                     

    float average = accumulate( v.begin(), v.end(), 0.0)/v.size();              
    cout << "The average is" << average << endl;                                

    return 0;                                                                   

}    
like image 35
swang Avatar answered Sep 18 '22 11:09

swang