Taulukko on kokoelma elementtejä, jotka on tallennettu viereisiin muistipaikkoihin. Se on eniten käytetty tietorakenne ohjelmoinnissa. Tässä artikkelissa opit löytämään taulukon kaikkien elementtien summan C ++: n, Pythonin ja JavaScriptin avulla.
Ongelmailmoitus
Sinulle annetaan joukko numeroita, ja sinun on laskettava ja tulostettava annetun taulukon kaikkien elementtien summa.
Esimerkki 1 : Olkoon arr = [1, 2, 3, 4, 5]
Siksi taulukon kaikkien elementtien summa = 1 + 2 + 3 + 4 + 5 = 15.
Lähtö on siis 15.
Esimerkki 2 : Olkoon arr = [34, 56, 10, -2, 5, 99]
Siksi taulukon kaikkien elementtien summa = 34 + 56 + 10 + (-2) + 5 + 99 = 202.
Tulos on siis 202.
Lähestymistapa löytää kaikkien matriisin elementtien summa
Löydät taulukon kaikkien elementtien summan noudattamalla seuraavaa lähestymistapaa:
iphone 12 pro vs samsung s21 ultra
- Alusta muuttuja summa tallentaaksesi taulukon kaikkien elementtien kokonaissumman.
- Kierrä matriisi ja lisää taulukon jokainen elementti summa muuttuja.
- Palauta lopuksi summa muuttuja.
C ++ -ohjelma löytää kaikkien matriisin elementtien summan
Alla on C ++ -ohjelma, joka löytää taulukon kaikkien elementtien summan:
// 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;
}
Lähtö:
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 ++ -ohjelma STL: n avulla taulukon kaikkien elementtien summan löytämiseksi
Voit myös etsiä taulukon kaikkien elementtien summan C ++ STL: n avulla.
// 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;
}
Aiheeseen liittyvää: Aloittelijan opas vakiomallikirjastoon C ++: ssa
valitettavasti prosessi android -prosessi acore on pysähtynyt
Lähtö:
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 -ohjelma löytää kaikkien matriisin elementtien summan
Alla on Python -ohjelma, joka löytää taulukon kaikkien elementtien summan:
# 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))
Lähtö:
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
Aiheeseen liittyviä: Python -projektiideat sopivat aloittelijoille
Python-ohjelma sisäänrakennetun toiminnon avulla löytääksesi taulukon kaikkien elementtien summan
Voit myös käyttää Python'sia summa() funktio löytääksesi taulukon kaikkien elementtien summan.
# 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))
Lähtö:
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 -ohjelma löytää kaikkien matriisin elementtien summan
Alla on JavaScript ohjelma löytääksesi taulukon kaikkien elementtien summan:
kuinka vanha on MacBook Airini
// 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) + '
');
Lähtö:
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
Aiheeseen liittyviä: Yksinkertaisen laskimen rakentaminen HTML-, CSS- ja JavaScript -tiedostojen avulla
JavaScript -ohjelma Reduce () -menetelmän avulla löytää taulukon kaikkien elementtien summa
Voit käyttää myös JavaScriptiä vähentää() tapa löytää taulukon kaikkien elementtien summa.
// 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 + '
');
Lähtö:
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
Haluatko oppia C ++?
C ++ on suosituimpia ohjelmointikieliä. Voit käyttää C ++: ta perusohjelmointiin, pelien kehittämiseen, GUI-pohjaisten sovellusten kehittämiseen, tietokantaohjelmistojen kehittämiseen, käyttöjärjestelmien kehittämiseen ja paljon muuta.
Jos olet aloittelija C ++: ssa tai haluat tarkistaa C ++ -käsitteesi, tutustu alkuun tuleviin verkkosivustoihin ja kursseihin.
Jaa Jaa Tweet Sähköposti Kuinka oppia C ++ -ohjelmointi: 6 sivustoa alkuunHaluatko oppia C ++? Tässä ovat parhaat C ++ -verkkosivustot ja verkkokurssit aloittelijoille ja kokeneille ohjelmoijille.
Lue seuraava Liittyvät aiheet- Ohjelmointi
- JavaScript
- Python
- Koodausoppaat
Yuvraj on tietojenkäsittelytieteen perusopiskelija Delhin yliopistossa Intiassa. Hän on intohimoinen Full Stack Web -kehityksestä. Kun hän ei kirjoita, hän tutkii eri tekniikoiden syvyyttä.
Lisää Yuvraj Chandraltatilaa uutiskirjeemme
Liity uutiskirjeeseemme saadaksesi teknisiä vinkkejä, arvosteluja, ilmaisia e -kirjoja ja ainutlaatuisia tarjouksia!
Klikkaa tästä tilataksesi