Hoe de som van alle elementen in een array te vinden

Hoe de som van alle elementen in een array te vinden

Een array is een verzameling elementen die zijn opgeslagen op aaneengesloten geheugenlocaties. Het is de meest gebruikte datastructuur bij het programmeren. In dit artikel leer je hoe je de som van alle elementen in een array kunt vinden met behulp van C++, Python en JavaScript.





Probleemstelling

U krijgt een reeks getallen en u moet de som van alle elementen in de gegeven reeks berekenen en afdrukken.





voorbeeld 1 : Laat arr = [1, 2, 3, 4, 5]





Daarom is de som van alle elementen van de array = 1 + 2 + 3 + 4 + 5 = 15.

De output is dus 15.



Voorbeeld 2 : Laat arr = [34, 56, 10, -2, 5, 99]

Daarom is de som van alle elementen van de array = 34 + 56 + 10 + (-2) + 5 + 99 = 202.





De output is dus 202.

Benadering om de som van alle elementen in een array te vinden

U kunt de som van alle elementen in een array vinden door de onderstaande benadering te volgen:





bestanden overzetten van de ene Google Drive naar de andere
  1. Een variabele initialiseren som om de totale som van alle elementen van de array op te slaan.
  2. Doorloop de array en voeg elk element van de array toe met de som variabel.
  3. Breng ten slotte de . terug som variabel.

C++-programma om de som van alle elementen in een array te vinden

Hieronder staat het C++-programma om de som van alle elementen in een array te vinden:

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

Uitgang:

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++-programma dat STL gebruikt om de som van alle elementen in een array te vinden

U kunt C++ STL ook gebruiken om de som van alle elementen in een array te vinden.

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

Gerelateerd: Een beginnershandleiding voor de standaardsjabloonbibliotheek in C++

wat moet u doen indien mogelijk voordat u de uefi-firmware bijwerkt?

Uitgang:

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-programma om de som van alle elementen in een array te vinden

Hieronder staat het Python-programma om de som van alle elementen in een array te vinden:

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

Uitgang:

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

Verwant: Ideeën voor Python-projecten die geschikt zijn voor beginners

Python-programma met ingebouwde functie om de som van alle elementen in een array te vinden

Je kunt ook Python's gebruiken som() functie om de som van alle elementen in een array te vinden.

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

Uitgang:

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-programma om de som van alle elementen in een array te vinden

Hieronder is de JavaScript programma om de som van alle elementen in een array te vinden:

kun je privéberichten sturen op youtube
// 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) + '
');

Uitgang:

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

Gerelateerd: Een eenvoudige rekenmachine bouwen met HTML, CSS en JavaScript

JavaScript-programma De methode reduce() gebruiken om de som van alle elementen in een array te vinden

U kunt ook JavaScript's gebruiken verminderen() methode om de som van alle elementen in een array te vinden.

// 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 + '
');

Uitgang:

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

Wil je C++ leren?

C++ is een van de meest populaire programmeertalen. U kunt C++ gebruiken voor basisprogrammering, het ontwikkelen van games, het ontwikkelen van GUI-gebaseerde applicaties, het ontwikkelen van databasesoftware, het ontwikkelen van besturingssystemen en nog veel meer.

Als je een beginner bent met C++ of je C++-concepten wilt herzien, bekijk dan enkele van de beste websites en cursussen om je op weg te helpen.

Deel Deel Tweeten E-mail Hoe u C++-programmering leert: 6 sites om aan de slag te gaan

C++ leren? Hier zijn de beste websites en online cursussen voor C++ voor zowel beginners als ervaren programmeurs.

Lees volgende
Gerelateerde onderwerpen
  • Programmeren
  • JavaScript
  • Python
  • Codeerhandleidingen
Over de auteur Yuvraj Chandra(60 artikelen gepubliceerd)

Yuvraj is een student Computerwetenschappen aan de Universiteit van Delhi, India. Hij is gepassioneerd door Full Stack Web Development. Als hij niet aan het schrijven is, onderzoekt hij de diepte van verschillende technologieën.

Meer van Yuvraj Chandra

Abonneer op onze nieuwsbrief

Word lid van onze nieuwsbrief voor technische tips, recensies, gratis e-boeken en exclusieve deals!

Klik hier om je te abonneren