C
C#12mo 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
boiled goose
boiled goose12mo ago
have you tried it?
EmperMiner
EmperMiner12mo 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
benqbenq12mo 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
benqbenq12mo 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
EmperMiner12mo ago
thank you for the explaination of ref, I still can't understand the code though. I'll keep trying
Accord
Accord12mo 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
More Posts
❔ @Html.DropDownListFor results in null for selected itemHi for the following command: @Html.DropDownListFor(m=>m.Name, new SelectList(Model.Names)) I get❔ WebApplicationFactory and integration testsI've run into an issue with my integration tests and I'm looking for some insight. I'm using `WebApp✅ ✅ C# HttpClient returning 400, but other tools return 200I am trying to make a simple web request to a URL using .NET/C# HttpClient. I am getting a 400 error❔ ASP.NET Core WebAPI - Cannot choose the AuthenticationScheme for MicrosoftGraphs?So I am using the Microsoft.Identity.Web Package to get the access token for a user. This works perf❔ Hello can anyone help me with partial view?It'll supposed to show the list of suppliers there and select but it's not showing❔ Why doesn't this button work?So I am trying to make a new project with vscode but this button isn't working. idk why.✅ Efcore help with 2 databasesI'm trying to add a config option for the user to be able to select between psql and SQLite, do I neTransition to Microsoft's Dependency Injection Libraryhttps://gist.github.com/MarioFares/f935dd74aa461feefb227631af1c2b3e I was wondering what I can do t❔ Problem while adding same article with different quantity on 2 different tablesI'm using EF Core and WPF. Once I add same article with different quantity on 2 different tables,qu❔ Better way to check 2 dictionaries against one another?I'm trying to write some logic for a game in Unity; a system that will check what items have been se