0

Runs on other online ide but doesn't run on CodeChef ide. I don't understand why this is happening. What I can do to run it on the CodeChef ide?

#include <bits/stdc++.h> 
#define REP(i,a,b) for(i=a;i<=b;i++) 

using namespace std; 

int main(){ 
ios::sync_with_stdio(0);
cin.tie(0);

int tt;
int array [tt];
int key;
int i;
int p;

cin >> tt;
REP(i,1,tt) cin >> array[i-1];

REP(i,1,tt-1){
    key = array[i];
    p = i-1;
    while(p>=0 && array[p]>key){
        array[p+1]=array[p];
        p--;
    }
    array[p+1] = key;
}

REP(i,1,tt) cout << array[i-1] << " ";

}
kc21
  • 1
  • 4
    `int array [tt];` is not standard C++ *and* it invokes *undefined behavior* because `tt` is not initialized (and every access to `array` after this invokes UB as well) – UnholySheep Dec 27 '19 at 10:12

1 Answers1

-1

No issues. Found my mistake. I should have used cin >> tt before declaring array. It was taking a garbage value I suppose and the array was going out of bounds I believe.

#include <bits/stdc++.h> 
#define REP(i,a,b) for(i=a;i<=b;i++) 

using namespace std; 



int main(){ 
ios::sync_with_stdio(0);
cin.tie(0);

int tt;
cin >> tt;

int array [tt];
int key;
int i;
int p;

REP(i,1,tt) cin >> array[i-1];

REP(i,1,tt-1){
    key = array[i];
    p = i-1;
    while(p>=0 && array[p]>key){
        array[p+1]=array[p];
        p--;
    }
    array[p+1] = key;
}

REP(i,1,tt) cout << array[i-1] << " ";

}
kc21
  • 1
  • 1
    This code is still full of *undefined behavior*. E.g.: every single loop utilizes an uninitialized `i` – UnholySheep Dec 27 '19 at 10:21
  • 1
    It was not taking a garbage value and going out-of-bounds. It was already [*undefined behavior*](https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior) to use the indeterminate value. – walnut Dec 27 '19 at 10:22
  • @UnholySheep ... Then what are the pointers you suggest so as to avoid the undefined behaviour in c++ as it basically is not caught by compilers many a times. – kc21 Dec 28 '19 at 12:01
  • @walnut ... Then what are the pointers you suggest so as to avoid the undefined behaviour in c++ as it basically is not caught by compilers many a times. – kc21 Dec 28 '19 at 12:01
  • 1. Always initialize your variables with a value (e.g.: `0` for integers) 2. Use `std::vector` instead of VLAs 3. Don't obfuscate your code with unnecessary macros like `#define REP(i,a,b) for(i=a;i<=b;i++) ` – UnholySheep Dec 28 '19 at 12:04
  • 1
    @UnholySheep Actually I think the `i` is initialized before use, although it is obfuscated. kc21: Even if you write code for competitive programming purposes, rewrite it in a readable way before posting it here (e.g. without hiding loops in `#define`s, etc.). And don't use this style if you want to actually learn programming. It applies only to these kind of programming competitions and is completely wrong for any productive programming task. Avoiding undefined behavior means not using raw arrays, etc, as UnholySheep wrote. – walnut Dec 28 '19 at 12:09