0

I created an array that takes a user input into an array and then reverses the order of the array however I am confused as to how the for loop functions to help

int length{};  
std::cin >> length;

int* array{ new int[length]{} }; 

for(int i=0; i<length; i++)
  cin>>array[i];

for (int i=length-1; i>=0; i--)  //where I need help understanding the mechanics
  cout<<array[i]<<" ";
            
return 0;
Mat
  • 202,337
  • 40
  • 393
  • 406
  • 1
    `for (int i=length-1;i>=0;i--)` -- What in the `for` loop syntax is not understandable? Do you understand this one: `for(int i=0; i – PaulMcKenzie Aug 01 '22 at 08:42
  • Hi Ishmael A, the first for loop inputs value into the array and the second for loop prints the array in reverse order. The array itself is not reversed. Hope it helps. – charlesdk Aug 01 '22 at 08:48
  • 1
    It doesn't reverse the order of the array, it prints the array elements in the reverse order. The array is unaffected by the second loop. – molbdnilo Aug 01 '22 at 09:08
  • the length of array is not the real length of it , for example when you input 5 for the length there is 0 to 4 arrays not 1-5 so if you want to access to the last element of array it is number 4 (as your loop wants to access last element ) in **int i = length -1** . – Mahdy.n Aug 17 '22 at 05:10

0 Answers0