Click here to Skip to main content
15,888,301 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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

C#
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);
            }
        }
Posted
Updated 11-May-23 23:28pm
v2
Comments
Richard MacCutchan 12-May-23 4:57am    
"But doesn't work"
Sorry, but we cannot guess what that is supposed to mean. Please use the Improve question link above, and add complete details of what is not working.
Graeme_Grant 12-May-23 5:25am    
That sure is a lump of code! A lot is going on...

Break each process into single responsibility methods. Use a descriptive name. What this will do is make the code more readable and easier to debug & maintain.
Member 14093277 12-May-23 5:27am    
Thank you Graeme_Grant for your reply.

no, it's the complete part of my code. Right now i add the part where i use this 2 method
Graeme_Grant 12-May-23 5:30am    
I also recommend posting before and after data sets demonstrating expectation.
Graeme_Grant 12-May-23 5:37am    
Not sure that I understand what you are trying to do, but if I read this correctly, these values look incorrect:
righe[0] = 10; righe[1] = 1; righe[2] = 1; righe[3] = 0; righe[4] = 1;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900