Com es pot trobar la suma de tots els elements en una matriu

Com es pot trobar la suma de tots els elements en una matriu

Una matriu és una col·lecció d'elements emmagatzemats en ubicacions de memòria contigües. És l’estructura de dades més utilitzada a la programació. En aquest article, aprendreu a trobar la suma de tots els elements d’una matriu mitjançant C ++, Python i JavaScript.





Plantejament del problema

Se us proporciona una matriu de nombres i heu de calcular i imprimir la suma de tots els elements de la matriu donada.





Exemple 1 : Sigui arr = [1, 2, 3, 4, 5]





Per tant, la suma de tots els elements de la matriu = 1 + 2 + 3 + 4 + 5 = 15.

Per tant, la sortida és de 15.



Exemple 2 : Sigui arr = [34, 56, 10, -2, 5, 99]

Per tant, la suma de tots els elements de la matriu = 34 + 56 + 10 + (-2) + 5 + 99 = 202.





Per tant, la sortida és de 202.

Enfocament per trobar la suma de tots els elements en una matriu

Podeu trobar la suma de tots els elements en una matriu seguint l'enfocament següent:





com moure fitxers d'una unitat de Google a una altra
  1. Inicialitzar una variable suma per emmagatzemar la suma total de tots els elements de la matriu.
  2. Recórrer la matriu i afegir cada element de la matriu amb el fitxer suma variable.
  3. Finalment, torneu el fitxer suma variable.

Programa C ++ per trobar la suma de tots els elements en una matriu

A continuació es mostra el programa C ++ per trobar la suma de tots els elements d’una matriu:

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

Sortida:

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

Programa C ++ que utilitza STL per trobar la suma de tots els elements en una matriu

També podeu utilitzar C ++ STL per trobar la suma de tots els elements d’una matriu.

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

Relacionat: Una guia per a principiants de la biblioteca de plantilles estàndard a C ++

el vostre PC no s'ha iniciat correctament

Sortida:

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

Programa Python per trobar la suma de tots els elements en una matriu

A continuació es mostra el programa Python per trobar la suma de tots els elements d’una matriu:

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

Sortida:

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

Relacionat: Idees de projecte Python aptes per a principiants

Programa Python que utilitza la funció integrada per trobar la suma de tots els elements en una matriu

També podeu utilitzar Python's suma() funció per trobar la suma de tots els elements d'una matriu.

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

Sortida:

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

Programa JavaScript per trobar la suma de tots els elements en una matriu

A continuació es mostra el JavaScript programa per trobar la suma de tots els elements d'una matriu:

quant de temps existeix bitconnect
// 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) + '
');

Sortida:

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

Relacionat: Com construir una calculadora senzilla mitjançant HTML, CSS i JavaScript

Programa JavaScript que utilitza el mètode reduce () per trobar la suma de tots els elements en una matriu

També podeu utilitzar JavaScript reduce () mètode per trobar la suma de tots els elements d'una matriu.

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

Sortida:

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

Voleu aprendre C ++?

C ++ es troba entre els llenguatges de programació més populars. Podeu utilitzar C ++ per a programacions bàsiques, desenvolupar jocs, desenvolupar aplicacions basades en GUI, desenvolupar programari de bases de dades, desenvolupar sistemes operatius i molt més.

Si sou principiant de C ++ o voleu revisar els vostres conceptes de C ++, consulteu alguns dels principals llocs web i cursos per començar.

Compartir Compartir Tweet Correu electrònic Com aprendre la programació C ++: 6 llocs per començar

Voleu aprendre C ++? Aquests són els millors llocs web i cursos en línia per a C ++ per a principiants i programadors d’experiència.

Llegiu a continuació
Temes relacionats
  • Programació
  • JavaScript
  • Python
  • Tutorials de codificació
Sobre l'autor Yuvraj Chandra(60 articles publicats)

Yuvraj és estudiant universitari en ciències de la computació a la Universitat de Delhi, Índia. És un apassionat del desenvolupament web Full Stack. Quan no escriu, explora la profunditat de les diferents tecnologies.

Més de Yuvraj Chandra

Subscriu-te al nostre butlletí

Uniu-vos al nostre butlletí per obtenir consells tècnics, ressenyes, llibres electrònics gratuïts i ofertes exclusives.

Feu clic aquí per subscriure-us