0

reverse

the question is: Write and test the following function: void reverse(int a[],int n); The function reverses the first n elements of the array. For example, the call reverse(a,5) would transform the array {22,33,44,55,66,77,88,99} into {66,55,44,33,22,77,88,99}. my code is: void reverse(int a[],int n); int main() { int a[8]={22,33,44,55,66,77,88,99}; cout<<"The array is: "<<endl; for(int i=0; i<8; i++) { cout<<a[i]<<","; } reverse(a,5); } void reverse(int a[],int n) { for(int i=0; i<n/2; i++) { swap(a[i],a[n-1-i]); } cout<<"\nAfter reverse(a,5):"; for(int i=0; i<n; i++) { cout<<a[i]<<","; } } why the rest not showing?

23rd Apr 2021, 8:58 PM
Ramisa Fariha
Ramisa Fariha - avatar
6 ответов
+ 6
There is nothing to be sorry for, buddy! happy learning ☺️
23rd Apr 2021, 9:04 PM
minirkk
minirkk - avatar
+ 4
replace reverse(a,5); with reverse(a,8); Since you are reversing the array you will be needing all the elements of the array and in your case it's 8 elements and not just 5 elements.
23rd Apr 2021, 9:02 PM
minirkk
minirkk - avatar
+ 2
Your function does exactly what it is supposed to do. The only problem is that your output loop doesn't output the whole array. It only goes to n, 5 in the example, but should go up to the length of the array, 8.
23rd Apr 2021, 10:54 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 1
yes yes sorry got it now
23rd Apr 2021, 9:03 PM
Ramisa Fariha
Ramisa Fariha - avatar
+ 1
ok thank you
24th Apr 2021, 6:29 AM
Ramisa Fariha
Ramisa Fariha - avatar