So finden Sie die Summe aller Elemente in einem Array

So finden Sie die Summe aller Elemente in einem Array

Ein Array ist eine Sammlung von Elementen, die an zusammenhängenden Speicherplätzen gespeichert sind. Es ist die am häufigsten verwendete Datenstruktur in der Programmierung. In diesem Artikel erfahren Sie, wie Sie mit C++, Python und JavaScript die Summe aller Elemente in einem Array ermitteln.





Problemstellung

Sie erhalten ein Array von Zahlen, und Sie müssen die Summe aller Elemente in dem angegebenen Array berechnen und ausgeben.





Beispiel 1 : Sei arr = [1, 2, 3, 4, 5]





Daher ist die Summe aller Elemente des Arrays = 1 + 2 + 3 + 4 + 5 = 15.

Somit ist die Ausgabe 15.



Beispiel 2 : Sei arr = [34, 56, 10, -2, 5, 99]

Daher ist die Summe aller Elemente des Arrays = 34 + 56 + 10 + (-2) + 5 + 99 = 202.





Somit ist die Ausgabe 202.

Ansatz zum Ermitteln der Summe aller Elemente in einem Array

Sie können die Summe aller Elemente in einem Array ermitteln, indem Sie dem folgenden Ansatz folgen:





Wohin gehen Screenshots auf dem Mac?
  1. Initialisieren einer Variablen Summe um die Gesamtsumme aller Elemente des Arrays zu speichern.
  2. Durchqueren Sie das Array und fügen Sie jedes Element des Arrays mit dem hinzu Summe Variable.
  3. Geben Sie schließlich die zurück Summe Variable.

C++-Programm zum Ermitteln der Summe aller Elemente in einem Array

Unten ist das C++-Programm zum Ermitteln der Summe aller Elemente in einem Array:

// C++ program to find the sum of elements in an array
#include
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 {
sum += arr[i];
}
return sum;
}

// Function to print the elements of the array
void printArray(int arr[], int size)
{
for(int i=0; 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;
}

Ausgabe:

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++-Programm, das STL verwendet, um die Summe aller Elemente in einem Array zu finden

Sie können auch C++ STL verwenden, um die Summe aller Elemente in einem Array zu ermitteln.

// C++ program using STL to find the sum of elements in an array
#include
using namespace std;
// Function to print the elements of the array
void printArray(int arr[], int size)
{
for(int i=0; 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;
}

Verwandte: Ein Leitfaden für Anfänger zur Standardvorlagenbibliothek in C++

Mac-Terminalbefehle Spickzettel pdf

Ausgabe:

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-Programm zum Ermitteln der Summe aller Elemente in einem Array

Unten ist das Python-Programm, um die Summe aller Elemente in einem Array zu finden:

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

Ausgabe:

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

Verwandt: Python-Projektideen für Anfänger geeignet

Python-Programm mit eingebauter Funktion zum Ermitteln der Summe aller Elemente in einem Array

Sie können auch Pythons verwenden Summe() Funktion, um die Summe aller Elemente in einem Array zu ermitteln.

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

Ausgabe:

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-Programm zum Ermitteln der Summe aller Elemente in einem Array

Unten ist die JavaScript Programm, um die Summe aller Elemente in einem Array zu finden:

Wie man Android vom PC aus steuert
// 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 {
sum += arr[i];
}
return sum;
}

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

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

Ausgabe:

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

Verwandte: So erstellen Sie einen einfachen Rechner mit HTML, CSS und JavaScript

JavaScript-Programm, das die Methode Reduce() verwendet, um die Summe aller Elemente in einem Array zu ermitteln

Sie können auch JavaScript verwenden reduzieren() -Methode, um die Summe aller Elemente in einem Array zu ermitteln.

// 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 {
document.write(arr[i] + ' ');
}
document.write('
');
}

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

Ausgabe:

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

Möchten Sie C++ lernen?

C++ gehört zu den beliebtesten Programmiersprachen. Sie können C++ für die grundlegende Programmierung, die Entwicklung von Spielen, die Entwicklung von GUI-basierten Anwendungen, die Entwicklung von Datenbanksoftware, die Entwicklung von Betriebssystemen und vieles mehr verwenden.

Wenn Sie C++-Anfänger sind oder Ihre C++-Konzepte überarbeiten möchten, sehen Sie sich einige der besten Websites und Kurse an, um Ihnen den Einstieg zu erleichtern.

Teilen Teilen Tweet Email C++-Programmierung lernen: 6 Sites zum Einstieg

Möchten Sie C++ lernen? Hier sind die besten Websites und Online-Kurse zu C++ für Anfänger und erfahrene Programmierer.

Weiter lesen
Verwandte Themen
  • Programmierung
  • JavaScript
  • Python
  • Codierungs-Tutorials
Über den Autor Yuvraj Chandra(60 veröffentlichte Artikel)

Yuvraj studiert Informatik an der University of Delhi, Indien. Seine Leidenschaft gilt der Full-Stack-Webentwicklung. Wenn er nicht gerade schreibt, erforscht er die Tiefe verschiedener Technologien.

Mehr von Yuvraj Chandra

Abonniere unseren Newsletter

Abonnieren Sie unseren Newsletter für technische Tipps, Rezensionen, kostenlose E-Books und exklusive Angebote!

Klicken Sie hier, um zu abonnieren