How do you create a method that excludes the lowest temperature and calculate the average temp. i just want a hint and not the complete solution, as I want to solve my programming problems myself. I have had only about 10 classes.. to comment on people comments my professor does not lecture and i have read my book looked back through it multiple times.
如何创建一个排除最低温度并计算平均温度的方法。我只是想要一个提示,而不是完整的解决方案,因为我想自己解决我的编程问题。我只有大约10个班级...评论人们的评论,我的教授没有讲课,我读过我的书,多次回顾过去。
I made this program to take a number from a user. That number is added into the array. That array is used to create an instance of the class Temp
to print the lowest and highest temps.
我让这个程序从用户那里拿一个号码。该数字将添加到数组中。该数组用于创建类Temp的实例以打印最低和最高临时值。
class Program
{
static void Main(string[] args)
{
Console.Write("Enter a Temperature in Degrees:");
string n = Console.ReadLine();
int number = Convert.ToInt32( n);
Temp t = new Temp(100, 52, 98, 30, 11, 54, number);
Console.WriteLine("Lowest Temperature:{0}", t.lowest());
Console.WriteLine("Highest Temperature: {0}", t.highest());
Console.WriteLine("Average Temperature: {0}", t.Average());
}
public class Temp
{
private int[] temp = new int[7]; // array
public Temp(int d1, int d2, int d3, int d4, int d5, int d6, int d7) // constructor with 7 parameters
{
temp[0] = d1; // assigning constructor parameters to array
temp[1] = d2;
temp[2] = d3;
temp[3] = d4;
temp[4] = d5;
temp[5] = d6;
temp[6] = d7;
}
public int lowest() // returning the lowest value of the set of numbers
{
int smallest = 150;
for (int c = 0; c < 7; c++)
{
if (temp[c] < smallest)
{
smallest = temp[c];
}
}
return smallest;
}
public int highest()
{
int highest = -1;
for (int c = 0; c < 7; c++)
{
if (temp[c] > highest)
{
highest = temp[c];
}
}
return highest;
}
public double Average()
{
double average = 0;
for (int c = 0; c < 7; c++)
{
}
return average;
}
}
}
5 个解决方案
#1
8
This is very easy to do with a single loop:
使用单个循环非常容易:
public double Average()
{
// Initialize smallest with the first value.
// The loop will find the *real* smallest value.
int smallest = temp[0];
// To calculate the average, we need to find the sum of all our temperatures,
// except the smallest.
int sum = temp[0];
// The loop does two things:
// 1. Adds all of the values.
// 2. Determines the smallest value.
for (int c = 1; c < temp.Length; ++c)
{
if (temp[c] < smallest)
{
smallest = temp[c];
}
sum += temp[c];
}
// The computed sum includes all of the values.
// Subtract the smallest.
sum -= smallest;
double avg = 0;
// and divide by (Length - 1)
// The check here makes sure that we don't divide by 0!
if (temp.Length > 1)
{
avg = (double)sum/(temp.Length-1);
}
return avg;
}
#2
2
Here is a little bit different version than Douglas posted (of course his version is totally fine and well described, I just put it for your review). It doesn't use lowest() method call.
这是一个与道格拉斯发布的版本略有不同的版本(当然他的版本非常精细并且描述得很好,我只是把它放在你的评论中)。它不使用lowest()方法调用。
public double Average()
{
double sum = temp[0]; // sum of temperatures, starting from value of first one in array
double lowest = temp[0]; // buffer for lowest temperature value
for (int c = 1; c < 7; c++) // start loop from second position in array
{
if (temp[c] < lowest) // checking if next value in array is smaller than the lowest one so far...
{
lowest = temp[c]; // ...if so, value of variable lowest is changing
}
sum = sum + temp[c]; // adding temparatures value to variable sum, one by one
}
sum = sum - lowest; // at the end we substract lowest value from sum of all temperatures
double average = sum / 6; // average value calculation
return average;
}
EDIT: Jim Mischel was first ;-) . His version is also more flexible thanks to using temp.Length, not static number (7 in this case).
编辑:Jim Mischel是第一个;-)。由于使用了temp.Length,他的版本也更灵活,而不是静态数字(在这种情况下为7)。
#3
1
You need to add error handling but this can help give you a start
您需要添加错误处理,但这可以帮助您开始
var ints = new List<int>();
var newInts = ints.OrderBy(x => x).ToList();
newInts.RemoveAt(0);
var avg = newInts.Average();
#4
0
You can do this easily with a few LINQ functions. There are plenty of other ways to do this but they will all be similar. If there is more than one min value your average will not include any of them.
您可以使用一些LINQ函数轻松完成此操作。还有很多其他方法可以做到这一点,但它们都是相似的。如果有超过一分钟的值,您的平均值将不包括任何一个。
int min = myArray.Min(); // get the min element
var withoutMin = myArray.Where(x => x != min); // get a new list without the min element
double mean = withoutMin.Average(); // take the sum and divide it by the count
#5
0
public double Average()
{
var tempsToUse = temp.OrderByDescending(t => t).Take(temp.Length - 1);
return tempsToUse.Average();
}
Edited to include full function signature.
编辑包括全功能签名。
#1
8
This is very easy to do with a single loop:
使用单个循环非常容易:
public double Average()
{
// Initialize smallest with the first value.
// The loop will find the *real* smallest value.
int smallest = temp[0];
// To calculate the average, we need to find the sum of all our temperatures,
// except the smallest.
int sum = temp[0];
// The loop does two things:
// 1. Adds all of the values.
// 2. Determines the smallest value.
for (int c = 1; c < temp.Length; ++c)
{
if (temp[c] < smallest)
{
smallest = temp[c];
}
sum += temp[c];
}
// The computed sum includes all of the values.
// Subtract the smallest.
sum -= smallest;
double avg = 0;
// and divide by (Length - 1)
// The check here makes sure that we don't divide by 0!
if (temp.Length > 1)
{
avg = (double)sum/(temp.Length-1);
}
return avg;
}
#2
2
Here is a little bit different version than Douglas posted (of course his version is totally fine and well described, I just put it for your review). It doesn't use lowest() method call.
这是一个与道格拉斯发布的版本略有不同的版本(当然他的版本非常精细并且描述得很好,我只是把它放在你的评论中)。它不使用lowest()方法调用。
public double Average()
{
double sum = temp[0]; // sum of temperatures, starting from value of first one in array
double lowest = temp[0]; // buffer for lowest temperature value
for (int c = 1; c < 7; c++) // start loop from second position in array
{
if (temp[c] < lowest) // checking if next value in array is smaller than the lowest one so far...
{
lowest = temp[c]; // ...if so, value of variable lowest is changing
}
sum = sum + temp[c]; // adding temparatures value to variable sum, one by one
}
sum = sum - lowest; // at the end we substract lowest value from sum of all temperatures
double average = sum / 6; // average value calculation
return average;
}
EDIT: Jim Mischel was first ;-) . His version is also more flexible thanks to using temp.Length, not static number (7 in this case).
编辑:Jim Mischel是第一个;-)。由于使用了temp.Length,他的版本也更灵活,而不是静态数字(在这种情况下为7)。
#3
1
You need to add error handling but this can help give you a start
您需要添加错误处理,但这可以帮助您开始
var ints = new List<int>();
var newInts = ints.OrderBy(x => x).ToList();
newInts.RemoveAt(0);
var avg = newInts.Average();
#4
0
You can do this easily with a few LINQ functions. There are plenty of other ways to do this but they will all be similar. If there is more than one min value your average will not include any of them.
您可以使用一些LINQ函数轻松完成此操作。还有很多其他方法可以做到这一点,但它们都是相似的。如果有超过一分钟的值,您的平均值将不包括任何一个。
int min = myArray.Min(); // get the min element
var withoutMin = myArray.Where(x => x != min); // get a new list without the min element
double mean = withoutMin.Average(); // take the sum and divide it by the count
#5
0
public double Average()
{
var tempsToUse = temp.OrderByDescending(t => t).Take(temp.Length - 1);
return tempsToUse.Average();
}
Edited to include full function signature.
编辑包括全功能签名。