9

How to Find the Sum of All Elements in an Array

 2 years ago
source link: https://www.makeuseof.com/find-sum-of-all-elements-in-array/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to Find the Sum of All Elements in an Array

By Yuvraj Chandra

Published 18 hours ago

Whether you're using JavaScript, Python, or C++, these programs definitely add up.

An array is a collection of elements stored at contiguous memory locations. It's the most used data structure in programming. In this article, you'll learn how to find the sum of all elements in an array using C++, Python, and JavaScript.

Problem Statement

You're given an array of numbers, and you need to calculate and print the sum of all elements in the given array.

Example 1: Let arr = [1, 2, 3, 4, 5]

Therefore, the sum of all elements of the array = 1 + 2 + 3 + 4 + 5 = 15.

Thus, the output is 15.

Example 2: Let arr = [34, 56, 10, -2, 5, 99]

Therefore, the sum of all elements of the array = 34 + 56 + 10 + (-2) + 5 + 99 = 202.

Thus, the output is 202.

Approach to Find the Sum of All Elements in an Array

You can find the sum of all elements in an array by following the approach below:

  1. Initialize a variable sum to store the total sum of all elements of the array.
  2. Traverse the array and add each element of the array with the sum variable.
  3. Finally, return the sum variable.

C++ Program to Find the Sum of All Elements in an Array

Below is the C++ program to find the sum of all elements in an array:

// C++ program to find the sum of elements in an array
#include <iostream>
using namespace std;
// Function to return the sum of elements in an array
int findSum(int arr[], int size)
{
int sum = 0;
for(int i=0; i<size; i++)
{
sum += arr[i];
}
return sum;
}

