简体   繁体   中英

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. 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.

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! :)

But when I tried to implement this in to my program with a struct array, it doesn't work:

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. 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;
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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