Advertisement
AshfaqFardin

Sum of array using pointer

Jul 31st, 2021
1,091
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.43 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void arraySum(int *arr, int size){
  6.     int sum = 0;
  7.     for(int i = 0; i < size; i++){
  8.         sum += arr[i];
  9.     }
  10.     cout << sum;
  11. }
  12.  
  13. int main()
  14. {
  15.     int size = 5;
  16.     int arr[] = {1, 2, 3, 4, 5};
  17.    
  18.     cout << "Sum of the elements: ";
  19.     arraySum(arr, size);
  20.     cout << endl;
  21.    
  22.     cout << "Sum of the elements: ";
  23.     arraySum(&arr[0], size);
  24.  
  25.     return 0;
  26. }
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement