0

this is the code which I tried

int main(){

int n;

cout<<"Type the number of elements\n";
cin>>n;

cout<<"Type the integars";
int arr[n];

for(int i=0;i<n;i++){

    cin>>arr[i];

}
cout<<"This is the reversed array ";

for(int i=n;i>=0;i--){

    cout<<arr[i]<<" ";
}

return 0;
}

this is the output pls explain why is 0 coming where did I go wrong?

enter image description here

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • 1
    You read n integers in the first for-loop but you try to print out n+1 integers in the second. Start at int i = n-1 in your second for-loop and you're good. – Padmina Apr 11 '21 at 12:34
  • The final loop n = 5, 4, 3, 2, 1, 0 - 6 iterations for an array of length 5. `arr[5]` is out-of-bounds. Start with `int i = n - 1` – Clifford Apr 11 '21 at 12:35
  • 1
    Even if you fix the off by one error, [VLAs are not standard](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) so your code isn't actually valid. Please take a look at `std::vector`. – cigien Apr 11 '21 at 12:35
  • Change your starting point to `int i = n - 1` while printing reversed array – Shailesh Apr 11 '21 at 12:36

5 Answers5

1

The problem is that your reversed array starts at n, and arr[n] is invalid, because the array starts at 0.

for (int i = n - 1; i >= 0; i--) {
    cout << arr[i] << " ";
}
Liyara
  • 33
  • 6
1

Your array is of size 5, but you're accessing 6 elements instead 5. That is, you are accessing elements 5...0 (6 in total) when the array only has 4...0 (5 in total) elements.

Start the 2nd for loop with n-1 and that should solve your issue. That is, (for(int i=n-1;i>=0;i--)) instead of (for(int i=n;i>=0;i--))

Sanjay
  • 11
  • 2
0

Solution: To print use i = n - 1

int main(){

int n;

cout<<"Type the number of elements\n";
cin>>n;

cout<<"Type the integars";
int arr[n];

for(int i=0;i<n;i++){

    cin>>arr[i];

}
cout<<"This is the reversed array ";

for(int i=n-1;i>=0;i--){

    cout<<arr[i]<<" ";
}

return 0;
}
0

Arrays begin counting at zero through to n (n being the target size value), so you need to ignore the zero value and start printing after zero has been iterated through.

Something like:

counter = target -1
Ada Lovelace
  • 156
  • 9
0

When printing the reverse of array make sure you remember the position of elements is always one less. So you have to start with n-1. '0' is getting printed because it is the garbage value at that position it could have been any number or value.

int main(){

int n;

cout<<"Type the number of elements\n";
cin>>n;

cout<<"Type the integars";
int arr[n];

for(int i=0;i<n;i++){

    cin>>arr[i];

}
cout<<"This is the reversed array ";


// When printing the reverse of array make sure you remember the position of elements is always one less.
// So you have to start with n-1. '0' is getting printed because it is the garbage value at that position it could have been any number or value.

for(int i=n-1;i>=0;i--){  

    cout<<arr[i]<<" ";
}

return 0;
}