+ 1

can somebody please explain me this code

#include<iostream> using namespace std; int add(int a[],int b) { int sum=0; for(int i=0;i<b;i++){ sum+=a[i]; ++a[i]; } return sum; } int main(){ int arr[2][3]={{1,0,1},{1,2,3}}; int s=3; cout<<"first "<<add(arr[0],s); cout<<" second "<<add(arr[1],s); }

27th Dec 2018, 10:59 PM
Ahmad Sheikh
Ahmad Sheikh - avatar
4 Réponses
+ 1
It counts the sum of the sub array. The function add has 2 params, a as array, b as array length to be counted.
28th Dec 2018, 12:15 AM
GiveAway
GiveAway - avatar
+ 1
1+0+1 = 2 1+2+3 = 6
28th Dec 2018, 1:02 AM
GiveAway
GiveAway - avatar
0
Can you explain why it outputs 2 for first and 6 for second
28th Dec 2018, 12:17 AM
Ahmad Sheikh
Ahmad Sheikh - avatar
0
I figured that out too but how does the code functions on the function call that is where i am stuck
28th Dec 2018, 1:04 AM
Ahmad Sheikh
Ahmad Sheikh - avatar