Cách tìm tổng của tất cả các phần tử trong một mảng

Cách tìm tổng của tất cả các phần tử trong một mảng

Mảng là một tập hợp các phần tử được lưu trữ tại các vị trí bộ nhớ liền nhau. Đây là cấu trúc dữ liệu được sử dụng nhiều nhất trong lập trình. Trong bài viết này, bạn sẽ học cách tìm tổng của tất cả các phần tử trong một mảng bằng C ++, Python và JavaScript.





Báo cáo vấn đề

Bạn được cung cấp một mảng số và bạn cần tính toán và in ra tổng của tất cả các phần tử trong mảng đã cho.





ví dụ 1 : Cho arr = [1, 2, 3, 4, 5]





Do đó, tổng tất cả các phần tử của mảng = 1 + 2 + 3 + 4 + 5 = 15.

Như vậy, sản lượng là 15.



Ví dụ 2 : Cho arr = [34, 56, 10, -2, 5, 99]

Do đó, tổng tất cả các phần tử của mảng = 34 + 56 + 10 + (-2) + 5 + 99 = 202.





Như vậy, đầu ra là 202.

Phương pháp tiếp cận để tìm tổng của tất cả các phần tử trong một mảng

Bạn có thể tìm thấy tổng của tất cả các phần tử trong một mảng bằng cách làm theo cách tiếp cận bên dưới:





wps trên bộ định tuyến là gì
  1. Khởi tạo một biến Tổng để lưu trữ tổng của tất cả các phần tử của mảng.
  2. Duyệt qua mảng và thêm từng phần tử của mảng với Tổng Biến đổi.
  3. Cuối cùng, trả lại Tổng Biến đổi.

Chương trình C ++ để tìm tổng của tất cả các phần tử trong một mảng

Dưới đây là chương trình C ++ để tìm tổng của tất cả các phần tử trong một mảng:

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

Đầu ra:

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

Chương trình C ++ sử dụng STL để tìm tổng của tất cả các phần tử trong một mảng

Bạn cũng có thể sử dụng C ++ STL để tìm tổng của tất cả các phần tử trong một mảng.

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

Liên quan: Hướng dẫn cho Người mới bắt đầu về Thư viện Mẫu Chuẩn trong C ++

cách ghép nối từ xa que lửa

Đầu ra:

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

Chương trình Python để tìm tổng của tất cả các phần tử trong một mảng

Dưới đây là chương trình Python để tìm tổng của tất cả các phần tử trong một mảng:

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

Đầu ra:

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ó liên quan: Ý tưởng dự án Python phù hợp cho người mới bắt đầu

Chương trình Python sử dụng hàm tích hợp để tìm tổng của tất cả các phần tử trong một mảng

Bạn cũng có thể sử dụng Python's Tổng() hàm để tìm tổng của tất cả các phần tử trong một mảng.

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

Đầu ra:

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

Chương trình JavaScript để tìm tổng của tất cả các phần tử trong một mảng

Dưới đây là JavaScript chương trình để tìm tổng của tất cả các phần tử trong một mảng:

cách chạy tập lệnh python khi khởi động raspberry pi
// 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) + '
');

Đầu ra:

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

Liên quan: Cách tạo một máy tính đơn giản bằng HTML, CSS và JavaScript

Chương trình JavaScript Sử dụng phương thức Reduce () để tìm tổng của tất cả các phần tử trong một mảng

Bạn cũng có thể sử dụng JavaScript của giảm() phương pháp để tìm tổng của tất cả các phần tử trong một mảng.

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

Đầu ra:

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

Muốn học C ++?

C ++ là một trong những ngôn ngữ lập trình phổ biến nhất. Bạn có thể sử dụng C ++ để lập trình cơ bản, phát triển trò chơi, phát triển ứng dụng dựa trên GUI, phát triển phần mềm cơ sở dữ liệu, phát triển hệ điều hành, v.v.

Nếu bạn là người mới bắt đầu làm quen với C ++ hoặc muốn sửa đổi các khái niệm C ++ của mình, hãy xem một số trang web và khóa học hàng đầu để giúp bạn bắt đầu.

Đăng lại Đăng lại tiếng riu ríu E-mail Cách học lập trình C ++: 6 trang web để bắt đầu

Bạn muốn học C ++? Dưới đây là các trang web tốt nhất và các khóa học trực tuyến về C ++ dành cho người mới bắt đầu cũng như các lập trình viên có kinh nghiệm.

Đọc tiếp
Chủ đề liên quan
  • Lập trình
  • JavaScript
  • Python
  • Hướng dẫn viết mã
Giới thiệu về tác giả Yuvraj Chandra(60 bài báo đã xuất bản)

Yuvraj là sinh viên ngành Khoa học Máy tính tại Đại học Delhi, Ấn Độ. Anh ấy đam mê Phát triển Web Full Stack. Khi không viết, anh ấy đang khám phá chiều sâu của các công nghệ khác nhau.

Xem thêm từ Yuvraj Chandra

Theo dõi bản tin của chúng tôi

Tham gia bản tin của chúng tôi để biết các mẹo công nghệ, đánh giá, sách điện tử miễn phí và các ưu đãi độc quyền!

Bấm vào đây để đăng ký