Add a method named sumArray to your last program that takes an array as a parameter and returns the sum of its values.
in python
Answers
➡️ Input : arr[] = {1, 2, 3}
Output : 6
1 + 2 + 3 = 6
Input : arr[] = {15, 12, 13, 10}
Output : 50
is your answer ❤️
Answer -
/* C++ Program to find sum of elements
in a given array */
#include <bits/stdc++.h>
using namespace std;
// function to return sum of elements
// in an array of size n
int sum(int arr[], int n)
{
int sum = 0; // initialize sum
// Iterate through all elements
// and add them to sum
for (int i = 0; i < n; i++)
sum += arr[i];
return sum;
}
// Driver code
int main()
{
int arr[] = {12, 3, 4, 15};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Sum of given array is " << sum(arr, n);
return 0;
}
// This code is contributed by rathbhupendra
→ Hope it will help you ❣️❣️
→ Please mark as Breinliest ❤️❤️