// Function to print the elements of the array
void printArray(int arr[], int size)
{
for(int i=0; i<size; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}

// Driver code
int main()
{
int arr1[] = {1, 2, 3, 4, 5};
int size1 = sizeof(arr1) / sizeof(arr1[0]);
cout << "Array 1:" << endl;
printArray(arr1, size1);
cout << "Sum of elements of the array: " << findSum(arr1, size1) << endl;
int arr2[] = {34, 56, 10, -2, 5, 99};
int size2 = sizeof(arr2) / sizeof(arr2[0]);
cout << "Array 2:" << endl;
printArray(arr2, size2);
cout << "Sum of elements of the array: " << findSum(arr2, size2) << endl;
int arr3[] = {-1, 50, -56, 43, 53, 356, -324};
int size3 = sizeof(arr3) / sizeof(arr3[0]);
cout << "Array 3:" << endl;
printArray(arr3, size3);
cout << "Sum of elements of the array: " << findSum(arr3, size3) << endl;
return 0;
}

Output:

Array 1:
1 2 3 4 5
Sum of elements of the array: 15
Array 2:
34 56 10 -2 5 99
Sum of elements of the array: 202
Array 3:
-1 50 -56 43 53 356 -324
Sum of elements of the array: 121

C++ Program Using STL to Find the Sum of All Elements in an Array

You can also use C++ STL to find the sum of all elements in an array.

// C++ program using STL to find the sum of elements in an array
#include <bits/stdc++.h>
using namespace std;
// Function to print the elements of the array
void printArray(int arr[], int size)
{
for(int i=0; i<size; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}

// Driver code
int main()
{
int arr1[] = {1, 2, 3, 4, 5};
int size1 = sizeof(arr1) / sizeof(arr1[0]);
cout << "Array 1:" << endl;
printArray(arr1, size1);
cout << "Sum of elements of the array: " << accumulate(arr1, arr1 + size1, 0) << endl;
int arr2[] = {34, 56, 10, -2, 5, 99};
int size2 = sizeof(arr2) / sizeof(arr2[0]);
cout << "Array 2:" << endl;
printArray(arr2, size2);
cout << "Sum of elements of the array: " << accumulate(arr2, arr2 + size2, 0) << endl;
int arr3[] = {-1, 50, -56, 43, 53, 356, -324};
int size3 = sizeof(arr3) / sizeof(arr3[0]);
cout << "Array 3:" << endl;
printArray(arr3, size3);
cout << "Sum of elements of the array: " << accumulate(arr3, arr3 + size3, 0) << endl;
return 0;
}

Related: A Beginner's Guide to the Standard Template Library in C++

Output:

Array 1:
1 2 3 4 5
Sum of elements of the array: 15
Array 2:
34 56 10 -2 5 99
Sum of elements of the array: 202
Array 3:
-1 50 -56 43 53 356 -324
Sum of elements of the array: 121

Python Program to Find the Sum of All Elements in an Array

Below is the Python program to find the sum of all elements in an array:

# Python program to find the sum of elements in an array
# Function to return the sum of elements in an array
def findSum(arr):
sum = 0
for element in arr:
sum += element
return sum
# Function to print the elements of the array
def printArray(arr):
for i in range(len(arr)):
print(arr[i] , end=' ')
print()
# Driver Code
arr1 = [1, 2, 3, 4, 5]
print("Array 1:")
printArray(arr1)
print("Sum of elements of the array:",findSum(arr1))
arr2 = [34, 56, 10, -2, 5, 99]
print("Array 2:")
printArray(arr2)
print("Sum of elements of the array:",findSum(arr2))
arr3 = [-1, 50, -56, 43, 53, 356, -324]
print("Array 3:")
printArray(arr3)
print("Sum of elements of the array:",findSum(arr3))

Output:

Array 1:
1 2 3 4 5
Sum of elements of the array: 15
Array 2:
34 56 10 -2 5 99
Sum of elements of the array: 202
Array 3:
-1 50 -56 43 53 356 -324
Sum of elements of the array: 121

Related: Python Project Ideas Suitable for Beginners

Python Program Using Built-in Function to Find the Sum of All Elements in an Array

You can also use Python's sum() function to find the sum of all elements in an array.

# Python program to find the sum of elements in an array
# Function to print the elements of the array
def printArray(arr):
for i in range(len(arr)):
print(arr[i] , end=' ')
print()
# Driver Code
arr1 = [1, 2, 3, 4, 5]
print("Array 1:")
printArray(arr1)
print("Sum of elements of the array:",sum(arr1))
arr2 = [34, 56, 10, -2, 5, 99]
print("Array 2:")
printArray(arr2)
print("Sum of elements of the array:",sum(arr2))
arr3 = [-1, 50, -56, 43, 53, 356, -324]
print("Array 3:")
printArray(arr3)
print("Sum of elements of the array:",sum(arr3))

Output:

Array 1:
1 2 3 4 5
Sum of elements of the array: 15
Array 2:
34 56 10 -2 5 99
Sum of elements of the array: 202
Array 3:
-1 50 -56 43 53 356 -324
Sum of elements of the array: 121

JavaScript Program to Find the Sum of All Elements in an Array

Below is the JavaScript program to find the sum of all elements in an array:

// JavaScript program to find the sum of elements in an array
// Function to return the sum of elements in an array
function findSum(arr, size)
{
let sum = 0;
for(let i=0; i<size; i++)
{
sum += arr[i];
}
return sum;
}

// Function to print the elements of the array
function printArray(arr, size)
{
for(let i=0; i<size; i++)
{
document.write(arr[i] + " ");
}
document.write("<br>");
}

// Driver code
const arr1 = [1, 2, 3, 4, 5]
size1 = arr1.length;
document.write("Array 1: <br>");
printArray(arr1, size1);
document.write("Sum of elements of the array: " + findSum(arr1, size1) + "<br>");
const arr2 = [34, 56, 10, -2, 5, 99]
size2 = arr2.length;
document.write("Array 2: <br>");
printArray(arr2, size2);
document.write("Sum of elements of the array: " + findSum(arr2, size2) + "<br>");
const arr3 = [-1, 50, -56, 43, 53, 356, -324]
size3 = arr3.length;
document.write("Array 3: <br>");
printArray(arr3, size3);
document.write("Sum of elements of the array: " + findSum(arr3, size3) + "<br>");

Output:

Array 1:
1 2 3 4 5
Sum of elements of the array: 15
Array 2:
34 56 10 -2 5 99
Sum of elements of the array: 202
Array 3:
-1 50 -56 43 53 356 -324
Sum of elements of the array: 121

Related: How to Build a Simple Calculator Using HTML, CSS, and JavaScript

JavaScript Program Using the reduce() Method to Find the Sum of All Elements in an Array

You can also use JavaScript's reduce() method to find the sum of all elements in an array.

// JavaScript program to find the sum of elements in an array
// Function to print the elements of the array
function printArray(arr, size)
{
for(let i=0; i<size; i++)
{
document.write(arr[i] + " ");
}
document.write("<br>");
}

// Driver code
const arr1 = [1, 2, 3, 4, 5]
size1 = arr1.length;
document.write("Array 1: <br>");
printArray(arr1, size1);
var sum1 = arr1.reduce(function(a, b) { return a + b; }, 0);
document.write("Sum of elements of the array: " + sum1 + "<br>");
const arr2 = [34, 56, 10, -2, 5, 99]
size2 = arr2.length;
document.write("Array 2: <br>");
printArray(arr2, size2);
var sum2 = arr2.reduce(function(a, b) { return a + b; }, 0);
document.write("Sum of elements of the array: " + sum2 + "<br>");
const arr3 = [-1, 50, -56, 43, 53, 356, -324]
size3 = arr3.length;
document.write("Array 3: <br>");
printArray(arr3, size3);
var sum3 = arr3.reduce(function(a, b) { return a + b; }, 0);
document.write("Sum of elements of the array: " + sum3 + "<br>");

Output:

Array 1:
1 2 3 4 5
Sum of elements of the array: 15
Array 2:
34 56 10 -2 5 99
Sum of elements of the array: 202
Array 3:
-1 50 -56 43 53 356 -324
Sum of elements of the array: 121

Want to Learn C++?

C++ is among the most popular programming languages. You can use C++ for basic programming, developing games, developing GUI-based applications, developing database software, developing operating systems, and much more.

If you're a beginner to C++ or want to revise your C++ concepts, check out some of the top websites and courses to get you started.

About The Author

6038eacecadb8-yuvraj%20profile%20pic.jpg?fit=crop&w=100&h=100

Yuvraj Chandra (37 Articles Published)

Yuvraj is a Computer Science undergraduate student at the University of Delhi, India. He's passionate about Full Stack Web Development. When he's not writing, he's exploring the depth of different technologies.

More From Yuvraj Chandra

Subscribe To Our Newsletter

Join our newsletter for tech tips, reviews, free ebooks, and exclusive deals!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK