Reputation: 1
I'm currently taking intro to c# in school.
My instructor has asked me to:
I'm getting the array values from user input.
This is what I have below, but I feel like this isn't really what he wants. --EDIT-- I can't use a maxvalue attribute. That is beyond the scope of what he's taught. For those that are looking for a more specific question.
Without showing me full code examples. How are some other ways I might be able to solve this without the use of minimumvalue or maximumvalue attributes?
I apologize for not being clearer than that. This is only my 3rd week in the course, so my understanding of programming in general is very low.
while (j < myDoubles.Length)
{
double myDoubles1 = myDoubles[j];
j++;
double myDoubles2 = myDoubles[j];
j++;
double myDoubles3 = myDoubles[j];
j++;
double myDoubles4 = myDoubles[j];
j++;
double myDoubles5 = myDoubles[j];
j++;
while (myDoubles1 < myDoubles2 && myDoubles1 < myDoubles3 && myDoubles1 < myDoubles4 && myDoubles1 < myDoubles5)
{
Console.WriteLine("myDoubles[1] is the lowest");
break;
}
while (myDoubles2 < myDoubles1 && myDoubles2 < myDoubles3 && myDoubles2 < myDoubles4 && myDoubles2 < myDoubles5)
{
Console.WriteLine("myDoubles[2] is the lowest");
break;
}
while (myDoubles3 < myDoubles1 && myDoubles3 < myDoubles2 && myDoubles3 < myDoubles4 && myDoubles3 < myDoubles5)
{
Console.WriteLine("myDoubles[3] is the lowest");
break;
}
while (myDoubles4 < myDoubles1 && myDoubles4 < myDoubles2 && myDoubles4 < myDoubles3 && myDoubles4 < myDoubles5)
{
Console.WriteLine("myDoubles[4] is the lowest");
break;
}
while (myDoubles5 < myDoubles1 && myDoubles5 < myDoubles2 && myDoubles5 < myDoubles3 && myDoubles5 < myDoubles4)
{
Console.WriteLine("myDoubles[5] is the lowest");
break;
}
Upvotes: 0
Views: 236
Reputation: 1
I ended up doing this which fully satisfied the teacher's requirements. Thank you Greg and Ludovic for your examples. You were a very big help in figuring out the answer to this assignment!
int j = 0;
double min = 0;
for (int i = 0; i < myDoubles.Length; i++)
{
if (i == 0)
{
min = myDoubles[i];
}
else if (min > myDoubles[i])
{
min = myDoubles[i];
j = i;
}
}
Console.WriteLine("myDoubles[{0}] = {1} is the lowest value in the array", j, min);
Upvotes: 0
Reputation: 11480
Couldn't you simply do the following: This is to find the maximum.
public static double? FindMaximum(double[] container)
{
double? max = null;
foreach(var content in contaner)
if(content > max || max == null)
max = content as double;
return max;
}
This approach is quite simple, you iterate through each item in your array. Then compare the maximum to the content, if it is higher then the previous value it will modify the variable. Then returns the highest value out of your array.
It isn't one of the ideal approaches but one solution.
Upvotes: 1
Reputation: 11916
Try this:
double minValue(double[] myDoubles){
double min = Double.MaxValue;
for(int i=0; i<myDoubles.Length; i++)
{
if(myDoubles[i]<min)
min = myDoubles[i];
}
return min;
}
You need to set you min
variable to the MaxValue
because you don't know what the maximum value will be. For example if your user give you 1000 has the min value but you set the variable to 0 the final min value will be 0, witch is obviously wrong !
Upvotes: 1