Max value from number array - CSharp System

CSharp examples for System:Array Calculation

Description

Max value from number array

Demo Code

/// Department of Proteomics and Signal Transduction. All rights reserved.
using System.Collections.Generic;
using System;//from   w w w  . j  av a  2 s.c o m

public class Main{
        public static int Max(int[] x)
        {
            int n = x.Length;
            if (n == 0)
            {
                return Int32.MinValue;
            }
            int max = Int32.MinValue;
            for (int i = 0; i < n; i++)
            {
                int val = x[i];
                if (val > max)
                {
                    max = val;
                }
            }
            return max;
        }
        public static float Max(float[] x)
        {
            int n = x.Length;
            if (n == 0)
            {
                return float.NaN;
            }
            float max = -float.MaxValue;
            for (int i = 0; i < n; i++)
            {
                float val = x[i];
                if (val > max)
                {
                    max = val;
                }
            }
            return max;
        }
        public static double Max(double[] x)
        {
            int n = x.Length;
            if (n == 0)
            {
                return Double.NaN;
            }
            double max = -Double.MaxValue;
            for (int i = 0; i < n; i++)
            {
                double val = x[i];
                if (val > max)
                {
                    max = val;
                }
            }
            return max;
        }
}

Related Tutorials