Question

I'm creating a weather program with random heat from -10 to 50 °C. i have it working but i need to find the specific days there was frost(under 0; -1, -5, etc...). i have a counter for how many frost days there are but i can't seem to find out how to get those specific days and print them on the screen. here's my code:

Random x = new Random();
int day = 1;
int frostDays = 0;
double sum = 0;
int[] days = {5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100};

int[] heat = new int[100];

for (int i = 1; i <= 100; i++)
{
    heat[0] = x.Next(-10, 51);

    if (heat[0] < 0)
    {
        //Console.WriteLine("Day number " + day + " heat " + heat[0]);
        frostDays++;
    }
    Console.Write(heat[0] + " ** ");
    if (days.Contains(day))
    {
        Console.WriteLine("\n");
    }
    //Console.WriteLine("Heat day " + day + ". is: " + heat[0]);

    sum += heat[0];
    day++;
}

double average = sum / 100;

Console.WriteLine("Heat sum: " + sum);
Console.WriteLine("average heat: " + average.ToString("F2") + " °C");
Console.WriteLine("frost days: " + frostDays);

OUTPUT: http://oi57.tinypic.com/mw4myv.jpg

I'm trying to retrieve the specific days(day numbers) which are frost(under 0°C) with their heat and print it on the screen, i can't seem to find out how, if anyone can help, please answer, greatly appreciated.

Was it helpful?

Solution

I suggest you change your structure to a more object oriented approach.

You should create a Day object, which will contain:

  • The Day's Temperature
  • If the Day had frost

This can be represented in a small class:

public class Day
{
    public int Temperature { get; set; }
    public bool Frost { get { return Temperature < 0; } }

    public Day(int temperature)
    {
        Temperature = temperature;
    }
}

Now that you have something to store your data in, you need to work on the logic of the program. (Which no offense, is not very well structured)

Start off by giving your program a few variables:

private const totalDays = 100; //Replace your hardcoded "100"s with this
private const int minTemp = -10; //Pretty self explanitory, the min and max temperatures for a day to generate
private const int maxTemp = 50;

Because your question states you are looking for frost days, I am going to modify your logic to only print them. Also note that you are incorrectly accessing your arrays by using heat[0], that will always get the first element at the 0 position, when you should be using heat[i] to get the value at the ith position.

The code should be as follows:

for (int i = 0; i < totalDays; i++)
{
    days[i] = new Day(x.Next(minTemp, maxTemp + 1)); //Create a day with random temperature
    if (days[i].Frost) //The "Frost" propery returns true if the "Temperature" is below 0
    {
         Console.WriteLine("Day {0} is frosty!", i);
    }
}

Notice how I modified your for loop to start at 0, rather than 1, as this is the start of the array. I also changed x.Next to take the new minTemp and maxTemp values into account.


If you want to get and process just the frost items, you can simply loop through them and check if a Days Frost property is true.

So to recap, your code should look like this:

class Program
{
    private const int totalDays = 100;
    private const int minTemp = -10;
    private const int maxTemp = 50;
    private static Day[] days = new Day[totalDays];
    private static Random x = new Random();

    static void Main(string[] args)
    {
        double sum = 0;

        for (int i = 0; i < totalDays; i++)
        {
            days[i] = new Day(x.Next(minTemp, maxTemp + 1)); //Create a day with random temperature
            sum += days[i].Temperature;
            Console.WriteLine("Day {0} is {1} degrees and is{2} frosty", i, days[i].Temperature, days[i].Frost ? string.Empty : " not");
        }

        double average = sum / totalDays;

        Console.WriteLine("Total Temperature: {0} °C", sum);
        Console.WriteLine("Average Temperature {0} °C", average.ToString("F2"));

        for (int i = 0; i < totalDays; i++)
        {
            if (days[i].Frost)
            {
                //Do something
            }
        }

        Console.ReadLine();
    }
}
public class Day
{
    public int Temperature { get; set; }
    public bool Frost { get { return Temperature < 0; } }

    public Day(int temperature)
    {
        Temperature = temperature;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top