❔ Can anyone explain this piece of code
So I found a piece of code online that solves my problem, but I have to understand how it works before being able to modify it to fit my needs. Can anyone tell me what the FindCloseTriplet() function is actually doing? I found it confusing to read.
https://www.geeksforgeeks.org/find-a-triplet-in-an-array-whose-sum-is-closest-to-a-given-number/amp/?fbclid=IwAR30br4s6ta3oq9O1IwhSWCD6E25hzk1ulSUPfiAjQcSQvyIuJcXXlN2C10
7 Replies
have you tried it?
Yes I have, it works but I don't know what it's actually doing. The website displays the addition step of the triplets but I don't see anything being added in the code
And yes, I am very bad at C# but good enough to make games
If you are talking about the algorithm itself, I don't think I can help you because:
1) recursive functions are hard to track
2) I'm DUM
The "ref" keyword means the value is passed by reference: so when some recursive call stop at some point and start going backwards to the initial caller, the intermediate calculated values (with "ref" keyword ones) will be not the same, as they were before stepping into this recursive call. To illustrate, here's an example:
You're not a lone, we all struggle in c#
If you want to understand the algorithm, I can only suggest you to ask ChatGPT not to explain(as its makes mistakes often), but at least to format the code, so its easier to read. Here's formatted one:
Also, you can use the debugger in VS or some other IDE to track the intermediate values.
thank you for the explaination of ref, I still can't understand the code though. I'll keep trying
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.