วิธีหาผลรวมขององค์ประกอบทั้งหมดในอาร์เรย์

วิธีหาผลรวมขององค์ประกอบทั้งหมดในอาร์เรย์

อาร์เรย์คือชุดขององค์ประกอบที่จัดเก็บไว้ในตำแหน่งหน่วยความจำที่อยู่ติดกัน เป็นโครงสร้างข้อมูลที่ใช้มากที่สุดในการเขียนโปรแกรม ในบทความนี้ คุณจะได้เรียนรู้วิธีค้นหาผลรวมขององค์ประกอบทั้งหมดในอาร์เรย์โดยใช้ C++, Python และ JavaScript





คำชี้แจงปัญหา

คุณได้รับอาร์เรย์ของตัวเลข และคุณต้องคำนวณและพิมพ์ผลรวมขององค์ประกอบทั้งหมดในอาร์เรย์ที่กำหนด





ตัวอย่างที่ 1 : ให้ arr = [1, 2, 3, 4, 5]





ดังนั้น ผลรวมขององค์ประกอบทั้งหมดของอาร์เรย์ = 1 + 2 + 3 + 4 + 5 = 15

ดังนั้นเอาต์พุตคือ 15



ตัวอย่างที่ 2 : ให้ arr = [34, 56, 10, -2, 5, 99]

ดังนั้น ผลรวมของอิลิเมนต์ทั้งหมดของอาร์เรย์ = 34 + 56 + 10 + (-2) + 5 + 99 = 202





ดังนั้นเอาต์พุตคือ 202

วิธีการหาผลรวมขององค์ประกอบทั้งหมดในอาร์เรย์

คุณสามารถค้นหาผลรวมขององค์ประกอบทั้งหมดในอาร์เรย์โดยทำตามวิธีการด้านล่าง:





วิธีลบอื่นๆ บน iphone
  1. เริ่มต้นตัวแปร ผลรวม เพื่อเก็บผลรวมขององค์ประกอบทั้งหมดของอาร์เรย์
  2. สำรวจอาร์เรย์และเพิ่มแต่ละองค์ประกอบของอาร์เรย์ด้วย ผลรวม ตัวแปร.
  3. สุดท้ายคืน ผลรวม ตัวแปร.

โปรแกรม C++ เพื่อค้นหาผลรวมขององค์ประกอบทั้งหมดในอาร์เรย์

ด้านล่างนี้คือโปรแกรม C++ เพื่อค้นหาผลรวมขององค์ประกอบทั้งหมดในอาร์เรย์:

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

เอาท์พุท:

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++ ใช้ STL เพื่อค้นหาผลรวมขององค์ประกอบทั้งหมดในอาร์เรย์

คุณยังสามารถใช้ C++ STL เพื่อค้นหาผลรวมขององค์ประกอบทั้งหมดในอาร์เรย์

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

ที่เกี่ยวข้อง: คู่มือสำหรับผู้เริ่มต้นใช้งานไลบรารีเทมเพลตมาตรฐานใน C++

คุณเห็นไหมว่าใครติดตามคุณบน facebook

เอาท์พุท:

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 หาผลรวมขององค์ประกอบทั้งหมดในอาร์เรย์

ด้านล่างนี้คือโปรแกรม Python เพื่อค้นหาผลรวมขององค์ประกอบทั้งหมดในอาร์เรย์:

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

เอาท์พุท:

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 เหมาะสำหรับผู้เริ่มต้น

โปรแกรม Python ใช้ฟังก์ชันในตัวเพื่อค้นหาผลรวมขององค์ประกอบทั้งหมดในอาร์เรย์

คุณยังสามารถใช้ Python's ผลรวม () ฟังก์ชันหาผลรวมขององค์ประกอบทั้งหมดในอาร์เรย์

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

เอาท์พุท:

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 เพื่อค้นหาผลรวมขององค์ประกอบทั้งหมดในอาร์เรย์

ด้านล่างคือ JavaScript โปรแกรมหาผลรวมขององค์ประกอบทั้งหมดในอาร์เรย์:

วิธีเพิ่มความเร็วการใช้ดิสก์
// 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) + '
');

เอาท์พุท:

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

ที่เกี่ยวข้อง: วิธีสร้างเครื่องคิดเลขอย่างง่ายโดยใช้ HTML, CSS และ JavaScript

โปรแกรม JavaScript ใช้ reduce() เพื่อค้นหาผลรวมขององค์ประกอบทั้งหมดใน Array

คุณยังสามารถใช้ JavaScript's ลด() วิธีหาผลรวมขององค์ประกอบทั้งหมดในอาร์เรย์

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

เอาท์พุท:

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 ++ หรือไม่

C ++ เป็นหนึ่งในภาษาโปรแกรมที่ได้รับความนิยมมากที่สุด คุณสามารถใช้ C++ สำหรับการเขียนโปรแกรมพื้นฐาน, การพัฒนาเกม, การพัฒนาแอพพลิเคชั่นบน GUI, การพัฒนาซอฟต์แวร์ฐานข้อมูล, การพัฒนาระบบปฏิบัติการ และอื่นๆ อีกมากมาย

หากคุณเพิ่งเริ่มใช้ C++ หรือต้องการแก้ไขแนวคิด C++ ให้ตรวจสอบเว็บไซต์และหลักสูตรชั้นนำบางส่วนเพื่อเริ่มต้นใช้งาน

แบ่งปัน แบ่งปัน ทวีต อีเมล วิธีเรียนรู้การเขียนโปรแกรม C++: 6 ไซต์เพื่อเริ่มต้น

ต้องการเรียนรู้ C ++ หรือไม่? นี่คือเว็บไซต์และหลักสูตรออนไลน์ที่ดีที่สุดสำหรับ C ++ สำหรับผู้เริ่มต้นและโปรแกรมเมอร์ที่มีประสบการณ์เหมือนกัน

อ่านต่อไป
หัวข้อที่เกี่ยวข้อง
  • การเขียนโปรแกรม
  • JavaScript
  • Python
  • บทเรียนการเข้ารหัส
เกี่ยวกับผู้เขียน ยุวราช จันทรา(60 บทความที่ตีพิมพ์)

Yuvraj เป็นนักศึกษาระดับปริญญาตรีสาขาวิทยาการคอมพิวเตอร์ที่มหาวิทยาลัยเดลี ประเทศอินเดีย เขาหลงใหลเกี่ยวกับ Full Stack Web Development เมื่อไม่ได้เขียน เขากำลังสำรวจความลึกของเทคโนโลยีต่างๆ

เพิ่มเติมจาก Yuvraj Chandra

สมัครรับจดหมายข่าวของเรา

เข้าร่วมจดหมายข่าวของเราสำหรับเคล็ดลับทางเทคนิค บทวิจารณ์ eBook ฟรี และดีลพิเศษ!

คลิกที่นี่เพื่อสมัครสมาชิก