C++:Sum an Array

From Progzoo
Revision as of 07:21, 20 November 2009 by Spoon! (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

You can add the numbers in the array [6,7,29] using a loop.

[Font] [Default] [Show] [Resize] [History] [Profile]

Alternatively, you can use the library function accumulate():

#include <iostream>
#include <numeric>
using namespace std;
int main(){
  int a[] = {6,7,29};
  cout << accumulate(a, a+3, 0) << endl;
  return 0;