C
C#17mo ago
EmperMiner

❔ 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
Omnissiah
Omnissiah17mo ago
have you tried it?
EmperMiner
EmperMinerOP17mo ago
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
benqbenq
benqbenq17mo ago
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:
int initialValueToCalculate = 1;
Example(0, ref initialValueToCalculate);
Console.WriteLine(initialValueToCalculate); // 8

static void Example(int currentIteration, ref int someValue)
{
// Stop on 3rd recursion call
if (currentIteration == 3)
{
return;
}

someValue *= 2; // Do calculations that will be shared in all recursion steps
Example(++currentIteration, ref someValue);
}
int initialValueToCalculate = 1;
Example(0, ref initialValueToCalculate);
Console.WriteLine(initialValueToCalculate); // 8

static void Example(int currentIteration, ref int someValue)
{
// Stop on 3rd recursion call
if (currentIteration == 3)
{
return;
}

someValue *= 2; // Do calculations that will be shared in all recursion steps
Example(++currentIteration, ref someValue);
}
The king of kings
You're not a lone, we all struggle in c#
benqbenq
benqbenq17mo ago
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:
int[] arr = new int[] { -1, 2, 1, -4 };
int target = 1;
int arrSize = arr.Length;
int minDiff = int.MaxValue;
int answer = 0;

FindClosestTriplet(
arr,
size: arrSize,
targetSum: target,
currentCount: 0,
currentSum: 0,
currentIndex: 0,
closestSum: ref answer,
minDifference: ref minDiff);

Console.WriteLine(answer);

static void FindClosestTriplet(int[] array, int size, int targetSum, int currentCount, int currentSum, int currentIndex, ref int closestSum, ref int minDifference)
{
// Return when reaching the end of the array
// If we have picked three elements so far, check if the sum is closest to our "targetSum"
if (currentIndex == size)
{
if (currentCount == 3)
{
int currentDifference = Math.Abs(targetSum - currentSum);
if (currentDifference < minDifference)
{
minDifference = currentDifference;
closestSum = currentSum;
}
}
return;
}

// Pick this number
FindClosestTriplet(array, size, targetSum, currentCount + 1, currentSum + array[currentIndex], currentIndex + 1, ref closestSum, ref minDifference);

// Don't pick this number
FindClosestTriplet(array, size, targetSum, currentCount, currentSum, currentIndex + 1, ref closestSum, ref minDifference);
}
int[] arr = new int[] { -1, 2, 1, -4 };
int target = 1;
int arrSize = arr.Length;
int minDiff = int.MaxValue;
int answer = 0;

FindClosestTriplet(
arr,
size: arrSize,
targetSum: target,
currentCount: 0,
currentSum: 0,
currentIndex: 0,
closestSum: ref answer,
minDifference: ref minDiff);

Console.WriteLine(answer);

static void FindClosestTriplet(int[] array, int size, int targetSum, int currentCount, int currentSum, int currentIndex, ref int closestSum, ref int minDifference)
{
// Return when reaching the end of the array
// If we have picked three elements so far, check if the sum is closest to our "targetSum"
if (currentIndex == size)
{
if (currentCount == 3)
{
int currentDifference = Math.Abs(targetSum - currentSum);
if (currentDifference < minDifference)
{
minDifference = currentDifference;
closestSum = currentSum;
}
}
return;
}

// Pick this number
FindClosestTriplet(array, size, targetSum, currentCount + 1, currentSum + array[currentIndex], currentIndex + 1, ref closestSum, ref minDifference);

// Don't pick this number
FindClosestTriplet(array, size, targetSum, currentCount, currentSum, currentIndex + 1, ref closestSum, ref minDifference);
}
Also, you can use the debugger in VS or some other IDE to track the intermediate values.
EmperMiner
EmperMinerOP17mo ago
thank you for the explaination of ref, I still can't understand the code though. I'll keep trying
Accord
Accord17mo ago
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.
Want results from more Discord servers?
Add your server