简体   繁体   English

在多维结构数组中查找最小值和最大值

[英]Find Minimum and Maximum values within a multi dimensional struct array

For a school project in learning c# I am making a data collection console application which saves floating point entries for 4 different locations along with the time/date, and user who recorded the entry. 对于正在学习c#的学校项目,我正在制作一个数据收集控制台应用程序,该应用程序将保存4个不同位置的浮点条目以及时间/日期以及记录该条目的用户。 I am required to use a multi dimensional struct array. 我需要使用多维结构数组。

I need to have a view that displays the average of the values, along with the minimum and maximum values and the first and last date. 我需要一个显示值平均值,最小值和最大值以及第一个和最后一个日期的视图。 I've got the average figured out by counting through the values and adding cumulatively but I can't figure out how to find the minimum and maximum values. 我已经通过计算值并累加得出平均值,但是我不知道如何找到最小值和最大值。

I tried searching and came across this page: http://www.dotnetperls.com/max I tried to implement this syntax into my code to no avail due to it being a much more complex array. 我尝试搜索并遇到了以下页面: http : //www.dotnetperls.com/max我尝试将这种语法实现到我的代码中,但由于数组更加复杂,因此无济于事。

It worked in my test with integers: 它在我的整数测试中起作用:

class Program
{
    static int[][] intarray = new int[4][] { new int[10], new int[10], new int[10], new int[10] };
    static void Main(string[] args)
    {
        intarray[0][0] = 5;
        intarray[0][1] = 3;
        intarray[0][2] = 10;
        intarray[0][3] = 4;
        intarray[0][4] = 2;
        Console.WriteLine(intarray[0].Max());
        Console.ReadLine();
    }
}

The above code perfectly displays the number 10! 上面的代码完美地显示了数字10! :) :)

But when I tried to implement this in to my program with a struct array, it doesn't work: 但是,当我尝试使用struct数组将其实现到程序中时,它不起作用:

class Program
{
    static byte location = 0;
    static float entriesmin;
    static float entriesmax;
    static Entry[][] entriesarray = new Entry[4][] { new Entry[10], new Entry[10], new Entry[10], new Entry[10] };

    struct Entry
    {
        public float value;
        public string user;
        public DateTime datetime;
    }

    static byte LoopEntries(bool display)
    {
        float runningtotal = 0;
        byte entrycount = 0;
        foreach (Entry entry in entriesarray[0])
        {
            if (entry.user != null)
            {
                if (display)
                {
                    string ten;
                    if (entrycount == 9)
                    {
                        ten = "  #";
                    }
                    else
                    {
                        ten = "   #";
                    }
                    Console.WriteLine(ten + (entrycount + 1) + " " + entry.datetime + " " + entry.user + new string(' ', 16 - entry.user.Length) + entry.value);
                }
                runningtotal += entry.value;
                entrycount += 1;                
            }
        }
        entriesmin = (entriesarray[location]).value.Min();
        entriesmax = (entriesarray[location]).value.Max();            
        if (entrycount == 0 && display)
        {
            Console.WriteLine("No entries to show!");                
        }
        return entrycount;
    }

I need to hand this project in on Monday! 我需要在星期一交这个项目! I really hope someone can help me! 我真的希望有人能帮助我! ;) ;)

What you have is not a multidimensional array, it's an array of array (aka a jagged array), but you work with them in a similar way. 您拥有的不是多维数组,而是数组数组(又称锯齿数组),但是您可以通过类似的方式来使用它们。

To loop through the array of arrays you need a loop in a loop: 要遍历数组数组,您需要一个循环:

foreach (Entry[] arr in entriesarray) {
  foreach (Entry entry in arr) {
    // ...
  }
}

You could use the Min and Max extension methods to get the minimum and maximum value of each inner array, but you still would have to find the minimum and maximum between those values. 您可以使用MinMax扩展方法来获取每个内部数组的最小值和最大值,但是您仍然必须在这些值之间找到最小值和最大值。 As you are already looping through all the entries anyway to get the average you can just keep a record of the smallest and largest values found: 无论如何,您已经在所有条目中进行循环以获取平均值,因此您只需记录找到的最小和最大值即可:

float runningtotal = 0;
int entrycount = 0;
float min = float.MaxValue;
float max = float.MinValue;
foreach (Entry[] arr in entriesarray) {
  foreach (Entry entry in arr) {
    runningtotal += entry.value;
    entrycount++;
    if (entry.value < min) {
      min = entry.value;
    }
    if (entry.value > max) {
      max = entry.value;
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM