【问题标题】:Minimum value of maximum values of two dimensional Array二维数组的最大值最小值
【发布时间】:2019-03-13 05:14:15
【问题描述】:

好的,我还有一个问题,因为正如我告诉你的那样,我正试图将所有这些值放入结果 3 中,就像我之前提到的那样 (x, y) 然后我必须得到每个点的最大值 (x , y ) 对于所有点然后得到最大值的最小值,我设法得到最大值但是当我试图得到它们中的最小值时它变成了零。

这是代码。

for (int i = 0; i < result1.GetLength(0); i++)
            {
                for (int j = 0; j < result1.GetLength(1); j++)
                {
                    for (int k = 0; k < result2.GetLength(0); k++)
                    {
                        for (int m = 0; m < result2.GetLength(1); m++)
                        {


                            result3[i, j] = result1[i, j] + "," + result2[k, m];
                            Console.WriteLine(result3[i, j]);
                            if (result1[i, j] > result2[k, m])
                            {
                                highestMoment[i, j] = result1[i, j];

                            }
                            else
                            {
                                highestMoment[i, j] = result2[k, m];
                            }
                            Console.WriteLine(highestMoment[i, j]);

                            if (lowestMoment[i, j] > highestMoment[i, j])
                            {
                                lowestMoment[i, j] = highestMoment[i, j];
                            }
                            Console.WriteLine(lowestMoment[i, j]);

                            counter++;



                        }
                    }

                }

            }

这是整个代码

double[,] Cranelocations = { { -12.3256, 0.5344 }, { -12.3256, -0.4656 }, { -12.3256, -1.4656 }, { -12.3256, -2.4656 } };
 double[,] Picklocation = { { -0.3256, -3.4656 }, { 0.6744, -3.4656 }, { 1.6744, -3.4656 }, { 2.6744, -3.4656 }, { 3.6744, -3.4656 }, { 4.6744, -3.4656 }, { 5.6744, -3.4656 } };
double[,] Setlocation = { { 20.62, 5.03 }, { 24.28, 5.03 }, { 28.40, 5.03 }, { 32.11, 5.03 }, { 35.99, 5.26 }, { 40.18, 5.26 } };
double[] Weights = { 11.7865, 14.7335, 15.1015, 10.7465 };
double[,] result1 = new double[Weights.Length * Cranelocations.GetLength(0), Picklocation.GetLength(0)];
    double[,] result2 = new double[Weights.Length * Cranelocations.GetLength(0), Setlocation.GetLength(0)];
    object[,] result3 = new object[result1.GetLength(0), result1.GetLength(1)];
    double[,] highestMoment = new double[result3.GetLength(0), result3.GetLength(1)];
    double[,] lowestMoment = new double[highestMoment.GetLength(0), highestMoment.GetLength(1)];
    int counter = 0;




                    for (int m = 0; m < Weights.Length; m++)
                    {
                        int offset = m * Cranelocations.GetLength(0);

                        for (int i = 0; i < Cranelocations.GetLength(0); i++)
                        {
                            for (int j = 0; j < Picklocation.GetLength(0); j++)
                            {
                                double x = Cranelocations[i, 0] - Picklocation[j, 0];
                                double y = Cranelocations[i, 1] - Picklocation[j, 1];

                                result1[i + offset, j] = Weights[m] * (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)));
                            }
                        }
                    }
                    //Console.WriteLine("-----------------------------------------------------------------");

                    for (int m = 0; m < Weights.Length; m++)

                    {
                        int offset = m * Cranelocations.GetLength(0);

                        for (int i = 0; i < Cranelocations.GetLength(0); i++)
                        {
                            for (int j = 0; j < Setlocation.GetLength(0); j++)
                            {


                                double x = Cranelocations[i, 0] - Setlocation[j, 0];
                                double y = Cranelocations[i, 1] - Setlocation[j, 1];

                                result2[i +offset, j] = Weights[m] * (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)));
                                //Console.WriteLine(result2[i, j]);

                            }

                        }
                    }




                    for (int i = 0; i < result1.GetLength(0); i++)
                    {
                        for (int j = 0; j < result1.GetLength(1); j++)
                        {
                            for (int k = 0; k < result2.GetLength(0); k++)
                            {
                                for (int m = 0; m < result2.GetLength(1); m++)
                                {


                                    result3[i, j] = result1[i, j] + "," + result2[k, m];
                                    Console.WriteLine(result3[i, j]);
                                    if (result1[i, j] > result2[k, m])
                                    {
                                        highestMoment[i, j] = result1[i, j];

                                    }
                                    else
                                    {
                                        highestMoment[i, j] = result2[k, m];
                                    }
                                    Console.WriteLine(highestMoment[i, j]);

                                    if (lowestMoment[i, j] > highestMoment[i, j])
                                    {
                                        lowestMoment[i, j] = highestMoment[i, j];
                                    }
                                    Console.WriteLine(lowestMoment[i, j]);

                                    counter++;







                                }
                            }

                        }

                    }

