Click here to Skip to main content
15,846,346 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
class Test
{
 int KT(int a)
 {
   if (a > 0) return 0;
   else return a + NT(ref a); //-5 + nt
 }

 int NT(ref int a)
 {
   if (a < 0) return a++; //-5+1 = -4
   else return a * a;
 }

 static void Main()
 {
   Test Obj = new Test();
   Console.WriteLine(Obj.KT(-5));
 }
}

Why answer is 10??

What I have tried:

Why answer is 10?
What is Explain how the code works
Posted
Updated 19-Sep-22 0:17am
v3

First off, what OriginalGriff stated above in the Solution 1 is very good advice.
Use the debugger! It is your greatest coding tool!
Take it from someone who has worked on systems without one!

Here's a hint for you. Pay special attention to the first line in the method NT.
You need to answer the question: What exactly will "return a++;" do?

Review how the postfix operator++ works.

BTW, the output result is NOT 10 as you state. Look a little closer.
 
Share this answer
 
Comments
OriginalGriff 19-Sep-22 0:34am    
Take a five!
Malek Alshorbaji 19-Sep-22 16:32pm    
The answer in virtual studio is -10
FreedMalloc 19-Sep-22 16:45pm    
Yes, it's -10. Do you understand why it's -10 and not -9?
If not, read the link in Solution 3 posted by OriginalGriff.
The postfix and prefix operators can be a bit tricky sometimes.
To add to what FreedMalloc has rightly said, I'll add this "solution" to get the link to you.

Have a look here: Why Does x = ++x + x++ Give Me the Wrong Answer?[^] - it explains what postfix operations do, and why you have to be very careful with them.
 
Share this answer
 
Comments
FreedMalloc 19-Sep-22 0:47am    
Yes. I couldn't remember the name of this article. I should have have thought to peruse your list. It too is worth a 5.
Do you have any idea how much work explaining code line by line is?
Every single line needs a paragraph of explanation! For example:
int next = r.Next();

Create a new variable called "next" which can hold a integer value. From the previously declared Random instance "r", call the "Next" method to get a new random number, and assign it to the "next" variable.

Can you imagine how long it would take us to explain even a very short code fragment like your example, line by line?

No. It is not going to happen. If you have a specific problem, then ask a question about it. But think first - would you want to sit down for 45 minutes and type up a line-by-line description for no good reason?

You don't understand how code you didn't write works? use the debugger and watch what happens while it is running.
 
Share this answer
 

With the Postfix and Prefix operators the sequence of operations is the same apart from the returned value.

C#
//++x
temp=x;
incrementedVal=temp+1;
x=incrementedVal;
//return undated value
return incrementedVal;

//x++
temp=x;
incrementedVal=temp+1;
x=incrementedVal;
//return original value
return temp;

So the original value of a is returned by your NT method.
The reason that int a does not get updated is because it is passed to your method by value. For it to be updated, pass it by reference. Something like.
C#
public int KT(ref int a)
 {
   //use the Ternary operator to avoid 'if' 'else'
   return a > 0 ? 0 : a + NT(ref a);
 }
 //use expression syntax to get the definition down to a single line
 //public int KT(ref int a) => a > 0 ? 0 : a + NT(ref a);

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900