TowersAlex
TowersAlex
CC#
Created by TowersAlex on 10/18/2023 in #help
❔ (Beginner) Seeking Advice on Distributing Items Equally in C# Program
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]);
}
}
}
8 replies