How do you exclude a value of an Array?
arrays, c#
Solution
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;
}
Problem
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. 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. ``` 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; } } } ```