Kako najti vsoto vseh elementov v nizu

Kako najti vsoto vseh elementov v nizu

Niz je zbirka elementov, shranjenih na sosednjih pomnilniških mestih. To je najpogosteje uporabljena podatkovna struktura pri programiranju. V tem članku boste izvedeli, kako poiskati vsoto vseh elementov v matriki s pomočjo C ++, Python in JavaScript.





Izjava o težavi

Dobili ste niz številk in morate izračunati in natisniti vsoto vseh elementov v danem nizu.





Primer 1 : Naj bo arr = [1, 2, 3, 4, 5]





Zato je vsota vseh elementov matrike = 1 + 2 + 3 + 4 + 5 = 15.

Tako je izhod 15.



Primer 2 : Naj bo arr = [34, 56, 10, -2, 5, 99]

Zato je vsota vseh elementov matrike = 34 + 56 + 10 + (-2) + 5 + 99 = 202.





Tako je izhod 202.

Pristop k iskanju vsote vseh elementov v nizu

Vsoto vseh elementov v matriki lahko najdete po spodnjem pristopu:





brezplačno prenesite filme brez wifi
  1. Inicializirajte spremenljivko vsota za shranjevanje skupne vsote vseh elementov matrike.
  2. Prečkajte matriko in dodajte vsak element matrike z vsota spremenljivka.
  3. Na koncu vrnite vsota spremenljivka.

Program C ++ za iskanje vsote vseh elementov v nizu

Spodaj je program C ++ za iskanje vsote vseh elementov v nizu:

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

Izhod:

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

Program C ++ z uporabo STL za iskanje vsote vseh elementov v nizu

Za iskanje vsote vseh elementov v matriki lahko uporabite tudi C ++ STL.

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

Povezano: Vodnik za začetnike po knjižnici standardnih predlog v C ++

kako preizkusiti baterijo mobilnega telefona

Izhod:

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

Pythonov program za iskanje vsote vseh elementov v nizu

Spodaj je program Python za iskanje vsote vseh elementov v nizu:

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

Izhod:

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

Povezano: Ideje za Python projekt primerne za začetnike

Program Python z vgrajeno funkcijo za iskanje vsote vseh elementov v nizu

Uporabite lahko tudi Python vsota () funkcijo za iskanje vsote vseh elementov v matriki.

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

Izhod:

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

Program JavaScript za iskanje vsote vseh elementov v nizu

Spodaj je JavaScript program za iskanje vsote vseh elementov v nizu:

kako obrisati trdi disk v sistemu Windows 10
// 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) + '
');

Izhod:

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

Povezano: Kako zgraditi preprost kalkulator z uporabo HTML, CSS in JavaScript

Program JavaScript z uporabo metode reduce () za iskanje vsote vseh elementov v nizu

Uporabite lahko tudi JavaScript zmanjšaj () metoda za iskanje vsote vseh elementov v matriki.

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

Izhod:

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

Se želite naučiti C ++?

C ++ je med najbolj priljubljenimi programskimi jeziki. C ++ lahko uporabite za osnovno programiranje, razvoj iger, razvoj aplikacij, ki temeljijo na grafičnem vmesniku, razvoj programske opreme za zbirke podatkov, razvoj operacijskih sistemov in še veliko več.

Če ste začetnik C ++ ali želite spremeniti svoje koncepte C ++, si oglejte nekaj najboljših spletnih mest in tečajev.

Deliti Deliti Cvrkutati E-naslov Kako se naučiti programiranja C ++: 6 spletnih mest za začetek

Se želite naučiti C ++? Tu so najboljša spletna mesta in spletni tečaji za C ++ za začetnike in programerje z izkušnjami.

Preberite Naprej
Sorodne teme
  • Programiranje
  • JavaScript
  • Python
  • Vadnice za kodiranje
O avtorju Yuvraj Chandra(Objavljenih 60 člankov)

Yuvraj je dodiplomski študent računalništva na Univerzi v Delhiju v Indiji. Navdušen je nad spletnim razvojem Full Stack. Ko ne piše, raziskuje globino različnih tehnologij.

Več od Yuvraja Chandre

Naročite se na naše novice

Pridružite se našemu glasilu za tehnične nasvete, ocene, brezplačne e -knjige in ekskluzivne ponudbe!

Kliknite tukaj, če se želite naročiti