Write a Program in C++ That Compute The Sum of The Elements of An Array

// Compute the sum of the elements of an array.
#include <iostream>
#include <array>
using namespace std;

int main()
{
   const size_t arraySize = 4; // specifies size of array
   array< int, arraySize > a = { 10, 20, 30, 40 };
   int total = 0;

   // sum contents of array a
   for ( size_t i = 0; i < a.size(); ++i )
      total += a[ i ];
   
   cout << "Total of array elements: " << total << endl;
} // end main

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...