Weighted Mean

#include <iostream> #include <vector> #include <iomanip> using namespace std; double weightedmean(vector<double> num, vector<double> weight) { double s1 = 0; double s2 = 0; for (int i = 0; i < num.size() ; i++) { s1 += num[i] * weight[i]; s2 += weight[i]; } return s1/s2; } int main() { int N; cin >> N; vector<double> num(N); vector<double> weight(N); for (int i = 0; i < num.size(); i++) { cin >> num[i]; } for (int i = 0; i < weight.size(); i++) { cin >> weight[i]; } cout << fixed << setprecision(1) << weightedmean(num, weight) << endl; return 0; }
Weighted Mean of N numbers at 1 decimal point precision.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.