Come trovare la somma di tutti gli elementi in un array

Come trovare la somma di tutti gli elementi in un array

Un array è una raccolta di elementi archiviati in locazioni di memoria contigue. È la struttura dati più utilizzata nella programmazione. In questo articolo imparerai come trovare la somma di tutti gli elementi in un array usando C++, Python e JavaScript.





Dichiarazione problema

Ti viene data una matrice di numeri e devi calcolare e stampare la somma di tutti gli elementi nella matrice data.





Esempio 1 : Sia arr = [1, 2, 3, 4, 5]





Pertanto, la somma di tutti gli elementi dell'array = 1 + 2 + 3 + 4 + 5 = 15.

Quindi, l'output è 15.



Esempio 2 : Sia arr = [34, 56, 10, -2, 5, 99]

Pertanto, la somma di tutti gli elementi dell'array = 34 + 56 + 10 + (-2) + 5 + 99 = 202.





Pertanto, l'output è 202.

Approccio per trovare la somma di tutti gli elementi in un array

Puoi trovare la somma di tutti gli elementi in un array seguendo l'approccio seguente:





posso scaricare giochi ps3 su ps4?
  1. Inizializzare una variabile somma per memorizzare la somma totale di tutti gli elementi dell'array.
  2. Attraversa l'array e aggiungi ogni elemento dell'array con il somma variabile.
  3. Infine, restituire il somma variabile.

Programma C++ per trovare la somma di tutti gli elementi in un array

Di seguito è riportato il programma C++ per trovare la somma di tutti gli elementi in un 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;
}

Produzione:

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

Programma C++ che utilizza STL per trovare la somma di tutti gli elementi in un array

Puoi anche usare C++ STL per trovare la somma di tutti gli elementi in un array.

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

Correlati: una guida per principianti alla libreria di modelli standard in C++

come scoprire quale scheda grafica ho Windows 10

Produzione:

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

Programma Python per trovare la somma di tutti gli elementi in un array

Di seguito è riportato il programma Python per trovare la somma di tutti gli elementi in un 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))

Produzione:

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

Imparentato: Idee di progetto Python adatte ai principianti

Programma Python che utilizza la funzione incorporata per trovare la somma di tutti gli elementi in un array

Puoi anche usare Python somma() funzione per trovare la somma di tutti gli elementi in un 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))

Produzione:

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

Programma JavaScript per trovare la somma di tutti gli elementi in un array

Di seguito è riportato il JavaScript programma per trovare la somma di tutti gli elementi in un array:

come collegare l'auricolare wireless a xbox one
// 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) + '
');

Produzione:

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

Correlati: Come costruire una calcolatrice semplice utilizzando HTML, CSS e JavaScript

Programma JavaScript che utilizza il metodo reduce() per trovare la somma di tutti gli elementi in un array

Puoi anche usare JavaScript ridurre() metodo per trovare la somma di tutti gli elementi in un 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 {
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 + '
');

Produzione:

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

Vuoi imparare il C++?

C++ è tra i linguaggi di programmazione più popolari. Puoi usare C++ per la programmazione di base, lo sviluppo di giochi, lo sviluppo di applicazioni basate su GUI, lo sviluppo di software di database, lo sviluppo di sistemi operativi e molto altro.

Se sei un principiante del C++ o vuoi rivedere i tuoi concetti di C++, dai un'occhiata ad alcuni dei migliori siti Web e corsi per iniziare.

Condividere Condividere Tweet E-mail Come imparare la programmazione C++: 6 siti per iniziare

Vuoi imparare il C++? Ecco i migliori siti Web e corsi online su C++ per principianti e programmatori esperti.

Leggi Avanti
Argomenti correlati
  • Programmazione
  • JavaScript
  • Pitone
  • Tutorial sulla programmazione
Circa l'autore Yuvraj Chandra(60 articoli pubblicati)

Yuvraj è uno studente universitario di Informatica presso l'Università di Delhi, in India. È appassionato di sviluppo Web Full Stack. Quando non scrive, esplora la profondità di diverse tecnologie.

Altro da Yuvraj Chandra

Iscriviti alla nostra Newsletter

Iscriviti alla nostra newsletter per consigli tecnici, recensioni, ebook gratuiti e offerte esclusive!

Clicca qui per iscriverti