2013-07-10 75 views
5

如何創建排除最低溫度並計算平均溫度的方法。我只想提示而不是完整的解決方案,因爲我想自己解決我的編程問題。我只有大約10個班級......評論人們評論我的教授沒有講課,我讀過我的書多次回顧。如何排除數組的值?

我讓這個程序從用戶處獲取一個數字。該數字被添加到數組中。該數組用於創建類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; 
     } 
    } 
} 

回答

8

這是很容易與一個循環做:

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; 
} 
+1

+1:我比我所建議的更喜歡這個答案。 – Douglas

1

您需要添加錯誤處理,但是這可以幫助給你一個開始

var ints = new List<int>(); 
var newInts = ints.OrderBy(x => x).ToList(); 
newInts.RemoveAt(0); 
var avg = newInts.Average(); 
0

您可以用幾個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 
+0

如果存在多個相等的元素,那麼這將是無效的。不一定必須是最終的 – Sayse

+0

@Sayse。它取決於列表的特徵以及如何定義Min。我認爲這遵循數學/ lambda微積分的約定。儘管如此,我確實在編輯中指出了這一點。 – evanmcdonnal

+0

我注意到:)公平,你可以使用'平均()'而不是你的最後一行 – Sayse

0
public double Average() 
    { 
     var tempsToUse = temp.OrderByDescending(t => t).Take(temp.Length - 1); 

     return tempsToUse.Average(); 
    } 

編輯,包括全功能的簽名。

2

這裏有一點點不同的版本比道格拉斯發佈(當然他的版本是完全正常和很好的描述,我只是把它爲你評論)。它不使用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; 
} 

編輯:吉姆Mischel第一;-)。由於使用了temp.Length,而不是靜態數字(本例中爲7),他的版本也更加靈活。

+0

你爲什麼使用temp [0] ..我假設你假設temp [0]總是最低的一組溫度。或者你只是初始化總和的價值和最低? –

+0

@ZachB:只是初始化。這是一個稍微高效的方法,因爲它將迭代次數減少了一次。 – Douglas