here we are passing array a without refrence then why would original value of array change
Doubt in question 5
Find the output of the following code void changing(int a[],int size) { for(i=0;i<size();i++) a[i]=a[i]+1; } void change(vector v) { for(i=0;i<v.size();i++) v[i]=v[i]+1; } int main() { int a[3]={0,1,2}; vector v; for(i=0;i<3;i++) v.push_back(i); changing(a,3); change(v); for(i=0;i<3;i++) cout<<a[i]<<” ”; for(i=0;i<3;i++) cout<<v[i]<<” ”; }
see array (int a[]) is always passed by pointer.
i.e only base address of array is passed. due to which whatever changes we will do inside function on array it will happen on the same original array.
in case of vector it is by default passed by value .
but if we want them to pass by refrence then we need to use & in function declartion
doubt in Find the time complexity of the following Code : int gcd(int a,int b) { if(b==0) return a; else Return gcd(b,a%b); }
some other ta is taking this doubt, pls wait for his response.
if this doubt is clear then pls mark it as resolved