Larry Larry - 1 month ago 10
C# Question

Code not providing desired output

I am currently trying to figure out where I went wrong in this logic of code for the assignment. Any remarks or advice would be appreciated!

The output I keep getting no matter what input I put in keeps giving me:


average temperature 0,

highest temperature 5,

lowest temperature 0,

average excluding lowest 0,

number of cold days 10


First Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TemperatureAverager
{
class Temperatures
{
public double[] weeksTemperatures;
public double threshTemp;
public double average;
public double averageExcludingLowest;
public double highest;
public double lowest;
public int numOfThreshs;




public double[] WeeksTemperatures
{
get
{
return weeksTemperatures;
}
}
public double ThreshTemp
{
get
{
return threshTemp;
}
}
public double Average
{
set
{
average = value;
}
get
{
return average;
}
}
public double AverageExcludingLowest
{
set
{
averageExcludingLowest = value;
}
get
{
return averageExcludingLowest;
}
}
public double Highest
{
set
{
highest = value;
}
get
{
return highest;
}
}
public double Lowest
{
set
{
lowest = value;
}
get
{
return lowest;
}
}
public int NumberOfThreshs
{
set
{
numOfThreshs = value;
}
get
{
return numOfThreshs;
}
}
public Temperatures()
{
}
public Temperatures(double[] wTemperatures, double threshT)
{
weeksTemperatures = wTemperatures;
threshTemp = threshT;
}
public double DetermineAverage()
{
double average = 0;
for (int x = 0; x < 7; x++)
{
average = average + weeksTemperatures[x];
}
average = average / 7;
return average;
}
public double DetermineAverageExcludingLowest()
{
for (int x = 0; x < 7; x++)
{
averageExcludingLowest = averageExcludingLowest + weeksTemperatures[x];
if (weeksTemperatures[x] < lowest)
{
lowest = weeksTemperatures[x];
}
}
averageExcludingLowest = ((averageExcludingLowest - lowest) / 7); // calculate average excluding lowest temperature
return averageExcludingLowest;
}
public double DetermineLowest()
{
for (int x = 0; x < 7; x++) //Traverse through the week's temperatures
{
if (weeksTemperatures[x] < lowest) //find the lowest temperature of the week
{
lowest = weeksTemperatures[x]; //and set it to lowest
}
}
return lowest;
}
public double DetermineHighest()
{
for (int x = 0; x < 7; x++) //Traverse through the week's temperatures
{
if (weeksTemperatures[x] > highest) //find the highest temperature of the week
{
highest = weeksTemperatures[x]; //and set it to highest
}
}
return highest;
}
public double DetermineNumberOfThreshs()
{
for (int x = 0; x < 7; x++) //Traverse through the week's temperatures
{
if (weeksTemperatures[x] < threshTemp) //find the lowest temperature of the week
{
numOfThreshs++;
}
}
return numOfThreshs;
}
public override string ToString()
{
return "=====================\nWeekly Statistics\n" + "---------------------\n" + "Average Temperature: " + average + "\nHighest Temperature: "
+ highest + "\nLowest Temperature: " + lowest + "\nAvg. Excl. Lowest: " + averageExcludingLowest + "\n# of Cold Days: " + numOfThreshs + "\n====================="; //Formats and the invoice to be printed
}
}
}


Second Class

namespace TemperatureAverager
{
class TemperatureApp
{
static void Main(string[] args)
{
double[] week = new double[7];
string[] days = new string[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; //array to track days of week
for (int x = 0; x < 7; x++)
{
Console.Write("What was the temperature on " + days[x] + "?: ");
string userTemperatureInput = Console.ReadLine();
week[x] = double.Parse(userTemperatureInput);
}
Console.Write("How cold is too cold?: ");
string userThreshInput = Console.ReadLine();
double thresh = double.Parse(userThreshInput);
Temperatures weekOne = new Temperatures(week, thresh);
Console.WriteLine(weekOne.ToString());
Console.ReadLine();
}
}
}

Answer

Class one

public class Temperatures
{
    private double sum;
    private int daysInWeek;

    public double[] WeeksTemperatures { get; set; }
    public double ThreshTemp { get; set; }
    public double Average { get; set; }
    public double AverageExcludingLowest { get; set; }
    public double Highest { get; set; }
    public double Lowest { get; set; }
    public int NumOfThreshs { get; set; }

    public Temperatures(double[] wTemperatures, double threshT)
    {
        this.WeeksTemperatures = wTemperatures;
        this.ThreshTemp = threshT;
        sum = 0.0;
        daysInWeek = 7;
    }

    public void GetWeekStatistics()
    {
        GetSum();
        DetermineLowest();
        DetermineHighest();
        DetermineAverage();
        DetermineAverageExcludingLowest();
        DetermineNumberOfThreshs();
    }

    private void GetSum()
    {
        for (int x = 0; x < daysInWeek; x++) //Traverse through the week's temperatures
        {
            this.sum = this.sum + this.WeeksTemperatures[x];
        }
    }

    public void DetermineLowest()
    {
        for (int x = 0; x < daysInWeek; x++) //Traverse through the week's temperatures
        {
            this.Lowest = this.WeeksTemperatures[0];
            if (this.WeeksTemperatures[x] < this.Lowest)   //find the lowest temperature of the week
            {
                this.Lowest = this.WeeksTemperatures[x];   //and set it to lowest
            }
        }
    }

    public void DetermineHighest()
    {
        for (int x = 0; x < daysInWeek; x++) //Traverse through the week's temperatures
        {
            this.Highest = this.WeeksTemperatures[0];

            if (this.WeeksTemperatures[x] > this.Highest)  //find the highest temperature of the week
            {
                this.Highest = this.WeeksTemperatures[x];  //and set it to highest
            }
        }
    }

    private void DetermineAverage()
    {
        this.Average = this.sum / daysInWeek;
    }

    public void DetermineAverageExcludingLowest()
    {
        this.AverageExcludingLowest = ((this.sum - this.Lowest) / daysInWeek);   // calculate average excluding lowest temperature
    }

    public void DetermineNumberOfThreshs()
    {
        for (int x = 0; x < daysInWeek; x++) //Traverse through the week's temperatures
        {
            if (this.WeeksTemperatures[x] < this.ThreshTemp)   //find the lowest temperature of the week
            {
                this.NumOfThreshs++;
            }
        }
    }
    public override string ToString()
    {
        return "=====================\nWeekly Statistics\n" + "---------------------\n" + "Average Temperature: " + this.Average + "\nHighest Temperature: "
        + this.Highest + "\nLowest Temperature: " + this.Lowest + "\nAvg. Excl. Lowest: " + this.AverageExcludingLowest + "\n# of Cold Days: " + this.NumOfThreshs + "\n====================="; //Formats and the invoice to be printed
    }
}

Change in class two

Temperatures weekOne = new Temperatures(week, thresh);
weekOne.GetWeekStatistics();
Comments