Hi guys, I have 3 arrays. The first multidimensional (e.g. 5 rows and 6 columns) of messy positive integer values. The second array, therefore composed of 6 elements, corresponding to the value of the sum of the single columns of the first. the second, therefore composed of 5 elements, corresponding to the value of the sum of the single lines of the first. I need a function that shifts the values of the first array so that the sums of the columns and rows match the second array and the third array. Possibly without using external libraries.
What I have tried:
I try to write two function, one fix the columns and one the rows. But doesn't work
public static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
int decimals = 0;
decimal[,] valori = new decimal[6,5];
valori[0, 0] = 2; valori[0, 1] = 0; valori[0, 2] = 0; valori[0, 3] = 0; valori[0, 4] = 0;
valori[1, 0] = 2; valori[1, 1] = 1; valori[1, 2] = 0; valori[1, 3] = 0; valori[1, 4] = 0;
valori[2, 0] = 0; valori[2, 1] = 0; valori[2, 2] = 1; valori[2, 3] = 0; valori[2, 4] = 1;
valori[3, 0] = 1; valori[3, 1] = 0; valori[3, 2] = 0; valori[3, 3] = 0; valori[3, 4] = 1;
valori[4, 0] = 2; valori[4, 1] = 1; valori[4, 2] = 0; valori[4, 3] = 0; valori[4, 4] = 0;
valori[5, 0] = 1; valori[5, 1] = 0; valori[5, 2] = 0; valori[5, 3] = 0; valori[5, 4] = 0;
decimal[] colonne = new decimal[6];
colonne[0] = 2;
colonne[1] = 3;
colonne[2] = 2;
colonne[3] = 1;
colonne[4] = 3;
colonne[5] = 2;
decimal[] righe = new decimal[5];
righe[0] = 10; righe[1] = 1; righe[2] = 1; righe[3] = 0; righe[4] = 1;
Funzioni funzioni = new Funzioni();
funzioni.PrintMatrice(valori);
funzioni.AdjustColumnOfSqueakyForFutureOnMatrix(valori, colonne, righe, decimals);
funzioni.PrintMatrice(valori);
funzioni.AdjustRowOfSqueakyForFutureOnMatrix(valori, colonne, righe, decimals);
funzioni.PrintMatrice(valori);
}
private void AdjustColumnOfSqueakyForFutureOnMatrix(decimal[,] matrixOfQtysByDPAndInstance, List<InstanceQtyHolderItem> instanceQtyHolderItems, decimal[] totalByDP, int decimals)
{
string matrixBeforeAdjust = GetStringViewOfMatrix(matrixOfQtysByDPAndInstance);
int mul = 1;
for (int i = 0; i < decimals; ++i)
{
mul *= 10;
}
decimal unit = 1 / mul;
Logger.Info("Start Adjust Column for FutureOnMatrix");
for (int j = 0; j <= matrixOfQtysByDPAndInstance.GetUpperBound(1); j++)
{
Logger.Info($"Try to Fix column {j}, rapresent element of InstanceQtyHolder idInstance: {instanceQtyHolderItems[j].IdInstance}, QtyHolder {instanceQtyHolderItems[j].QtyHolder}");
do
{
decimal totalByColumn = SumByColumnOnFutureOnMatrix(matrixOfQtysByDPAndInstance, j);
Logger.Info($"Currently we have {totalByColumn} element on column {j}, expected {totalByDP[j]}");
if (totalByColumn == totalByDP[j])
{
Logger.Info($"column {j}, correct");
break;
}
bool isToAdd = totalByDP[j] - totalByColumn > 0;
decimal maxValueOfColumn = 0;
int rowOfMaxValueOfColumn = -1;
for (int ii = 0; ii <= matrixOfQtysByDPAndInstance.GetUpperBound(0); ii++)
{
if (matrixOfQtysByDPAndInstance[ii, j] > maxValueOfColumn)
{
maxValueOfColumn = matrixOfQtysByDPAndInstance[ii, j];
rowOfMaxValueOfColumn = ii;
}
}
if (rowOfMaxValueOfColumn == -1)
{
if (isToAdd)
rowOfMaxValueOfColumn = 0;
else
{
Logger.Warn($"In column {j}, there isn't a value that i can swap");
Logger.Warn($"Matrix, at the begin of the function {matrixBeforeAdjust}Matrix, at this moment {GetStringViewOfMatrix(matrixOfQtysByDPAndInstance)}");
break;
}
}
decimal maxValueOfRow = 0;
int colOfMaxValueOfRow = -1;
for (int jj = j + 1; jj <= matrixOfQtysByDPAndInstance.GetUpperBound(1); jj++)
{
if(SumByColumnOnFutureOnMatrix(matrixOfQtysByDPAndInstance, jj) != totalByDP[jj])
{
if (matrixOfQtysByDPAndInstance[rowOfMaxValueOfColumn, jj] > maxValueOfRow)
{
maxValueOfRow = matrixOfQtysByDPAndInstance[rowOfMaxValueOfColumn, jj];
colOfMaxValueOfRow = jj;
}
}
}
if (colOfMaxValueOfRow == -1)
{
if (!isToAdd && j < matrixOfQtysByDPAndInstance.GetUpperBound(1))
colOfMaxValueOfRow = j + 1;
else
{
Logger.Warn($"In column {j} there is value on row ({rowOfMaxValueOfColumn}) that i need to swap, but there isn't on another column with same row a value aviable to swap");
Logger.Warn($"Matrix, at the begin of the function {matrixBeforeAdjust}Matrix, at this moment {GetStringViewOfMatrix(matrixOfQtysByDPAndInstance)}");
break;
}
}
matrixOfQtysByDPAndInstance[rowOfMaxValueOfColumn, j] += isToAdd ? unit : -unit;
matrixOfQtysByDPAndInstance[rowOfMaxValueOfColumn, colOfMaxValueOfRow] += isToAdd ? -unit : unit;
} while(true);
}
}
private void AdjustRowOfSqueakyForFutureOnMatrix(decimal[,] matrixOfQtysByDPAndInstance, List<DeliveryPlanSaveEntity> sortedFutureDeliveryPlans, decimal[] totalByInstance, int decimals)
{
string matrixBeforeAdjust = GetStringViewOfMatrix(matrixOfQtysByDPAndInstance);
int mul = 1;
for (int i = 0; i < decimals; ++i)
{
mul *= 10;
}
decimal unit = 1 / mul;
Logger.Info("Start Adjust Row for FutureOnMatrix");
for (int i = 0; i <= matrixOfQtysByDPAndInstance.GetUpperBound(0); i++)
{
Logger.Info($"Try to Fix row {i}, rapresent element of DeliveryPlans id: {sortedFutureDeliveryPlans[i].IdDeliveryPlan}, idArticle {sortedFutureDeliveryPlans[i].IdArticle}, week: {sortedFutureDeliveryPlans[i].Week}");
do
{
decimal totalByRow = SumByRowOnFutureOnMatrix(matrixOfQtysByDPAndInstance, i);
Logger.Info($"Currently we have {totalByRow} element on row {i}, expected {totalByInstance[i]}");
if (totalByRow == totalByInstance[i])
{
Logger.Info($"row {i}, correct");
break;
}
bool isToAdd = totalByInstance[i] - totalByRow > 0;
decimal maxValueOfRow = 0;
int columnOfMaxRowOfColumn = -1;
for (int j = 0; j <= matrixOfQtysByDPAndInstance.GetUpperBound(1); j++)
{
if (matrixOfQtysByDPAndInstance[i, j] > maxValueOfRow)
{
maxValueOfRow = matrixOfQtysByDPAndInstance[i, j];
columnOfMaxRowOfColumn = j;
}
}
if (columnOfMaxRowOfColumn == -1)
{
if (isToAdd)
columnOfMaxRowOfColumn = 0;
else
{
Logger.Warn($"In row {i}, there isn't a value that i can swap");
Logger.Warn($"Matrix, at the begin of the function {matrixBeforeAdjust}Matrix, at this moment {GetStringViewOfMatrix(matrixOfQtysByDPAndInstance)}");
break;
}
}
decimal maxValueOfColumn = 0;
int rowOfMaxValueOfColumn = -1;
for (int ii = i + 1; ii <= matrixOfQtysByDPAndInstance.GetUpperBound(0); ii++)
{
if (SumByRowOnFutureOnMatrix(matrixOfQtysByDPAndInstance, ii) != totalByInstance[ii])
{
if (matrixOfQtysByDPAndInstance[ii, columnOfMaxRowOfColumn] > maxValueOfColumn)
{
maxValueOfColumn = matrixOfQtysByDPAndInstance[ii, columnOfMaxRowOfColumn];
rowOfMaxValueOfColumn = ii;
}
}
}
if (rowOfMaxValueOfColumn == -1)
{
if (!isToAdd && i < matrixOfQtysByDPAndInstance.GetUpperBound(0))
rowOfMaxValueOfColumn = i + 1;
else
{
Logger.Warn($"In row {i} there is value on column ({columnOfMaxRowOfColumn}) that i need to swap, but there isn't on another row with same column a value aviable to swap");
Logger.Warn($"Matrix, at the begin of the function {matrixBeforeAdjust}Matrix, at this moment {GetStringViewOfMatrix(matrixOfQtysByDPAndInstance)}");
break;
}
}
matrixOfQtysByDPAndInstance[i, columnOfMaxRowOfColumn] += isToAdd ? unit : -unit;
matrixOfQtysByDPAndInstance[rowOfMaxValueOfColumn, columnOfMaxRowOfColumn] += isToAdd ? -unit : unit;
} while (true);
}
}