Can someone explain this?

closed account (EApGNwbp)
Can someone explain how the last for loop works which prints out the user's input backwards?? Thanks in advance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;

int main()
{
    
    
    const int SIZE = 10;
    int nums[SIZE];
   
    for(int i=0; i<SIZE; i++)
    {
        cout << "Please enter a number: ";
        cin >> nums[i];
    }
    
    // Print the numbers backwards
    
  
    
    for(int i=SIZE-1; i>=0; i--)
    {
        cout << nums[i] << " ";
    }
    
    cout << endl;
    
}
Last edited on
For the for loop, it will first start from i = 9, output the array nums at position 9. Then i will be decrease to 8, 7, ... all the way to 0.

For array, the index is always 0 instead of 1.

Hope this helps :)

More information can be found in http://www.cplusplus.com/doc/tutorial/arrays/
Topic archived. No new replies allowed.