I am having troubles figuring out how to extract a minimum and maximum number from an Array. This program allows a user to input up to 12 assignment grades and then my array gathers those grades and displays them in the output box. I just also am required to display the minumum and maximum grades from the array as well. Thanks for the help!

private void display_output()
        {
            Txt_Output.Text = "";
            course = Txt_Course.Text;
            name = Txt_Name.Text;

            Txt_Output.Text = "Course: " + course + "\r\n" + "Student: " + name + "\r\n\r\n" + "Assignments:" + "\r\n";

            for (int posn = 0; posn < assigns.Length; posn++)
            {
                 Txt_Output.Text += assigns[posn] + "\r\n";
            }
            return; 
        }

Can you use Linq?
If not, use the GetMaxFromArray and GetMinFromArray methods.

[These are just options]

using System;
using System.Linq;

namespace DW_397808
{
   class Program
   {
      private static int GetMaxFromArray(int[] ia)
      {
         int iMax = ia[0];
         foreach(int i in ia)
         {
            if(i > iMax)
               iMax = i;
         }
         return iMax;
      }

      private static int GetMinFromArray(int[] ia)
      {
         int iMin = ia[0];
         foreach (int i in ia)
         {
            if (i < iMin)
               iMin = i;
         }
         return iMin;
      }

      static void Main(string[] args)
      {
         int[] arr_int = new int[] { 5, 4, 123, 555, 2, -1, 45 };

         Console.WriteLine(arr_int.Min()); // prints -1
         Console.WriteLine(arr_int.Max()); // prints 555
         Console.WriteLine(GetMinFromArray(arr_int)); // prints -1
         Console.WriteLine(GetMaxFromArray(arr_int)); // prints 555
      }
   }
}

Unfortunately not, but I think I finally figured out the max and min. And I finished my code and cleaned it up a bit, but on a sort of related question, it still isn't working. My Txt_Output textbox only shows my last method, get_average(), and nothing above it. Why is this happening? I really appreciate the help.

ps. I added in my get_average() method for reference. Is it something in that method or the display_output method?

private double get_average()
        {
            double total = 0.0;
            double average;

            for (int posn = 0; posn < assigns.Length; posn++)
            {
                total += assigns[posn];
            }

            average = total / assigns.Length;
            Txt_Output.Text = "Average: " + average + "\r\n";
            return average;
        }

        private void display_output()
        {
            Txt_Output.Text = "";

            string course = get_course();
            string name = get_name();
            
            course = Txt_Course.Text;
            name = Txt_Name.Text;

            Txt_Output.Text = "Course: " + course + "\r\n" + "Student: " + name + "\r\n\r\n";

            get_list();
            get_max();
            get_min();
            get_average();
        }

My post had both Linq and non-Linq solutions in it.
And, could you show me your get_min() and get_max() functions?

private int get_list()
        {
            for (int posn = 0; posn < assigns.Length; posn++)
            {
                Txt_Output.Text += "Assignments:" + "\r\n" + assigns[posn] + "\r\n";
            }
            return assigns[0];
        }

        private int get_max()
        {
            int highest = assigns[0];

            for (int posn = 1; posn < assigns.Length; posn++)
            {
                if (assigns[posn] > highest)
                {
                    highest = assigns[posn];
                }
            }
            Txt_Output.Text = "Max: " + highest + "\r\n";
            return highest;
        }

        private int get_min()
        {
            int lowest = assigns[0];

            for (int posn = 1; posn < assigns.Length; posn++)
            {
                if (assigns[posn] < lowest)
                {
                    lowest = assigns[posn];
                }
            }
            Txt_Output.Text = "Min: " + lowest + "\r\n";
            return lowest;
        }

Good, but there is one thing you need to keep in mind:
You will need an array min/max solution at some point other than this one program.
You should make a generic routine that is independent of display that will do that for you.

Right now, your result is bound to your Text Box.
If you need those functions again in another program, you would need to copy/paste/modify/re-compile.

If you had the get_min() and get_max() and the other functions separated into a module that does not display, you could call them from any type of dot net program (or language) including ASP.NET and Silverlight.

That type of re-use will get you a lot of programming power and change how you feel about tackling tough problems (because you'll only need to "fix" it once).

You could also (for now) just remove the Txt_Output.Text lines from your functions and take the values after the functions run.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.