Realitiez
Realitiez

Reputation: 9

C# loop goes from 100 to 1 instead of 1 to 100

I'm creating a program in C# that has random values for weather for 100 days in the heat -10 to 50°C, i have it almost working but i have a problem, the output of the frosty days were in an ascending order but the days and their heat are in a descending order, how could i fix this? you may think this is a stupid question but i have tried to change the arguments inside the parenthesis of the second loop(days and their heat) but i can't seem to find the ascending order for it. if anyone can help please answer, here is my code:

Random x = new Random();
int day = 0;
int frostDaysCounter = 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[] array = Enumerable.Range(1, 101).ToArray();

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

    if (array[i] < 0) {
        Console.WriteLine("Day number: " + dagurP + " Temperature: " + array[i]);
        frostDays++;
    }
        day++;
}

    Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

    //THIS LOOP IS THE MAIN PROBLEM
    for (int a = 100; a >= 1; a--)
    {
        Console.WriteLine("Temperature day " + day + ". is: " + array[a]);
        sum += array[a];
        day--;
    }

    Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    double average = sum / 100;
    Console.WriteLine("Sum: " + sum);
    Console.WriteLine("Average temperature: " + average.ToString("F2") + " °C");
    Console.WriteLine("Frost was in total: " + frostDagar + " days.");

I know this may sound like a silly question with an easy answer but I'm just lost, if anyone can help please answer with the right loop format. Thanks.

Upvotes: 0

Views: 353

Answers (2)

user1968030
user1968030

Reputation:

You should change you fro loop:

for (int i = 1; i <= 100; i++)

Upvotes: 0

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101701

Change your for loop, start it from 1 like your first loop:

for (int a = 1; a <= 100; a++)

Upvotes: 1

Related Questions