1

Please help me with this strange problem where the input value is given as 4 (i.e. n = 4) and after a for loop the same value is getting displayed as 2, but why? It was not used anywhere (AFAIK) and it shouldn't get changed (I have no clue about it).

The original problem on HackerRank.

MY CODE >>
#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;
    int arr[n];
    cin >> n; // input given from stdin is 4
    
    cout << n << "\n"; // outputs 4
    
    for(int i=0; i<n; i++){
        scanf("%d",&arr[i]);
    }
    
    cout << n << "\n"; // outputs 2 (but why?)
    
    for(int i=n-1; i>=0; i--){
        printf("%d ",arr[i]); // if it were n = 4, then i will go from 3 to 0, but it goes from 1 to 0 (as n=2, but again, why?)
    }
    return 0;
}

Thank you for any help!

Shubham Nanche
  • 130
  • 2
  • 11
  • 6
    `n` is uninitialized when you define `arr` so you have no idea what size the array is (and it is probably small since you are seeing overwriting). Define `arr` after you've read `n`. But you're using a C feature (VLA — variable-length array) in a C++ program, which is also not reliable. G++ allows it; there's a good chance Clang++ allows it; but it is not standard C++, AFAIK. – Jonathan Leffler Sep 30 '21 at 18:09
  • Please include any necessary information in the question itself as text, rather than relying on external links. In this case I don't think the context of the HackerRank problem ends up being that necessary to understand why your code is misbehaving, but still good question-asking practice. – Nathan Pierson Sep 30 '21 at 18:16
  • @JonathanLeffler Thank you so much. Yeah! That was the exact issue. I defined arr after I initialized n and it worked fine! – Shubham Nanche Sep 30 '21 at 18:16
  • @NathanPierson sure, next time I'll take care to be more precise on the question itself. – Shubham Nanche Sep 30 '21 at 18:19

1 Answers1

4
int n;
int arr[n];   // <<<<<< I magically know what n will be after the user types it!
cin >> n; // input given from stdin is 4

First of all, that's not even legal in C++. As a gcc extension to support C style VLA's, what is the value of n when the array declaration is seen? You have not read it yet!!


Instead, use:

int n;
cin >> n;
std::vector arr(n);

Although this still is not the "C++ way" as you are pre-defining the entire collection and then assigning to each element in turn; as opposed to adding each element to the collection. This is not a big deal with an int but more generally you don't want dead unused items in a collection; rather they simply don't exist at all.

std::vector arr;  // don't pre-allocate any size
for(int i=0; i<n; i++){
        int val;
        scanf("%d",&val);   //<<<   uhhhhhh.  you know about `cin` why this?
        arr.push_back(val);
    }
JDługosz
  • 5,592
  • 3
  • 24
  • 45
  • 1
    Thank you. I forgot about that actually. I am relatively new to C++ but I knew some C , so my bad :( But, thanks for that detailed explanation. – Shubham Nanche Sep 30 '21 at 18:24