如何求数组中所有元素的总和

数组是存储在连续内存位置的元素的集合。它是编程中最常用的数据结构。在本文中,您将学习如何使用 C++、Python 和 JavaScript 查找数组中所有元素的总和。

问题陈述

给定一个数字数组,您需要计算并打印给定数组中所有元素的总和。

示例 1 :让 arr = [1, 2, 3, 4, 5]

因此,数组所有元素的总和 = 1 + 2 + 3 + 4 + 5 = 15。

因此,输出为 15。

示例 2 :让 arr = [34, 56, 10, -2, 5, 99]

因此,数组所有元素的总和 = 34 + 56 + 10 + (-2) + 5 + 99 = 202。

因此,输出为 202。

求数组中所有元素之和的方法

您可以按照以下方法找到数组中所有元素的总和:

  1. 初始化一个变量sum来存储数组所有元素的总和。
  2. 遍历数组并将数组的每个元素与sum变量相加
  3. 最后,返回sum变量。

计算数组中所有元素总和的 C++ 程序

下面是用于查找数组中所有元素之和的 C++ 程序:

 // 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;
}

输出:

 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

使用 STL 计算数组中所有元素之和的 C++ 程序

您还可以使用 C++ STL 来查找数组中所有元素的总和。

 // 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;
}

相关: C++ 标准模板库的初学者指南

输出:

 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 程序

以下是用于查找数组中所有元素之和的 Python 程序:

 # 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))

输出:

 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项目思路

Python 程序使用内置函数求数组中所有元素的总和

您还可以使用 Python 的sum()函数来查找数组中所有元素的总和。

 # 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))

输出:

 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 程序

下面是用于查找数组中所有元素之和的JavaScript程序:

 // 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>");

输出:

 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

相关:如何使用 HTML、CSS 和 JavaScript 构建简单的计算器

JavaScript 程序使用 reduce() 方法查找数组中所有元素的总和

您还可以使用 JavaScript 的reduce()方法来查找数组中所有元素的总和。

 // 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>");

输出:

 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++?

C++ 是最流行的编程语言之一。您可以使用 C++ 进行基本编程、开发游戏、开发基于 GUI 的应用程序、开发数据库软件、开发操作系统等等。

如果您是 C++ 的初学者或想要修改您的 C++ 概念,请查看一些顶级网站和课程以帮助您入门。