【问题讨论】:

  • 这是一个不错的想法,如果您希望我们认真研究您的问题,您可以花 1 分钟将其格式化,使其可读
  • 我很抱歉,但一个小时前已经为另一个问题发布了相同的代码,这里的一个人(感谢他)回复了解决方案并且没有对代码的外观发表任何评论就像这样,我认为它是可读的!
  • @MichaelRandall 我也是一个初学者,所以如果你发现我的编码方式不可读但我正在努力学习,我很抱歉..
  • 一些改进您的问题的建议:使其独立。没有人会搜索您的其他问题。准确说明你拥有什么以及你想要什么。在那一行中,你的意思是什么最大值中的最小值?我觉得很难说。而且我看不到代码在做什么。并且修复格式,正如您已经被要求的那样。使用问题下方的编辑按钮。
  • @NicoSchertler 感谢您的建议,我一定会遵循的:)

标签: c# arrays


【解决方案1】:

在将lowestMomenthighestMoment 进行比较之前,您永远不会分配它。由于 double 的默认值为 0,因此最低时刻将始终低于您将其与结果 0 进行比较的任何值。

这可能是您正在寻找的:

for (int i = 0; i < result1.GetLength(0); i++)
{
    int iOffset = i * result1.GetLength(1);

    for (int j = 0; j < result1.GetLength(1); j++)
    {
        for (int k = 0; k < result2.GetLength(0); k++)
        {
            int kOffset = k * result2.GetLength(1);

            for (int m = 0; m < result2.GetLength(1); m++)
            {
                result3[iOffset + j, kOffset + m] = result1[i, j] + "," + result2[k, m];
                Console.WriteLine(result3[iOffset + j, kOffset + m]);

                if (result1[i, j] > result2[k, m])
                {
                    highestMoment[i, j] = result1[i, j];
                }
                else
                {
                    highestMoment[i, j] = result2[k, m];
                }

                if (lowestMoment[i, j] == 0
                    || lowestMoment[i, j] > highestMoment[i, j])
                {
                    lowestMoment[i, j] = highestMoment[i, j];
                }

                Console.WriteLine(highestMoment[i, j]);
                Console.WriteLine(lowestMoment[i, j]);

                counter++;
            }
        }
    }
}

【讨论】:

  • 其实很接近,只是我想从结果 3 中得到最大值的最小值,例如我得到 (1, 2) , (5 ,6) , (10 , 8) 每个点的最大值是 (10 , 5 , 2) 然后我想得到这 3 个点中的最小值所以它将是 (2)。这个数字 (2) 就是我要找的。​​span>
  • 好的,看看我的编辑。但是,就像以前一样,您在 m & k 的每次迭代中都覆盖了highestMoment。您必须将索引更改为与result3 相同。但这回答了您的问题,为什么最小值为 0。
  • @AlaaMagdy 抱歉,Visual Studio 正在更新,所以我手动输入了这些答案。我希望这应该会给你你正在寻找的东西。
  • 它越来越近了,但它还没有显示出最低值但是我真的很感谢你的帮助
  • 之前的解决方案是最接近的解决方案,但它给了我两个最低的最小值,我只想要最小值。
猜你喜欢
  • 2016-06-21
  • 2017-03-16
  • 1970-01-01
  • 2015-04-06
  • 1970-01-01
  • 2017-09-28
  • 2014-06-28
  • 2019-03-31
  • 2020-06-20
相关资源
最近更新 更多