Masyvas yra elementų, saugomų gretimose atminties vietose, rinkinys. Tai dažniausiai naudojama programavimo duomenų struktūra. Šiame straipsnyje sužinosite, kaip rasti visų masyvo elementų sumą naudojant „C ++“, „Python“ ir „JavaScript“.
Problemos pareiškimas
Jums pateikiamas skaičių masyvas, ir jūs turite apskaičiuoti ir išspausdinti visų duoto masyvo elementų sumą.
1 pavyzdys : Tegul arr = [1, 2, 3, 4, 5]
Todėl visų masyvo elementų suma = 1 + 2 + 3 + 4 + 5 = 15.
Taigi išėjimas yra 15.
2 pavyzdys : Tegul arr = [34, 56, 10, -2, 5, 99]
Todėl visų masyvo elementų suma = 34 + 56 + 10 + (-2) + 5 + 99 = 202.
Taigi išvestis yra 202.
Metodas rasti visų masyvo elementų sumą
Visų masyvo elementų sumą galite rasti laikydamiesi toliau pateikto metodo:
kaip rasti mano pagrindinės plokštės modelį
- Inicijuokite kintamąjį suma išsaugoti bendrą visų masyvo elementų sumą.
- Eikite į masyvą ir pridėkite kiekvieną masyvo elementą su suma kintamasis.
- Galiausiai grąžinkite suma kintamasis.
C ++ programa, skirta rasti visų masyvo elementų sumą
Žemiau yra C ++ programa, skirta rasti visų masyvo elementų sumą:
// 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;
}
Išėjimas:
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 ++ programa naudojant STL, norint rasti visų masyvo elementų sumą
Taip pat galite naudoti C ++ STL, kad surastumėte visų masyvo elementų sumą.
// 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;
}
Susijęs: „C ++“ standartinės šablonų bibliotekos pradedančiųjų vadovas
Ar galite žiūrėti filmus per „Nintendo Switch“?
Išėjimas:
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“ programa, skirta rasti visų masyvo elementų sumą
Žemiau yra „Python“ programa, skirta rasti visų masyvo elementų sumą:
# 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))
Išėjimas:
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
Susijęs: „Python“ projekto idėjos tinka pradedantiesiems
„Python“ programa, naudojant integruotą funkciją visų masyvo elementų sumai rasti
Taip pat galite naudoti „Python“ suma() funkcija, skirta rasti visų masyvo elementų sumą.
# 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))
Išėjimas:
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“ programa, skirta rasti visų masyvo elementų sumą
Žemiau yra „JavaScript“ programa, skirta rasti visų masyvo elementų sumą:
kaip atidengti dainą spotify
// 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) + '
');
Išėjimas:
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
Susiję: Kaip sukurti paprastą skaičiuotuvą naudojant HTML, CSS ir „JavaScript“
„JavaScript“ programa „Reduce“ () metodo naudojimas visų masyvo elementų sumai rasti
Taip pat galite naudoti „JavaScript“ sumažinti () metodas rasti visų masyvo elementų sumą.
// 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 + '
');
Išėjimas:
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
Nori išmokti C ++?
C ++ yra viena populiariausių programavimo kalbų. Galite naudoti C ++ pagrindiniam programavimui, žaidimų kūrimui, GUI pagrįstų programų kūrimui, duomenų bazės programinės įrangos kūrimui, operacinių sistemų kūrimui ir dar daugiau.
Jei esate pradedantysis C ++ ar norite peržiūrėti savo C ++ sąvokas, peržiūrėkite keletą geriausių svetainių ir kursų, kad galėtumėte pradėti.
Dalintis Dalintis „Tweet“ Paštu Kaip išmokti programuoti C ++: 6 svetainės, kad galėtumėte pradėtiNori išmokti C ++? Čia yra geriausios C ++ svetainės ir internetiniai kursai pradedantiesiems ir patyrusiems programuotojams.
Skaityti toliau Susijusios temos- Programavimas
- „JavaScript“
- Python
- Kodavimo pamokos
Yuvraj yra kompiuterių mokslo bakalauro studentas Delyje, Indijoje. Jis aistringas „Full Stack“ žiniatinklio kūrimui. Kai jis nerašo, jis tyrinėja skirtingų technologijų gylį.
Daugiau iš Yuvraj ChandraPrenumeruokite mūsų naujienlaiškį
Prisijunkite prie mūsų naujienlaiškio, kad gautumėte techninių patarimų, apžvalgų, nemokamų el. Knygų ir išskirtinių pasiūlymų!
Norėdami užsiprenumeruoti, spustelėkite čia