Заменить в массиве минимальное значение на максимальное - C#

Узнай цену своей работы

Формулировка задачи:

Листинг программы
  1. int[] masiv = new int[]{ 2, -5, 50, 15, -20, 25, 12, -1, 120, 10 };
  2. int index, max, min;
  3. max = min = masiv[0];
  4. for ( index = 1; index < 10; index++ )
  5. {
  6. if ( masiv[index] > max ) max = masiv[index];
  7. if ( masiv[index] < min ) min = masiv[index];
  8. }
  9. label1.Text = " Max = " + max.ToString() +
2. Найти их индексы 3. Заменить минимальное на максимальное 4. Найти среднее значение массива. 5. Найти максимальное среди положительных.

Решение задачи: «Заменить в массиве минимальное значение на максимальное»

textual
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _9 /*     1.Дан одномерный массив
  8.                     2. Найти максимальный и минимальный элемент и  их индексы
  9.                     3. Заменить минимальное на максимальное
  10.                     4. Найти среднее значение массива.
  11.                     5. Найти максимальное среди Отрицательных.
  12.                     6.  Минимальное среди положительных*/
  13. {
  14.     class Program
  15.     {
  16.         static void Main(string[] args)
  17.         {
  18.             int countplus = 0;
  19.             int countminus = 0;
  20.  
  21.             Console.ForegroundColor = ConsoleColor.Cyan;
  22.             Console.Write("Введите размерность массива: ");
  23.             Console.ForegroundColor = ConsoleColor.Green;
  24.             int a = int.Parse(Console.ReadLine());
  25.             Console.WriteLine("");
  26.  
  27.             Random r = new Random();
  28.  
  29.  
  30.             int[] mas = new int[a];
  31.             for (int i = 0; i < a; i++)
  32.             {
  33.                 mas[i] = r.Next(-100, 100);
  34.                 if (mas[i] > 0)
  35.                 {
  36.                     countplus++;
  37.                 }
  38.                 if (mas[i] < 0)
  39.                 {
  40.                     countminus++;
  41.                 }
  42.             }
  43.  
  44.             for (int i = 0; i < a; i++)
  45.             {
  46.                 if (mas[i] == mas.Max() || mas[i] == mas.Min())
  47.                 {
  48.                     Console.ForegroundColor = ConsoleColor.Green;
  49.                     Console.Write("{0,4}", mas[i]);
  50.                 }
  51.                 else
  52.                 {
  53.                     Console.ForegroundColor = ConsoleColor.White;
  54.                     Console.Write("{0,4}", mas[i]);
  55.                 }
  56.             }
  57.             Console.WriteLine("");
  58.             Console.WriteLine("");
  59.  
  60.             Console.ForegroundColor = ConsoleColor.Cyan;
  61.             Console.Write("Максимальный элемент массива: ");
  62.             Console.ForegroundColor = ConsoleColor.Green;
  63.             Console.Write("{0}", mas.Max());
  64.             Console.ForegroundColor = ConsoleColor.Cyan;
  65.             Console.Write(" находиться под индексом: ");
  66.             Console.ForegroundColor = ConsoleColor.Green;
  67.             Console.Write("{0}", Array.IndexOf(mas, mas.Max()));
  68.             Console.WriteLine("");
  69.             Console.WriteLine("");
  70.  
  71.             Console.ForegroundColor = ConsoleColor.Cyan;
  72.             Console.Write("Минимальный элемент массива: ");
  73.             Console.ForegroundColor = ConsoleColor.Green;
  74.             Console.Write("{0}", mas.Min());
  75.             Console.ForegroundColor = ConsoleColor.Cyan;
  76.             Console.Write(" находиться под индексом: ");
  77.             Console.ForegroundColor = ConsoleColor.Green;
  78.             Console.Write("{0}", Array.IndexOf(mas, mas.Min()));
  79.             Console.WriteLine("");
  80.             Console.WriteLine("");
  81.  
  82.             Console.ForegroundColor = ConsoleColor.Cyan;
  83.             Console.Write("Среднее значение массива равно: ");
  84.             Console.ForegroundColor = ConsoleColor.Green;
  85.             Console.Write("{0}", mas.Average());
  86.             Console.WriteLine("");
  87.             Console.WriteLine("");
  88.  
  89.             Console.ForegroundColor = ConsoleColor.Cyan;
  90.             Console.Write("countplus: ");
  91.             Console.ForegroundColor = ConsoleColor.Green;
  92.             Console.WriteLine("{0}", countplus);
  93.             Console.WriteLine("");
  94.  
  95.             Console.ForegroundColor = ConsoleColor.Cyan;
  96.             Console.Write("countminus: ");
  97.             Console.ForegroundColor = ConsoleColor.Green;
  98.             Console.WriteLine("{0}", countminus);
  99.             Console.WriteLine("");
  100.  
  101.             Console.ForegroundColor = ConsoleColor.Cyan;
  102.             Console.WriteLine("Массив из положительных значений!!!");
  103.             Console.WriteLine("");
  104.  
  105.             Console.ForegroundColor = ConsoleColor.White;
  106.             int[] masplus = new int[countplus];
  107.             for (int i = 0, j = 0; i < countplus || j < a; j++)
  108.             {
  109.                 if (mas[j] > 0)
  110.                 {
  111.                     masplus[i] = mas[j];
  112.                     Console.Write("{0,4}", masplus[i]);
  113.                     i++;
  114.                 }
  115.             }
  116.             Console.WriteLine("");
  117.             Console.WriteLine("");
  118.  
  119.             Console.ForegroundColor = ConsoleColor.Cyan;
  120.             Console.Write("Минимальное среди положительных: ");
  121.             Console.ForegroundColor = ConsoleColor.Green;
  122.             Console.WriteLine("{0}", masplus.Min());
  123.             Console.WriteLine("");
  124.             Console.WriteLine("");
  125.  
  126.             Console.ForegroundColor = ConsoleColor.Cyan;
  127.             Console.WriteLine("Массив из отрицательных значений!!!");
  128.             Console.WriteLine("");
  129.  
  130.             Console.ForegroundColor = ConsoleColor.White;
  131.             int[] masminus = new int[countminus];
  132.             for (int i = 0, j = 0; i < countminus || j < a; j++)
  133.             {
  134.                 if (mas[j] < 0)
  135.                 {
  136.                     masminus[i] = mas[j];
  137.                     Console.Write("{0,4}", masminus[i]);
  138.                     i++;
  139.                 }
  140.             }
  141.             Console.WriteLine("");
  142.             Console.WriteLine("");
  143.  
  144.             Console.ForegroundColor = ConsoleColor.Cyan;
  145.             Console.Write("Максимальное среди отрицательных: ");
  146.             Console.ForegroundColor = ConsoleColor.Green;
  147.             Console.WriteLine("{0}", masminus.Max());
  148.             Console.WriteLine("");
  149.             Console.WriteLine("");
  150.  
  151.  
  152.  
  153.             Console.ReadLine();
  154.         }
  155.     }
  156. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

8   голосов , оценка 4.125 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы