Arrays Introduction

Sort by

recency

|

1365 Discussions

|

  • + 0 comments

    The “C++ Introduction: Arrays” section on HackerRank is a great starting point for beginners learning how arrays work in C++. Radhe Exch Login xyz

  • + 0 comments

    Arrays allow you to store multiple values of the same type in a single variable, making it easier to manage collections of data! Funinmatch360 com mobile

  • + 0 comments

    Easy solution

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

    Reading input and writing output can honestly be done either with cin / cout or scanf / printf. Coming from a MATLAB / Python / C background, I personally prefer using printf than cout. Regardless, this two-pass solution is relatively easy

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    
    int main() {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
         
        int n ;
        scanf("%d", &n);
        int arr[n];
        
        for (int i=0; i <=n ; i++) {
            scanf("%d", &arr[i]);  
        }
        
        for (int i = n-1; i >=0 ; i--) {
            printf("%d ", arr[i] );    
        }
        
        return 0;
    	}
    

    Although it really bothers me that it throws this warning :

    Variable length arrays in C++ are a Clang extensionclang(-Wvla-cxx-extension) Solution.cpp(14, 13): Read of non-const variable 'n' is not allowed in a constant expression.

    It still compiles and works, but I think that exercises shouldn't throw warnings when a correct solution is written....

    }

  • + 0 comments

    The code asks for an array, but you can't initialise and array with a non-const integer. The exercise should either explicitly state you can use a vector object (as you would), or if an array is required that it should be created on the heap so it's possible.