Question-:PERMUT2 Problem - CodeChef
code-:CodeChef: Practical coding for everyone
Pay attention to compiler warnings!
[simon@simon-laptop][06:59:48]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh
Compiling jai02-PERMUT2.cpp
+ g++ -std=c++14 jai02-PERMUT2.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
jai02-PERMUT2.cpp: In function ‘int main()’:
jai02-PERMUT2.cpp:7:8: warning: unused variable ‘temp’ [-Wunused-variable]
int T,temp,i,k;
^~~~
jai02-PERMUT2.cpp:7:15: warning: unused variable ‘k’ [-Wunused-variable]
int T,temp,i,k;
^
jai02-PERMUT2.cpp:8:9: warning: ‘T’ is used uninitialized in this function [-Wuninitialized]
while(T!=0){
~^~~
+ set +x
Successful
Still I am getting error
Post a link to your updated code.
remove k,temp from initialisationsince it is not used
and initialise t with 0
The compiler warning remains, and gives a huge clue as to what is wrong:
[simon@simon-laptop][07:05:18]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh
Compiling jai02-PERMUT2.cpp
+ g++ -std=c++14 jai02-PERMUT2.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
jai02-PERMUT2.cpp: In function ‘int main()’:
jai02-PERMUT2.cpp:8:9: warning: ‘T’ is used uninitialized in this function [-Wuninitialized]
while(T!=0){
~^~~
+ set +x
Successful
initialize T with 0
That’ll remove the warning, but won’t fix the actual problem that the compiler is hinting at.
Phew
Out-of-bounds accesses with the sample test input:
[simon@simon-laptop][07:12:49]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh
Compiling jai02-PERMUT2.cpp
+ g++ -std=c++14 jai02-PERMUT2.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
+ set +x
Successful
[simon@simon-laptop][07:13:05]
[~/tmp/DONTSYNC/hackerrank/otherpeoples]>echo "4
1 4 3 2
5
2 3 4 5 1
1
1
0" | ./a.out
jai02-PERMUT2.cpp:22:18: runtime error: index 4 out of bounds for type 'int [*]'
jai02-PERMUT2.cpp:22:17: runtime error: index 4 out of bounds for type 'int [*]'
not ambiguous
jai02-PERMUT2.cpp:22:17: runtime error: index 1 out of bounds for type 'int [*]'
jai02-PERMUT2.cpp:22:18: runtime error: index 1 out of bounds for type 'int [*]'
and it only prints out one value.
@jai02 bro u have not initiaqlisted the variable ‘t’ so u cannot do operation while(T!=0),there might be some garbage value int he variable T.First initialise T with 0.
The input format for this Problem uses some concepts you’re probably not familiar with, yet - I’d recommend looking at some of the AC C++ solutions for it. Here’s mine.
ok…thanks
Sir, I just changed the value of the flag and I got the solution-:CodeChef: Practical coding for everyone
can you explain how?
https://www.codechef.com/viewsolution/46624442
see the comments in my solution then you will get why is it working
even in your accepted program arr size should be T+1
it got AC instead it should throw exception of array out of bound .
but still it got accepted ,
How ?
reason - to be ambiguous one element should be not at its required position if it it it is not it automatically changes the position of one more that’s why it got AC .
But still That - array out of bound
ok thanks