Un tableau est un ensemble d'éléments stockés dans des emplacements mémoire contigus. C'est la structure de données la plus utilisée en programmation. Dans cet article, vous apprendrez à trouver la somme de tous les éléments d'un tableau à l'aide de C++, Python et JavaScript.
Énoncé du problème
On vous donne un tableau de nombres, et vous devez calculer et imprimer la somme de tous les éléments du tableau donné.
Exemple 1 : Soit arr = [1, 2, 3, 4, 5]
Par conséquent, la somme de tous les éléments du tableau = 1 + 2 + 3 + 4 + 5 = 15.
Ainsi, la sortie est de 15.
Exemple 2 : Soit arr = [34, 56, 10, -2, 5, 99]
Par conséquent, la somme de tous les éléments du tableau = 34 + 56 + 10 + (-2) + 5 + 99 = 202.
Ainsi, la sortie est 202.
Approche pour trouver la somme de tous les éléments d'un tableau
Vous pouvez trouver la somme de tous les éléments d'un tableau en suivant l'approche ci-dessous :
comment retirer un protecteur d'écran
- Initialiser une variable somme pour stocker la somme totale de tous les éléments du tableau.
- Parcourez le tableau et ajoutez chaque élément du tableau avec le somme variable.
- Enfin, retournez le somme variable.
Programme C++ pour trouver la somme de tous les éléments d'un tableau
Vous trouverez ci-dessous le programme C++ pour trouver la somme de tous les éléments d'un tableau :
// 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;
}
Sortir:
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
Programme C++ utilisant STL pour trouver la somme de tous les éléments d'un tableau
Vous pouvez également utiliser C++ STL pour trouver la somme de tous les éléments d'un tableau.
// 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;
}
Connexes : Guide du débutant sur la bibliothèque de modèles standard en C++
quelqu'un a piraté mon compte psn comment le récupérer
Sortir:
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
Programme Python pour trouver la somme de tous les éléments d'un tableau
Vous trouverez ci-dessous le programme Python pour trouver la somme de tous les éléments d'un tableau :
# 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))
Sortir:
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
En rapport: Idées de projets Python adaptées aux débutants
Programme Python utilisant une fonction intégrée pour trouver la somme de tous les éléments d'un tableau
Vous pouvez également utiliser Python somme() fonction pour trouver la somme de tous les éléments d'un tableau.
# 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))
Sortir:
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
Programme JavaScript pour trouver la somme de tous les éléments d'un tableau
Ci-dessous se trouve le JavaScript programme pour trouver la somme de tous les éléments d'un tableau :
pourquoi mes haut-parleurs ne fonctionnent pas sur mon pc
// 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) + '
');
Sortir:
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
Connexes : Comment créer une calculatrice simple à l'aide de HTML, CSS et JavaScript
Programme JavaScript utilisant la méthode reduce() pour trouver la somme de tous les éléments d'un tableau
Vous pouvez également utiliser JavaScript réduire() méthode pour trouver la somme de tous les éléments d'un tableau.
// 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 + '
');
Sortir:
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
Vous voulez apprendre le C++ ?
C++ fait partie des langages de programmation les plus populaires. Vous pouvez utiliser C++ pour la programmation de base, le développement de jeux, le développement d'applications basées sur une interface graphique, le développement de logiciels de base de données, le développement de systèmes d'exploitation et bien plus encore.
Si vous êtes un débutant en C++ ou si vous souhaitez réviser vos concepts C++, consultez certains des meilleurs sites Web et cours pour vous aider à démarrer.
Partager Partager Tweeter E-mail Comment apprendre la programmation C++ : 6 sites pour commencerVous voulez apprendre le C++ ? Voici les meilleurs sites Web et cours en ligne sur C++ pour les programmeurs débutants et expérimentés.
Lire la suite Rubriques connexes- La programmation
- JavaScript
- Python
- Tutoriels de codage
Yuvraj est un étudiant de premier cycle en informatique à l'Université de Delhi, en Inde. Il est passionné par le développement Web Full Stack. Quand il n'écrit pas, il explore la profondeur de différentes technologies.
Plus de Yuvraj ChandraAbonnez-vous à notre newsletter
Rejoignez notre newsletter pour des conseils techniques, des critiques, des ebooks gratuits et des offres exclusives !
Cliquez ici pour vous abonner