Solved

Min Value of Array

Posted on 2008-06-23
18
3,079 Views
Last Modified: 2008-06-25
I found the below - which gets the max value of an array - but ow do I get the minimum value of the array?
int max = intArray[0];    // first element be the max
        int i = 0;
        for (i = 1; i < intArray.Length; i++)
        {
            if (intArray[i] > max)
            {
                // if the elemnt at index  i  is greater than
                //previous max , than set this value to the max
                max = intArray[i];
            } //end of if 
 
        }//end of for

Open in new window

0
Comment
Question by:tbaseflug
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 3
  • +3
18 Comments
 
LVL 6

Expert Comment

by:MuhammadAdil
ID: 21848510
Just change condition
if(intArray[i] < max)
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21848514
assuming that this is more for "learning":
§ to find the max, the code loops and compares the current item with the current max value, using >
so, to find the min, you have just to change > by < ...
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 21848600
If you just want to find out the max value and the min value of the array, you can do this:

Array.Sort(intArray);
int max = Int32.Parse(intArray[intArray.Length -1]);
int min = Int32.Parse(intArray[0]);
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

Author Comment

by:tbaseflug
ID: 21848606
I am using arrays for the first time - I have the below and when I use the original (above) code (max) it seems to get the max array value - but when switch > to < (above) it does not work


        int[] intArray = new int[5];
        //IDC10 
        if (Grd.Contains("ICD10"))
        {
            _idc10.Enabled = true;
            PlaceHolder ph = _idc10.FindControl("phICD10") as PlaceHolder;
            UserControl uc = (UserControl)Page.LoadControl("~/reports/ICD10.ascx");
            ph.Controls.Add(uc);
            intArray[2] = 3;
        }
        else
        {
            _idc10.Enabled = false;
        }
        //ICD9 
        if (Grd.Contains("ICD9"))
        {
            _ICD9.Enabled = true;
            PlaceHolder ph = _ICD9.FindControl("phICD9") as PlaceHolder;
            UserControl uc = (UserControl)Page.LoadControl("~/reports/ICD9.ascx");
            ph.Controls.Add(uc);
            intArray[1] = 2;
        }
        else
        {
            _ICD9.Enabled = false;
        }
 
 
        int max = intArray[0];    // first element be the max
        int i = 0;
        for (i = 1; i < intArray.Length; i++)
        {
            if (intArray[i] < max)
            {
                // if the elemnt at index  i  is greater than
                //previous max , than set this value to the max
                max = intArray[i];
            } //end of if 
 
        }//end of for 

Open in new window

0
 
LVL 22

Expert Comment

by:prairiedog
ID: 21848632
Have you tried Array.Sort(intArray) as I suggested?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21848634
>it does not work
what is the error/problem?
0
 

Author Comment

by:tbaseflug
ID: 21848644
prairedog - it is throwing

The best overloaded method match for 'int.Parse(string)' has some invalid arguments
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 21848668
Try this:
Array.Sort(intArray);
int max = Int32.Parse(intArray[intArray.Length -1].ToString());
int min = Int32.Parse(intArray[0].ToString());
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21848692
why actually use the Parse and Tostring?
Array.Sort(intArray);
int max = intArray[intArray.Length -1];
int min = intArray[0];

Open in new window

0
 

Author Comment

by:tbaseflug
ID: 21848695
I am setting the activetable index to it =  _tabs.ActiveTabIndex = min;  When I set it to the max it sets the index properly, when I set it to min, it does not move
0
 

Author Comment

by:tbaseflug
ID: 21848720
here is all of the cose I am using

int[] intArray = new int[5];
        //IDC10 
        if (Grd.Contains("ICD10"))
        {
            _idc10.Enabled = true;
            PlaceHolder ph = _idc10.FindControl("phICD10") as PlaceHolder;
            UserControl uc = (UserControl)Page.LoadControl("~/reports/ICD10.ascx");
            ph.Controls.Add(uc);
            intArray[3] = 3;
        }
        else
        {
            _idc10.Enabled = false;
        }
        //ICD9 
        if (Grd.Contains("ICD9"))
        {
            _ICD9.Enabled = true;
            PlaceHolder ph = _ICD9.FindControl("phICD9") as PlaceHolder;
            UserControl uc = (UserControl)Page.LoadControl("~/reports/ICD9.ascx");
            ph.Controls.Add(uc);
            intArray[2] = 2;
        }
        else
        {
            _ICD9.Enabled = false;
        }
        //Modifier Lookups
        if (Grd.Contains("Modifier"))
        {
            _mod.Enabled = true;
            PlaceHolder ph = _mod.FindControl("phMOD") as PlaceHolder;
            UserControl uc = (UserControl)Page.LoadControl("~/reports/Modifiers.ascx");
            ph.Controls.Add(uc);
            intArray[4] = 4;
        }
        else
        {
            _mod.Enabled = false;
        }
        //UB92
        if (Grd.Contains("UB92"))
        {
            _ub92.Enabled = true;
            PlaceHolder ph = _ub92.FindControl("phUB92") as PlaceHolder;
            UserControl uc = (UserControl)Page.LoadControl("~/reports/UB92.ascx");
            ph.Controls.Add(uc);
            intArray[5] = 5;
        }
        else
        {
            _ub92.Enabled = false;
        }
        //Devices
        if (Grd.Contains("Device"))
        {
            _devices.Enabled = true;
            PlaceHolder ph = _devices.FindControl("phDevices") as PlaceHolder;
            UserControl uc = (UserControl)Page.LoadControl("~/reports/Devices.ascx");
            ph.Controls.Add(uc);
            intArray[1] = 1;
        }
        else
        {
            _devices.Enabled = false;
        }
 
        TabContainer _tabs = Parent.Parent.Parent.Parent.Parent.FindControl("tabs") as TabContainer;
 
        
        Array.Sort(intArray);
        int max = intArray[intArray.Length - 1];
        int min = intArray[0];

Open in new window

0
 
LVL 22

Expert Comment

by:prairiedog
ID: 21848778
angelIII, you are right. No Parse or ToString() is needed here. I was actually trying to correct it while you were posting.
0
 
LVL 11

Expert Comment

by:CMYScott
ID: 21848780
what version of VS are you using?

If you have VS2008, you can just use...

int max= intArray.Max();
int min = intArray.Min();
0
 

Author Comment

by:tbaseflug
ID: 21848859
I am stuck on VS2005
0
 
LVL 18

Accepted Solution

by:
jcoehoorn earned 500 total points
ID: 21849435
You have this:
   int[] intArray = new int[5];

and later:

  intArray[5] = 5;

new int[5] means create 5 elements indexed 0-4.  There is no intArray[5].  

Also, this is definitely a medical application of some kind, and I'm wondering about your logic here.  A mod is not in the same category as an icd9 or icd10 diagnosis code, and neither are anything at all like a device or ub92 message.
0
 

Author Comment

by:tbaseflug
ID: 21850775
I have never used arrays - what I am trying to do is to set a for each of the load controls that I load - so thought (for lack of correct terminology) - I was setting the array "node" #5 to a value of "5" with intArray[5] = 5;
0
 

Author Comment

by:tbaseflug
ID: 21851620
OK - when using this, the min always returns 0 - the max does as expected and does return the max #

        Array.Sort(intArray);
        int max = intArray[intArray.Length - 1];
        int min = intArray[0];
0
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 21851893
Because if you don't assign a value to an array element that element still exists, and it's value is 0.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Generate Unique ID in VB.NET 21 105
asp web application 3 43
Exception when using Alea null reference in fsharp.core.dll 3 55
Converting PHP triple des decrypt to c# 9 76
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question