c++ - How to use array address to reverse an array? -
i understand array address code found in book made me nut.i understand recursive function did not one.here code:
int main(){ const int arraysize = 5; int a[arraysize] = { 32, 27, 64, 18, 95}; cout << "the values in reverse array are:" << endl; somefunction(a, arraysize); cout << endl; cin.get(); return 0; } void somefunction(int b[], int size) { if (size > 0) { somefunction(&b[1], size - 1); cout << b[0] << " "; } }
i got code in exercise.my question how reversing array?i happy if explain bit more.thanks
here pseudo-code shows how recusive calls somefunction
made, , in order:
somefunction( { 32, 27, 64, 18, 95} , 5) somefunction( { 27, 64, 18, 95}, 4) somefunction( { 64, 18, 95}, 3) somefunction( { 18, 95}, 2) somefunction( { 95}, 1) somefunction( { }, 0)
somefunction({ }, 0) return without doing because there nothing left in array. somefunction
print first element b[0]
of arrays comes out of recursion, beginning array containing 1 item {95}:
{ 95 } { 18, 95} { 64, 18, 95} { 27, 64, 18, 95} { 32, 27, 64, 18, 95}
so output be:
"the values in reverse array are:" 95 18 64 27 32
Comments
Post a Comment