C
C#8mo 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
Angius8mo 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
TowersAlex8mo 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
Accord8mo 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
❔ ENV var dump collection not working on some machinesI'm using these env vars to collect dumps from my .net core macOS app (docs: https://learn.microsoft❔ How to play music files from resources/relative paths in wpf c# ?I have spend good 2 to 3 hours trying to get a sound file played from relative path, it just doesnt ❔ Simple code that I dont understand why it wont work.Hello, I am an IT specialized class student and we started learning C# in class, I am trying out somEmulating Windows FeaturesHello, I'm trying to figure out a couple of things before I start a new project. The first of which ❔ IEnumerable to ObservableCollectionI am not sure if im doing this correctly but I am trying to create a sqlite table with some data in ❔ EF Core optional where extension methodI have a lot of optional parameters for queries. Filter by this, filter by that etc. I can do it lik❔ Need help on UI Design and tips for implementation - .Net Mauithree questions: 1. How can I edit the blue bar on the top? I'm not sure where its coming from, I o❔ Implementation of Search page in ASP. NET MVC along with stored procedureCan anyone help me to create a webform to search the data from the database to filter it on web page❔ MySQL server issue (Error 28)I'll prefix this with a few things: - I'm not driven with Ubuntu or other unix based systems. - The ✅ data override in dictionary of dictionariesI'm trying to create a dictionary of dictionaries, and use temp variable to create it, but when i ch