Click here to Skip to main content
16,021,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi..I have an array containing a group of values(type of double)..can I know how find maximum and minimum value from the array?because i want to display the values in textbox when the button is clicked..How if the array is stored in string form?How can i convert them into double array and then to be used for the methods?thanks in advance..
Posted
Updated 12-Jul-12 21:38pm
v2
Comments
Esmond90 13-Jul-12 3:36am    
Thanks everyone for replying.=) How if the array is store in string form?How can i convert them into double array and then to be used for the methods that you all have mentioned?

Hi,
Code for Finding the Maximum Value in an Array:
C#
maxValue = function (array) {
    mxm = array[0];
    for (i=0; i<array.length;>
       if (array[i]>mxm) {    
                mxm = array[i];    
       }
    }
    return mxm;
};

Code for Finding the Minimum Value in an Array:
C#
minValue = function (array) {
    mn = array[0];
    for (i=0; i<array.length;>        
        if (array[i]<mn){           
            mn = array[i];
        }
    }
    return mn;
};
 
Share this answer
 
v3
Using LINQ, all you need to do is:
C#
int[] array1 = { 1, -1, -2, 0 };

// Find maximum number.
Console.WriteLine(array1.Max());

// Find minimum number.
Console.WriteLine(array1.Min());

Refer: C# Max, Min[^]
 
Share this answer
 
Comments
Esmond90 17-Jul-12 23:49pm    
Do you mean using System.Linq? but error occur: Error 1 The type or namespace name 'Linq' does not exist in the namespace 'System' (are you missing an assembly reference?)
Sandeep Mewara 18-Jul-12 1:49am    
Yes, using Linq. Add the System.Core.dll as reference in your project to use it.
Esmond90 18-Jul-12 5:24am    
how can i add System.Linq.dll?i just couldn't find it..hope you can solve my problem.=)
Sandeep Mewara 18-Jul-12 5:51am    
Which .NET framework are you working on?
Sandeep Mewara 18-Jul-12 5:56am    
You need to add System.Core.dll for it. System.Linq is a namespace in it.
This way.
Click here[^]
 
Share this answer
 
See if below code helps you:

C#
private int Min(double[] numbers)
        {
            double m = numbers[0];

            for (int i = 0; i < numbers.Length; i++)
                if (m > numbers[i])
                    m = numbers[i];

            return m;
        }

        private int Max(double[] numbers)
        {
            double m = numbers[0];

            for (int i = 0; i < numbers.Length; i++)
                if (m > numbers[i])
                    m = numbers[i];

            return m;
        }
 
Share this answer
 
v2
Comments
Member 13416110 28-Sep-17 14:12pm    
Find minimum and maximum value in array using textbox in asp.net C# and also the sum of total array?kindly anyone help me.tahnks in advance
C#
In simple array You can manually perform max min algorithm . for more you can used collection like arraylist ,SortedList etc. in Simple Array You can used the following Code.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Exercise
{
    public class Program
    {
        static int Main(string[] args)
        {
            var Numbers = new double[10];
            var Minimum = 0D;
            var Maximum = 0D;

            Numbers[0] = 4478.72;
            Numbers[1] = 67.97;
            Numbers[2] = 2.32;
            Numbers[3] = 13004.05;
            Numbers[4] = 0.44;
            Numbers[5] = 873846.08;
            Numbers[6] = 120.12;
            Numbers[7] = 479.23;
            Numbers[8] = 12.42;
            Numbers[9] = 873846.04;

            Minimum = Numbers[0];

            for (var i = 0; i < Numbers.Length; i++)
            {
                Console.WriteLine("Number {0,2}:{1, 10}", i + 1, Numbers[i]);
            }


            for (var i = 0; i < Numbers.Length; i++)
            {
                if (Minimum > Numbers[i])
                    Minimum = Numbers[i];
            }

            Console.WriteLine();
            Console.WriteLine("Minimum = {0}", Minimum);

            for (int i = 0; i < Numbers.Length; i++)
            {
                if (Maximum < Numbers[i])
                    Maximum = Numbers[i];
            }

            Console.WriteLine("Maximum = {0}\n", Maximum);
            return 0;
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900