Jak znaleźć sumę wszystkich elementów w tablicy

Jak znaleźć sumę wszystkich elementów w tablicy

Tablica to zbiór elementów przechowywanych w ciągłych lokalizacjach pamięci. Jest to najczęściej używana struktura danych w programowaniu. W tym artykule dowiesz się, jak znaleźć sumę wszystkich elementów w tablicy za pomocą C++, Pythona i JavaScript.





Stwierdzenie problemu

Dostajesz tablicę liczb i musisz obliczyć i wydrukować sumę wszystkich elementów w podanej tablicy.





Przykład 1 : Niech arr = [1, 2, 3, 4, 5]





Dlatego suma wszystkich elementów tablicy = 1 + 2 + 3 + 4 + 5 = 15.

Tak więc wyjście wynosi 15.



Przykład 2 : Niech arr = [34, 56, 10, -2, 5, 99]

Dlatego suma wszystkich elementów tablicy = 34 + 56 + 10 + (-2) + 5 + 99 = 202.





Tak więc wyjście wynosi 202.

Podejście do znalezienia sumy wszystkich elementów w tablicy

Możesz znaleźć sumę wszystkich elementów w tablicy, wykonując poniższe podejście:





nieznane żądanie deskryptora urządzenia USB nie powiodło się w systemie Windows 10
  1. Zainicjuj zmienną suma do przechowywania całkowitej sumy wszystkich elementów tablicy.
  2. Przemierz tablicę i dodaj każdy element tablicy za pomocą suma zmienny.
  3. Na koniec zwróć suma zmienny.

Program C++ do znajdowania sumy wszystkich elementów w tablicy

Poniżej znajduje się program C++ do znalezienia sumy wszystkich elementów w tablicy:

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

Wyjście:

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++ używający STL do znalezienia sumy wszystkich elementów w tablicy

Możesz także użyć C++ STL, aby znaleźć sumę wszystkich elementów w tablicy.

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

Powiązane: Przewodnik dla początkujących do standardowej biblioteki szablonów w C++

jak sprawdzić, czy zostałeś zablokowany na facebooku

Wyjście:

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 Pythona do znajdowania sumy wszystkich elementów w tablicy

Poniżej znajduje się program Pythona do znajdowania sumy wszystkich elementów w tablicy:

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

Wyjście:

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

Związane z: Pomysły na projekty w Pythonie odpowiednie dla początkujących

Program w Pythonie wykorzystujący wbudowaną funkcję do znajdowania sumy wszystkich elementów w tablicy

Możesz także użyć Pythona suma() funkcja, aby znaleźć sumę wszystkich elementów w tablicy.

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

Wyjście:

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 do znajdowania sumy wszystkich elementów w tablicy

Poniżej znajduje się JavaScript program do znalezienia sumy wszystkich elementów w tablicy:

jak zrobić przygodę tekstową
// 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) + '
');

Wyjście:

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

Powiązane: Jak zbudować prosty kalkulator za pomocą HTML, CSS i JavaScript

Program JavaScript Używanie metody reduction() do znajdowania sumy wszystkich elementów w tablicy

Możesz także użyć JavaScript zmniejszyć() metoda, aby znaleźć sumę wszystkich elementów w tablicy.

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

Wyjście:

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

Chcesz nauczyć się C++?

C++ należy do najpopularniejszych języków programowania. Możesz używać C++ do podstawowego programowania, tworzenia gier, tworzenia aplikacji opartych na graficznym interfejsie użytkownika, tworzenia oprogramowania bazodanowego, tworzenia systemów operacyjnych i wielu innych.

Jeśli jesteś początkującym językiem C++ lub chcesz zrewidować swoje koncepcje C++, sprawdź niektóre z najlepszych stron internetowych i kursów, aby zacząć.

Udział Udział Ćwierkać E-mail Jak nauczyć się programowania w C++: 6 stron na początek

Chcesz nauczyć się C++? Oto najlepsze strony internetowe i kursy online C++ dla początkujących i doświadczonych programistów.

Czytaj dalej
Powiązane tematy
  • Programowanie
  • JavaScript
  • Pyton
  • Poradniki kodowania
O autorze Yuvraj Chandra(60 opublikowanych artykułów)

Yuvraj jest studentem informatyki na Uniwersytecie w Delhi w Indiach. Jest pasjonatem Full Stack Web Development. Kiedy nie pisze, bada głębię różnych technologii.

Więcej od Yuvraja Chandra

Zapisz się do naszego newslettera

Dołącz do naszego newslettera, aby otrzymywać porady techniczne, recenzje, bezpłatne e-booki i ekskluzywne oferty!

Kliknij tutaj, aby zasubskrybować