Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C++ Exercises: Compute the sum of the elements of a given array of integers

C++ Basic Algorithm: Exercise-84 with Solution

Write a C++ program to compute the sum of the elements of a given array of integers.

Sample Solution:

C++ Code :

#include <iostream>
using namespace std;

int  test(int a1[])
          {
            return a1[0] + a1[1] + a1[2] + a1[3] + a1[4];
          } 
          
int main() 
 {  
  int nums[] = {10, 20, 30, 40, 50};	
  cout << test(nums) << endl; 
  int nums1[] = {10, 20, -30, -40, 50};	
  cout << test(nums1) << endl;
  return 0;    
}

Sample Output:

150
10

Pictorial Presentation:

C++ Basic Algorithm Exercises: Compute the sum of the elements of a given array of integers.

Flowchart:

Flowchart: Compute the sum of the elements of a given array of integers.

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C++ program to check two given arrays of integers of length 1 or more and return true if they have the same first element or they have the same last element.
Next: Write a C++ program to rotate the elements of a given array of integers (length 4 ) in left direction and return the new array.

What is the difficulty level of this exercise?