C++ problem | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 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

24th Mar 2022, 3:11 PM
Oliver Pasaribu
Oliver Pasaribu - avatar
11 Respuestas
+ 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...
25th Mar 2022, 12:24 PM
Jayakrishna 🇮🇳
+ 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:)
26th Mar 2022, 8:57 AM
Oliver Pasaribu
Oliver Pasaribu - avatar
+ 1
Oliver Pasaribu *ptr=*(&a) = a = 0 0 == false hence answer = 9
26th Mar 2022, 1:31 PM
Sanjay Kamath
Sanjay Kamath - avatar
+ 1
Exactly:)
26th Mar 2022, 2:27 PM
Oliver Pasaribu
Oliver Pasaribu - avatar
0
Where you found this? Answer must be "9"
24th Mar 2022, 3:21 PM
Jayakrishna 🇮🇳
0
This is a example of c++ code challenge from sololearn:)
24th Mar 2022, 3:28 PM
Oliver Pasaribu
Oliver Pasaribu - avatar
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?
24th Mar 2022, 3:53 PM
Jayakrishna 🇮🇳
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.
24th Mar 2022, 9:39 PM
Oliver Pasaribu
Oliver Pasaribu - avatar
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..
24th Mar 2022, 9:51 PM
Jayakrishna 🇮🇳
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.
24th Mar 2022, 10:11 PM
Oliver Pasaribu
Oliver Pasaribu - avatar
0
Jayakrishna🇮🇳 Thank you Jaykrishna Sir
27th Mar 2022, 4:39 AM
Sanjay Kamath
Sanjay Kamath - avatar