reverse | Sololearn: Learn to code for FREE!

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?

4/23/2021 8:58:05 PM

Ramisa Fariha

6 Answers

New Answer

+6

There is nothing to be sorry for, buddy! happy learning ☺️

+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.

+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.

+1

yes yes sorry got it now

+1

ok thank you