Arrays Introduction

Sort by

recency

|

1345 Discussions

|

  • + 0 comments
    int main() {
        int n; cin >> n; 
        int *arr = new int[n]; // since cpp doent support VLAs sizing 
        for (int i = 0; i < n; i++) {
            cin >> arr[i]; 
        }
        for (int i = n - 1; i >= 0; i--) {
            cout << arr[i] << " "; 
        }
        delete [] arr; 
        return 0;
    }
    
  • + 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;
    }
    
  • + 0 comments

    Solving this problem helps build a solid foundation for understanding how arrays work. By finding the solution, you will enhance your learning experience on HackerRank. Fairplay24 pro register

  • + 0 comments

    include

    include

    include

    include

    include

    using namespace std;

    int main() { int n; cin>>n; vectorarr(n); for(int i=0;i>arr[i]; } for(int i=n-1;i>=0;i--){ cout<

  • + 0 comments
    int main() {
            int num;
            std::cin>>num;
            int arr[num];
            for(int i=0;i<num;i++){cin>>arr[i];}
            for(int i=num;i--;){cout<<arr[i]<<" ";}
            return 0;
    }