Niz je zbirka elemenata pohranjenih na susjednim memorijskim mjestima. To je najčešće korištena struktura podataka u programiranju. U ovom ćete članku naučiti kako pronaći zbroj svih elemenata u nizu koristeći C ++, Python i JavaScript.
Izjava o problemu
Dobili ste niz brojeva i morate izračunati i ispisati zbroj svih elemenata u danom nizu.
Primjer 1 : Neka je arr = [1, 2, 3, 4, 5]
Stoga je zbroj svih elemenata niza = 1 + 2 + 3 + 4 + 5 = 15.
Dakle, izlaz je 15.
Primjer 2 : Neka je arr = [34, 56, 10, -2, 5, 99]
Stoga je zbroj svih elemenata niza = 34 + 56 + 10 + (-2) + 5 + 99 = 202.
Dakle, izlaz je 202.
Pristup pronalaženju zbroja svih elemenata u nizu
Zbroj svih elemenata u nizu možete pronaći slijedeći donji pristup:
kako isključiti brzo pokretanje sustava Windows 10
- Inicijalizirajte varijablu iznos za pohranu ukupnog zbroja svih elemenata niza.
- Pomičite niz i dodajte svaki element niza sa iznos promjenjivo.
- Na kraju vratite iznos promjenjivo.
C ++ program za pronalaženje zbroja svih elemenata u nizu
Ispod je program C ++ za pronalaženje zbroja svih elemenata u 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;
}
Izlaz:
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 ++ program pomoću STL -a za pronalaženje zbroja svih elemenata u nizu
Također možete koristiti C ++ STL za pronalaženje zbroja svih elemenata u nizu.
// 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: Vodič za početnike u knjižnici standardnih predložaka u C ++
čijem telefonskom broju ovo pripada besplatno
Izlaz:
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 program za pronalaženje zbroja svih elemenata u nizu
Ispod je program Python za pronalaženje zbroja svih elemenata u 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))
Izlaz:
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 projekte prikladne za početnike
Program Python pomoću ugrađene funkcije za pronalaženje zbroja svih elemenata u nizu
Također možete koristiti Python iznos() funkcija za pronalaženje zbroja svih elemenata u nizu.
# 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))
Izlaz:
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 program za pronalaženje zbroja svih elemenata u nizu
Ispod je JavaScript program za pronalaženje zbroja svih elemenata u nizu:
iphone zvučnik ne radi tijekom poziva
// 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) + '
');
Izlaz:
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 izgraditi jednostavan kalkulator pomoću HTML -a, CSS -a i JavaScript -a
JavaScript program Pomoću metode reduce () za pronalaženje zbroja svih elemenata u nizu
Također možete koristiti JavaScript smanjiti() metoda za pronalaženje zbroja svih elemenata u nizu.
// 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 + '
');
Izlaz:
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
Želite naučiti C ++?
C ++ jedan je od najpopularnijih programskih jezika. C ++ možete koristiti za osnovno programiranje, razvoj igara, razvoj aplikacija temeljenih na grafičkom sučelju, razvoj softvera za baze podataka, razvoj operativnih sustava i još mnogo toga.
Ako ste početnik u C ++ ili želite revidirati svoje C ++ koncepte, pogledajte neke od najboljih web stranica i tečajeva za početak.
Udio Udio Cvrkut E -pošta Kako naučiti programiranje na C ++: 6 stranica za početakŽelite naučiti C ++? Ovdje su najbolje web stranice i online tečajevi za C ++ za početnike i programere s iskustvom.
Pročitajte Dalje Povezane teme- Programiranje
- JavaScript
- Piton
- Vodiči za kodiranje
Yuvraj je student preddiplomskog studija Računarstva na Sveučilištu u Delhiju u Indiji. Oduševljen je Full Stack web razvojem. Kad ne piše, istražuje dubinu različitih tehnologija.
Više od Yuvraja ChandrePretplatite se na naše obavijesti
Pridružite se našem biltenu za tehničke savjete, recenzije, besplatne e -knjige i ekskluzivne ponude!
Kliknite ovdje za pretplatu