+ 1
C++ problem
I've got this code or problem twice. At first time, I choose "9" as the right answer. But sololearn flagged my answer as a wrong answer. At the 2nd chance, I change my answer by choosing 0. But I wonder why sololearn still flagged my answer as a wrong answer? So what is the right answer? I also check the right answer by writing this code, and I have got the right answer is 9. https://code.sololearn.com/c2A98W89cfhs/?ref=app
11 Respostas
+ 1
Yes. You have clarity..
if(*ptr) there if(0) so condition become false, for any other value , it becomes true.
if(ptr) , if ptr==null then condition become false but any other value becomes true..
! means negation, revert value.
So you have clarity then if you are sure about wrong answer then take screen shot and send mail to report as wrong answer. Or there you can find 'report' option from menu, you can report there..
Hope it helps...
+ 1
Yes you right. Unfortunately I didn't take the screenshot at the first chance answer a= 0, so it evaluated as false and result is cout<<"9";. Because I thought that my answer is correct:)
+ 1
Oliver Pasaribu *ptr=*(&a) = a = 0
0 == false hence answer = 9
+ 1
Exactly:)
0
Where you found this?
Answer must be "9"
0
This is a example of c++ code challenge from sololearn:)
0
Answer must be "9"
Do you think it's a wrong answer in quiz? Can you show sreen shot?
There may something you missing?
0
The problem is not change but all the option is justified as wrong answer? But the latest the answer is 9 because we define int a= 0, int * ptr = &a.. If(*ptr) cout<<a else cout<<"9";. You know that if (*ptr) means if value store in the memory address *ptr pointed by int *ptr > 0 Will be evaluated as true while if if value store in the memory address *ptr pointed by int *ptr <= 0 will be evaluated as false statement.
0
Almost but anything other than zero, ( null, empty string) are evaluated as true (negetives also) .
So if it is
if (*ptr)
cout<<a;
else
cout<<"9";
Answer is "9"
But if it is
if (ptr)
cout<<a;
else
cout<<"9";
Answer is 0
Do you noticed difference?
I guess challenges have these both questions..
0
Sure if (*ptr) means if the value stored at address pointed by int * ptr is not 0 or !null.
If (ptr) means if int *ptr points to a specific location.
If (!ptr) means that if the int *ptr doesn't point' to other specific memory location.
This has similarities with Binary tree, innwhere each node has a right child node and left child node.
0
Jayakrishnađźđł Thank you Jaykrishna Sir