SUM OF ALL ELEMENTS OF ARRAY
#include <iostream>
using namespace std;
int abc(int brr[], int n)
//abc is the name of function
{
int sum=0;
for(int i=0;
i<n; i++)
{
sum+=brr[i];
}
return sum;
}
int main()
{
int n = 10;
int arr[n]= {1,2,3,4,5,6,7,8,9,10};
cout<<"Sum is : "<<abc(arr,n)<<endl;
return 0;
}
AVERAGE
OF ALL ELEMENTS OF ARRAY
#include <iostream>
using namespace std;
double getAverage(int arr[], int size)
{
int i, sum = 0;
double avg;
for (i = 0; i <
size; i++) {
sum += arr[i];
}
avg = double(sum) /
size;
return avg;
}
int main () {
int balance[] = {1000,
2, 3, 17, 50};
cout <<
"Average value is: " <<
getAverage( balance, 5 ) <<
endl;
return 0;
}
Comments
Post a Comment