Pass Array to Function
C++
Medium
4 views
Problem Description
Pass array to function and find sum of all elements.
Real Life: Like sending list of items to calculate total bill.
Step-by-Step Logic:
1. Function receives array and its size
2. Loop through array
3. Add all elements
4. Return total sum
Official Solution
int arraySum(int arr[], int size) {
int total = 0;
for(int i = 0; i < size; i++) {
total += arr[i];
}
return total;
}
void function_q8_array_parameter() {
int numbers[] = {10, 20, 30, 40, 50};
int size = 5;
int sum = arraySum(numbers, size);
cout << "Sum of array elements: " << sum << endl;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!