Can you share a concept of a function?
#include <iostream>
using namespace std;
int getSum(int arr[],int n){
int sum=0;
for(int i=0;i<n;i++){
sum+=arr[i];
}
return sum;
}
int main()
{
int n1=9;
int n2=9;
int array1[n1]={1,2,3,4,5,6,7,8,9};
int array2[n2]={10,20,30,40,50,60,70,80,90};
cout<<"\nThe sum of the elements in the first array is: "<<getSum(array1,n1);
cout<<"\nThe sum of the elements in the second array is: "<<getSum(array2,n2);
return 0;
}
Comments
Leave a comment