1// Program to calculate the sum of array elements by passing to a function
2
3#include <stdio.h>
4float calculateSum(float age[]);
5
6int main() {
7 float result, age[] = {23.4, 55, 22.6, 3, 40.5, 18};
8
9 // age array is passed to calculateSum()
10 result = calculateSum(age);
11 printf("Result = %.2f", result);
12 return 0;
13}
14
15float calculateSum(float age[]) {
16
17 float sum = 0.0;
18
19 for (int i = 0; i < 6; ++i) {
20 sum += age[i];
21 }
22
23 return sum;
24}
1#include <iostream>
2using namespace std;
3
4// function declaration:
5double getAverage(int arr[], int size);
6
7int main () {
8 // an int array with 5 elements.
9 int balance[5] = {1000, 2, 3, 17, 50};
10 double avg;
11
12 // pass pointer to the array as an argument.
13 avg = getAverage( balance, 5 ) ;
14
15 // output the returned value
16 cout << "Average value is: " << avg << endl;
17
18 return 0;
19}