Arrays Introduction

  • + 0 comments

    I though Variable Length Arrays (VLAs) where not supported by C++, and my compiler thinks so too. I had to use vectors,

    #include <cmath>
    #include <vector>
    #include <iostream>
    using namespace std;
    
    
    int main() {
        int n{};
        cin >> n;
        
        if (n < 1 || n > 1000) return 1;
        
        vector<int> vec(n);
        
        // Read array.
        for (int i = 1; i < n + 1; i++) 
        {
            // Load input back to front so its reversed.
            cin >> vec[n - i];    
                 
            // Validate.
            if (vec[n - i] < 1 || vec[n - i] > 10000) return 1; 
        }
        
        
        // Output array.
        for (int i = 0; i < n; i++)
        {
            cout << vec[i] << " ";
        }
    		
    		
        
        return 0;
    }