Найти минимальный элемент массива и его индексы каждой строке массива - C#
Формулировка задачи:
Дан массив А( N,M). Найти минимальный элемент массива и его индексы каждой строке массива.
помогите пожалуйста. никак не могу найти и исправить ошибки. и не пойму куда вписать индекы элемента. вот что получилось. и большая просьба коментировать максимально подробно, так как расзбираюсь в теме плохо.
Листинг программы
- static void Main(string[]) args)
- {
- double[,] a;
- int n, m, kolmin=0;
- Console. Write("строк? ");
- n = Convert.ToInt32(Console.ReadLine());
- Console. Write("столбцов? ");
- m = Convert.ToInt32(Console.ReadLine());
- a = new double[n, m];
- for(int i=0; i<=a.GetUpperBound(0); i++)
- for (int j = 0; j <= a.GetUpperBound(1); j++)
- {
- Console.Write("a[" +i+ "," +j+ "]=");
- a[i,j] = Convert.ToDouble(Console.ReadLine());
- }
- for(int i=0; i<a.GetLength(0); i++)
- j=1;
- int x = a[i,j];
- for(int j=2; j<a.Getlength(1); j++)
- {
- double[] min = new double[kolmin];
- if(a[i,j] < x) min[k++] = a[i,j];
- else min[k++] = x;
- foreach (double x in min)
- Console.WriteLine("x=" + x +);
- }
- Console.Readline();
- }
Решение задачи: «Найти минимальный элемент массива и его индексы каждой строке массива»
textual
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication1
- {
- internal class Program
- {
- public static void Main()
- {
- Massiv mas= new Massiv();
- for (int i = 0; i < mas.LengthI; i++)
- {
- for (int j = 0; j < mas.LengthJ; j++)
- {
- Console.WriteLine("a[" + i + "," + j + "]={0}",mas[i,j]);
- }
- }
- //поиск минимального элемента
- for (int i = 0; i < mas.LengthI; i++)
- {
- double min = double.MaxValue;
- int indexI = 0, indexJ = 0;
- for (int j = 0; j < mas.LengthJ; j++)
- {
- if (min > mas[i, j])
- {
- min = mas[i, j];
- indexI = i;
- indexJ = j;
- }
- }
- Console.WriteLine("min is={0}, and indes is={1},{2}", min, indexI, indexJ); //вывод минимального
- }
- Console.ReadKey();
- }
- }
- class Massiv
- {
- public double[,] a;
- public Massiv()
- {
- int n = 2;
- int m = 2;
- a = new double[n, m];
- Random rand = new Random();
- for (int i = 0; i < a.GetUpperBound(0); i++)
- {
- for (int j = 0; j < a.GetUpperBound(1); j++)
- {
- a[i, j] = rand.Next(-100, 100);
- }
- }
- }
- public Massiv(int n,int m)
- {
- a = new double[n, m];
- Random rand = new Random();
- for (int i = 0; i <= a.GetUpperBound(0); i++)
- {
- for (int j = 0; j <= a.GetUpperBound(1); j++)
- {
- a[i,j]=rand.Next(-100, 100);
- }
- }
- }
- // To enable client code to validate input
- // when accessing your indexer.
- public int LengthI
- {
- get { return a.GetLength(0); }
- }
- public int LengthJ
- {
- get { return a.GetLength(1); }
- }
- // Indexer declaration.
- // If index is out of range, the temps array will throw the exception.
- public double this[int indexI,int indexJ]
- {
- get
- {
- return a[indexI, indexJ];
- }
- set
- {
- a[indexI, indexJ] = value;
- }
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д