Solved

Finding highest counter

Posted on 2006-11-21
7
228 Views
Last Modified: 2010-04-16
Hello experts:
I need help with determining the highest counter.
I have a Windows C# application and I accumulate a bunch of counters.

Count1++
Count2++
Count3++
Count4++

How would I go about deterrmining which one has the highest value in the shortest statment possible so that I can
add more logic.

if count1 is highest do something
if count2 is highest do seomthing else
etc

Thanks  
0
Comment
Question by:lancerxe
  • 5
7 Comments
 
LVL 22

Expert Comment

by:_TAD_
ID: 17988184

The method that uses the least amount of line space would be to use a function.

public void Main()
{

int count1;
int count2;
int count3;
int count4;

//load counters

int highest = GetLargest(new int[] {count1, count2, count3, count4});
...
..
.
}

private int GetLargest(int[] vals)
{
    int highest = int.minimum;
   
    for(int i=0;i<vals.Length;i++)
       if(vals[i]>highest)
           highest = vals[i];

   return highest;
}



0
 
LVL 22

Expert Comment

by:_TAD_
ID: 17988185

The method that uses the least amount of line space would be to use a function.

public void Main()
{

int count1;
int count2;
int count3;
int count4;

//load counters

int highest = GetLargest(new int[] {count1, count2, count3, count4});
...
..
.
}

private int GetLargest(int[] vals)
{
    int highest = int.minimum;
   
    for(int i=0;i<vals.Length;i++)
       if(vals[i]>highest)
           highest = vals[i];

   return highest;
}



0
 
LVL 22

Expert Comment

by:_TAD_
ID: 17988187

The method that uses the least amount of line space would be to use a function.

public void Main()
{

int count1;
int count2;
int count3;
int count4;

//load counters

int highest = GetLargest(new int[] {count1, count2, count3, count4});
...
..
.
}

private int GetLargest(int[] vals)
{
    int highest = int.minimum;
   
    for(int i=0;i<vals.Length;i++)
       if(vals[i]>highest)
           highest = vals[i];

   return highest;
}



0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 16

Expert Comment

by:AlexNek
ID: 17988789
I suggest to create a new Counter class
like
   class myCounter
    {
        const int MAX_COUNTER = 4;

        private int[] mCounter= new int[MAX_COUNTER];
        private int mMaxValue = int.MinValue;
        private int mMaxCounterIndex = -1;

        public myCounter()
        {
            int i;

            for (i = 0; i < MAX_COUNTER; i++)
            {
                mCounter[i] = 0;
            }
        }

        public void IncCounter(int aIndex)
        {
            if (aIndex > 0 && aIndex < MAX_COUNTER)
            {
                mCounter[aIndex]++;
                if (mCounter[aIndex] > mMaxValue)
                {
                    mMaxValue = mCounter[aIndex];
                    mMaxCounterIndex = aIndex;
                }
            }
        }

        public int maxCounterValue
        {
            get
            {
                return mMaxValue;
            }
        }

        public int maxCounterIndex
        {
            get
            {
                return mMaxCounterIndex;
            }
        }
0
 
LVL 10

Accepted Solution

by:
tpwells earned 250 total points
ID: 17989761
if all your counters are of the ame type you can quickly add them to an ArrayList and sort the list. After sorting the last item in the ArrayList will be the highest number.

System.Collections.ArrayList arList= new System.Collections.ArrayList();
arList.Add(counter1);
arList.Add(counter2);
arList.Add(counter3);
arList.Add(counter4);
arList.Sort();
int top = (int)arList[arList.Count-1];
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 17989948

I like tpwells' arraylist idea.... (same concept, abreviated code)


ArrayList arList = new ArrayList(new int[] {count1, count2, count3, count4});
arList.Sort();
int top = (int)arList[arList.Count-1];


0
 
LVL 22

Assisted Solution

by:_TAD_
_TAD_ earned 250 total points
ID: 17990066

On the same vein, if all of the counters are of the same type, you could simply create an int[] and sort that array.

int[] myArray = new int[]{count1, count2, count3, count4};
Array.Sort(myArray);
int top = myArray[myArray.Length-1];

using this code does two things
  1) It will make your memory footprint a bit smaller because it does not have to include the System.Collections library in memory.

  2) The performance will be bette because it does not have to box and unbox your data.


This is not to detract from tpwells' solution (kudos to him for the idea).  The performance increase and memory footprint arguments are purely accademic.  (around 8 k of memory and < 0.3 ms of time is saved)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Parallel.ForEach using SendMailAsync() detect when loop is complete 24 54
Events in static methods 3 50
Data is not showing from images 15 37
Connection String 16 42
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

919 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now