Reversing The Array(Diffrent from "Printing Array in Reverse Order")
# include <iostream>
using namespace std ;
int main(){
int length_of_array ;
cout << "Enter the no. of Elements :" ;
cin >> length_of_array ;
int array[length_of_array];
for (int i = 0; i < length_of_array; i++) {
cin >> array[i];
}
cout << "Before Reversing : " << '\n' ;
for (int i = 0; i < length_of_array; i++) {
cout << array[i] << " " ;
}
cout << '\n' ;
int start{0};
int end{length_of_array - 1};
while (start <= end){
swap(array[start], array[end]) ;
start ++ ;
end -- ;
}
cout << "After Reversing : " << '\n' ;
for (int i = 0; i < length_of_array; ++i) {
cout << array[i] << " " ;
}
return 0 ;
}
Comments
Post a Comment