C
C#14mo ago
TowersAlex

❔ (Beginner) Seeking Advice on Distributing Items Equally in C# Program

Hey everyone! I hope you're doing well. I'm still pretty new around here and not exactly a pro in the world of C#, but I love programming in my spare time. A few weeks back, I started with C# and now I'm working on this project for an app. I'm tinkering with a program that evenly distributes different things (like 3 tomatoes, 4 bananas, and 6 apples) among a set number of bags (let's say 3 bags). The total count of each type of fruit and the overall count of items shouldn't differ by more than one, so it's as fair as possible for each bag. My approach is to create a list of integer arrays, which I'd call 'bags.' Each part of this list would hold an integer array where the items for each bag would go. Do you see where I'm going with this? I could really use your help and would appreciate your thoughts on how best to proceed. Should I divide the count of items or what would be your approach? Looking forward to your suggestions! Thanks in advance.
3 Replies
Angius
Angius14mo ago
Dividing the number of items by the number of bags seems like a fine way to go about it Then you would round the result of that division down for n - 1 items, and up for one item Or... no, forget that last bit It wouldn't always work You'd divide the number of items by the number of bags, round that down and sum up the decimal parts of all those numbers. Then you'd distribute that, one per bag until it's gone
TowersAlex
TowersAlexOP14mo ago
Are the rounded numbers correctly stored in the 'results' array and the remainders, i.e., the decimal remainders, in the 'sumUp' array? Now, the only thing left is the distribution, but is this approach correct?" Code:
// Online C# Editor for free
// Write, Edit and Run your C# code using C# Online Compiler

using System;

public class HelloWorld
{
public static void Main()
{
double[] objects = new double[]{4,4,2};
Pack(objects, 3);
}

public static void Pack(double[] things, int numBags)
{
double[] results = new double[]{0,0,0};
double[] sumUp = new double[]{0,0,0};

for(int i = 0; i < things.Length; i++)
{
double current = things[i] / numBags;
double nextnumber = (double)Math.Floor(current);

results[i] = current;

double part = current - nextnumber;
sumUp[i] = part;

Console.WriteLine(results[i] + "/" + Math.Floor(current) + "/" + part);
}

for(int b = 0; b < sumUp.Length; b++)
{
sumUp[b] = sumUp[b] * numBags;
Console.WriteLine(sumUp[b]);
}
}
}
// Online C# Editor for free
// Write, Edit and Run your C# code using C# Online Compiler

using System;

public class HelloWorld
{
public static void Main()
{
double[] objects = new double[]{4,4,2};
Pack(objects, 3);
}

public static void Pack(double[] things, int numBags)
{
double[] results = new double[]{0,0,0};
double[] sumUp = new double[]{0,0,0};

for(int i = 0; i < things.Length; i++)
{
double current = things[i] / numBags;
double nextnumber = (double)Math.Floor(current);

results[i] = current;

double part = current - nextnumber;
sumUp[i] = part;

Console.WriteLine(results[i] + "/" + Math.Floor(current) + "/" + part);
}

for(int b = 0; b < sumUp.Length; b++)
{
sumUp[b] = sumUp[b] * numBags;
Console.WriteLine(sumUp[b]);
}
}
}
Accord
Accord14mo 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