Masīvs ir elementu kopums, kas tiek glabāts blakus esošās atmiņas vietās. Tā ir programmēšanā visbiežāk izmantotā datu struktūra. Šajā rakstā jūs uzzināsit, kā atrast visu masīva elementu summu, izmantojot C ++, Python un JavaScript.
Paziņojums par problēmu
Jums ir dots skaitļu masīvs, un jums ir jāaprēķina un jāizdrukā visu dotā masīva elementu summa.
1. piemērs : Ļaujiet arr = [1, 2, 3, 4, 5]
Tāpēc visu masīva elementu summa = 1 + 2 + 3 + 4 + 5 = 15.
Tādējādi izlaide ir 15.
2. piemērs : Ļaujiet arr = [34, 56, 10, -2, 5, 99]
Tāpēc visu masīva elementu summa = 34 + 56 + 10 + (-2) + 5 + 99 = 202.
Tādējādi izlaide ir 202.
Pieeja masīva visu elementu summas atrašanai
Jūs varat atrast visu masīva elementu summu, ievērojot tālāk norādīto pieeju.
kā pārsūtīt e -pastu no Outlook uz Gmail
- Inicializējiet mainīgo summa lai saglabātu visu masīva elementu kopējo summu.
- Pārvietojiet masīvu un pievienojiet katru masīva elementu ar summa mainīgais.
- Visbeidzot, atgrieziet summa mainīgais.
C ++ programma, lai atrastu visu masīva elementu summu
Zemāk ir programma C ++, lai atrastu visu masīva elementu summu:
// 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;
}
Izeja:
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 ++ programma, izmantojot STL, lai atrastu visu masīva elementu summu
Varat arī izmantot C ++ STL, lai atrastu visu masīva elementu summu.
// 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;
}
Saistīts: Iesācēja rokasgrāmata standarta veidņu bibliotēkai C ++
kā saglabāt YouTube videoklipus iPhone
Izeja:
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 programma, lai atrastu visu masīva elementu summu
Zemāk ir programma Python, lai atrastu visu masīva elementu summu:
# 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))
Izeja:
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
Saistīts: Python projekta idejas, kas piemērotas iesācējiem
Python programma, izmantojot iebūvētu funkciju, lai atrastu visu masīva elementu summu
Varat arī izmantot Python's summa () funkcija, lai atrastu visu masīva elementu summu.
# 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))
Izeja:
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 programma, lai atrastu visu masīva elementu summu
Zemāk ir JavaScript programma, lai atrastu visu masīva elementu summu:
windows 10 bsod memory_management
// 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) + '
');
Izeja:
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
Saistīts: Kā izveidot vienkāršu kalkulatoru, izmantojot HTML, CSS un JavaScript
JavaScript programma Izmantojot metodi reducēt (), lai atrastu visu masīva elementu summu
Varat arī izmantot JavaScript samazināt () metode, lai atrastu visu masīva elementu summu.
// 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 + '
');
Izeja:
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
Vai vēlaties iemācīties C ++?
C ++ ir viena no populārākajām programmēšanas valodām. Jūs varat izmantot C ++ pamata programmēšanai, spēļu izstrādei, uz GUI balstītu lietojumprogrammu izstrādei, datu bāzes programmatūras izstrādei, operētājsistēmu izstrādei un daudz ko citu.
Ja esat iesācējs lietot C ++ vai vēlaties pārskatīt savas C ++ koncepcijas, iepazīstieties ar dažām populārākajām vietnēm un kursiem, lai sāktu darbu.
Kopīgot Kopīgot Čivināt E -pasts Kā apgūt C ++ programmēšanu: 6 vietnes, lai sāktu darbuVai vēlaties iemācīties C ++? Šeit ir labākās vietnes un tiešsaistes kursi C ++ iesācējiem un pieredzējušiem programmētājiem.
Lasīt Tālāk Saistītās tēmas- Programmēšana
- JavaScript
- Python
- Kodēšanas apmācības
Yuvraj ir datorzinātņu bakalaura students Deli universitātē, Indijā. Viņš aizraujas ar Full Stack tīmekļa izstrādi. Kad viņš neraksta, viņš pēta dažādu tehnoloģiju dziļumu.
Vairāk no Yuvraj ChandraAbonējiet mūsu biļetenu
Pievienojieties mūsu informatīvajam izdevumam, lai iegūtu tehniskus padomus, pārskatus, bezmaksas e -grāmatas un ekskluzīvus piedāvājumus!
Noklikšķiniet šeit, lai abonētu