+ 1
Trying to reverse an array.. Can u ppl help me figure out the prblm..?? Its not giving crrct results
#include<iostream> using namespace std; int main() {int a[5],b[5]; int i=0,j=4; cout<<"enter elements"; for(i=0;i<5;i++) {cin>>a[i];} while(i<5&&j>=0) {b[j]=a[i]; j--; i++; } for(i=0;i<5;i++) cout<<b[i]; return 0; }
1 ответ
+ 1
Try resetting the variable i to zero after the first for loop because it is be equal to 5 before the while loop is initiated.
Otherwise you could consolidate the first for loop and the while loop by doing something like:
int a[5], b[5];
int j = 4;
cout << "enter elements: " << endl;
for (int i = 0; i < 5; i++) {
cin >> a[i];
b[j] = a[i];
j--;
}