I'm working on a problem set at college where were using c#. I have an array of integers (apologies first off if I don't know all the problem terms) and need to find the position in array of the maximum number (along with the minimum). I have it working but it doesn't seem a very good way to do it. Can anyone suggest a better way to achive what I have?
Heres my code:
int[] usageHours = { 3, 3, 5, 4, 0, 0, 2, 2, 4, 25, 158, 320, 212, 356, 401, 460, 480, 403, 298, 213, 102, 87, 34, 45 };
double myAverage = usageHours.Average();
int runningTotal = 0;
int runningMaxPosition = 0;
for (int i = 0; i < usageHours.Length; i++)
{
if (usageHours[i] > runningTotal)
{
runningMaxPosition = i;
runningTotal = usageHours[i];
}
}
txtmax.Text = Convert.ToString(runningMaxPosition)+" With: "+Convert.ToString(runningTotal)+" Users";
txtAv.Text = Convert.ToString(myAverage);
I hope this is readable enough, its not particularly neat as i always make it readable once its working well.