O
O
onixela2016-11-06 20:03:29
Arrays
onixela, 2016-11-06 20:03:29

How to find max and min value C#?

You also need to write the day of the week on which the largest and smallest values ​​were entered. In my version, it calculates only the position, I can’t understand how I can write the day of the week due to this.

Console.WriteLine("Введите температуру воздуха за неделю: ");

            string[] dayOfWeek = new string[] { "Понедельник", "Вторник", "Среда", "Четверг", "Пятница", "Суббота", "Воскресенье" };
            int[] temp = new int[dayOfWeek.Length];

            double count = 0;

            for (int i = 0; i < dayOfWeek.Length; i++)
            {


                Console.Write("{0,-12}: ", dayOfWeek[i]);
                temp[i] = Convert.ToInt32(Console.ReadLine());
                count += temp[i];


            }

            MyArray.PrintArray(temp);


            Console.WriteLine("Средняя температура за неделю: {0:f2} градусов", count / dayOfWeek.Length);

            int max;
            max = temp[0];
            int pos = 0;

            for (int i = 1; i < temp.Length; i++)
            {
                if (temp[i] > max)
                {
                    max = temp[i];
                    pos = i + 1;
                }
            }

            Console.WriteLine("Максимальная температура: {0} - {1} градус(-ов)", pos, max);

            Console.ReadKey();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Grekhov, 2016-11-12
@Sterk

Oh, these students, how are you going to work then?

using System;
using System.Linq;
using System.Collections.Generic;
          
public class Program
{
  public static void Main()
  {
    string[] dayOfWeek = new string[] { "Понедельник", "Вторник", "Среда", "Четверг", "Пятница", "Суббота", "Воскресенье" };
    
    Console.WriteLine("Введите температуру воздуха за неделю: ");
    
    var temp = new List<int>();
    var r = new Random();
    
    foreach(var day in dayOfWeek){
    //	Console.Write("{0,-12}: ", day);
    //	temp.Add(Convert.ToInt32(Console.ReadLine()));
      temp.Add(r.Next(-10,10));
    }
    
    Console.WriteLine(string.Join(", ", temp));
    Console.WriteLine();
    //Для обычных людей
    Console.WriteLine("Средняя температура за неделю: {0} градусов", temp.Average());
    Console.WriteLine("Максимальная температура: {0} градус(-ов)", temp.Max());
    Console.WriteLine("Минимальная температура: {0} градус(-ов)", temp.Min());
    Console.WriteLine();
    //Для студентов
    {
      double sum = 0;
      for(int i = 0; i < temp.Count; i++){
        sum += temp[i];
      }
      Console.WriteLine("Средняя температура за неделю: {0} градусов", sum / temp.Count);
    }
    {
      int pos = 0, max = temp[0];
      for(int i = 1; i < temp.Count; i++){
        if(max < temp[i]){
          pos = i;
          max = temp[i];
        }
      }
      Console.WriteLine("Максимальная температура: {0} градус(-ов) позиция {1}", max, pos+1);
    }
    {
      int pos = 0, min = temp[0];
      for(int i = 1; i < temp.Count; i++){
        if(min > temp[i]){
          pos = i;
          min = temp[i];
        }
      }
      Console.WriteLine("Минимальная температура: {0} градус(-ов) позиция {1}", min, pos+1);
    }
    
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question