Заменить в массиве минимальное значение на максимальное - C#
Формулировка задачи:
Листинг программы
- int[] masiv = new int[]{ 2, -5, 50, 15, -20, 25, 12, -1, 120, 10 };
- int index, max, min;
- max = min = masiv[0];
- for ( index = 1; index < 10; index++ )
- {
- if ( masiv[index] > max ) max = masiv[index];
- if ( masiv[index] < min ) min = masiv[index];
- }
- label1.Text = " Max = " + max.ToString() +
Решение задачи: «Заменить в массиве минимальное значение на максимальное»
textual
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _9 /* 1.Дан одномерный массив
- 2. Найти максимальный и минимальный элемент и их индексы
- 3. Заменить минимальное на максимальное
- 4. Найти среднее значение массива.
- 5. Найти максимальное среди Отрицательных.
- 6. Минимальное среди положительных*/
- {
- class Program
- {
- static void Main(string[] args)
- {
- int countplus = 0;
- int countminus = 0;
- Console.ForegroundColor = ConsoleColor.Cyan;
- Console.Write("Введите размерность массива: ");
- Console.ForegroundColor = ConsoleColor.Green;
- int a = int.Parse(Console.ReadLine());
- Console.WriteLine("");
- Random r = new Random();
- int[] mas = new int[a];
- for (int i = 0; i < a; i++)
- {
- mas[i] = r.Next(-100, 100);
- if (mas[i] > 0)
- {
- countplus++;
- }
- if (mas[i] < 0)
- {
- countminus++;
- }
- }
- for (int i = 0; i < a; i++)
- {
- if (mas[i] == mas.Max() || mas[i] == mas.Min())
- {
- Console.ForegroundColor = ConsoleColor.Green;
- Console.Write("{0,4}", mas[i]);
- }
- else
- {
- Console.ForegroundColor = ConsoleColor.White;
- Console.Write("{0,4}", mas[i]);
- }
- }
- Console.WriteLine("");
- Console.WriteLine("");
- Console.ForegroundColor = ConsoleColor.Cyan;
- Console.Write("Максимальный элемент массива: ");
- Console.ForegroundColor = ConsoleColor.Green;
- Console.Write("{0}", mas.Max());
- Console.ForegroundColor = ConsoleColor.Cyan;
- Console.Write(" находиться под индексом: ");
- Console.ForegroundColor = ConsoleColor.Green;
- Console.Write("{0}", Array.IndexOf(mas, mas.Max()));
- Console.WriteLine("");
- Console.WriteLine("");
- Console.ForegroundColor = ConsoleColor.Cyan;
- Console.Write("Минимальный элемент массива: ");
- Console.ForegroundColor = ConsoleColor.Green;
- Console.Write("{0}", mas.Min());
- Console.ForegroundColor = ConsoleColor.Cyan;
- Console.Write(" находиться под индексом: ");
- Console.ForegroundColor = ConsoleColor.Green;
- Console.Write("{0}", Array.IndexOf(mas, mas.Min()));
- Console.WriteLine("");
- Console.WriteLine("");
- Console.ForegroundColor = ConsoleColor.Cyan;
- Console.Write("Среднее значение массива равно: ");
- Console.ForegroundColor = ConsoleColor.Green;
- Console.Write("{0}", mas.Average());
- Console.WriteLine("");
- Console.WriteLine("");
- Console.ForegroundColor = ConsoleColor.Cyan;
- Console.Write("countplus: ");
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("{0}", countplus);
- Console.WriteLine("");
- Console.ForegroundColor = ConsoleColor.Cyan;
- Console.Write("countminus: ");
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("{0}", countminus);
- Console.WriteLine("");
- Console.ForegroundColor = ConsoleColor.Cyan;
- Console.WriteLine("Массив из положительных значений!!!");
- Console.WriteLine("");
- Console.ForegroundColor = ConsoleColor.White;
- int[] masplus = new int[countplus];
- for (int i = 0, j = 0; i < countplus || j < a; j++)
- {
- if (mas[j] > 0)
- {
- masplus[i] = mas[j];
- Console.Write("{0,4}", masplus[i]);
- i++;
- }
- }
- Console.WriteLine("");
- Console.WriteLine("");
- Console.ForegroundColor = ConsoleColor.Cyan;
- Console.Write("Минимальное среди положительных: ");
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("{0}", masplus.Min());
- Console.WriteLine("");
- Console.WriteLine("");
- Console.ForegroundColor = ConsoleColor.Cyan;
- Console.WriteLine("Массив из отрицательных значений!!!");
- Console.WriteLine("");
- Console.ForegroundColor = ConsoleColor.White;
- int[] masminus = new int[countminus];
- for (int i = 0, j = 0; i < countminus || j < a; j++)
- {
- if (mas[j] < 0)
- {
- masminus[i] = mas[j];
- Console.Write("{0,4}", masminus[i]);
- i++;
- }
- }
- Console.WriteLine("");
- Console.WriteLine("");
- Console.ForegroundColor = ConsoleColor.Cyan;
- Console.Write("Максимальное среди отрицательных: ");
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("{0}", masminus.Max());
- Console.WriteLine("");
- Console.WriteLine("");
- Console.ReadLine();
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д