c++ - Adding All Array Elements Together -


i wanted know how add of elements of float array , make sum float average;. have use loop or there way add element 0 1 2 3, etc.?

you may use loop, or can use std::accumulate.

#include <iostream> #include <numeric>  int main() {   float arr[17] = { 1, 2, 3, };    //sum array   const float sum = std::accumulate(arr, arr+17, 0.0 );     std::cout << "sum: " << sum << "\n";   std::cout << "average: " << sum/17 << "\n"; } 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -