Program in C++ to find the sum of all array elements using function
#include<iostream> #include<iomanip> #include<conio.h> #define size 30 using namespace std; int Arraysum(int *arr,int); int main() { int a[size],n; cout<<" Enter the size of array:-"; cin>>n; if(n>size) { cout<<" Invalid array size."; return 0; } else { cout<<" Enter "<<n<<" elements :-"; for(int i=0;i<n;i++) cin>>a[i]; int sum=0; sum=Arraysum(a,n); cout<<" sum of all elements = "<<sum; } } int Arraysum(int *arr,int n1) { int sum=0; for(int i=0;i<n1;i++) { sum+=arr[i]; //*(i+arr); //*(arr+i); //i[arr]; } return sum; }Output:-
0 Comments