Найти минимальный элемент массива и его индексы каждой строке массива - C#

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

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

Дан массив А( N,M). Найти минимальный элемент массива и его индексы каждой строке массива. помогите пожалуйста. никак не могу найти и исправить ошибки. и не пойму куда вписать индекы элемента. вот что получилось. и большая просьба коментировать максимально подробно, так как расзбираюсь в теме плохо.
Листинг программы
  1. static void Main(string[]) args)
  2. {
  3. double[,] a;
  4. int n, m, kolmin=0;
  5. Console. Write("строк? ");
  6. n = Convert.ToInt32(Console.ReadLine());
  7. Console. Write("столбцов? ");
  8. m = Convert.ToInt32(Console.ReadLine());
  9. a = new double[n, m];
  10. for(int i=0; i<=a.GetUpperBound(0); i++)
  11. for (int j = 0; j <= a.GetUpperBound(1); j++)
  12. {
  13. Console.Write("a[" +i+ "," +j+ "]=");
  14. a[i,j] = Convert.ToDouble(Console.ReadLine());
  15. }
  16. for(int i=0; i<a.GetLength(0); i++)
  17. j=1;
  18. int x = a[i,j];
  19. for(int j=2; j<a.Getlength(1); j++)
  20. {
  21. double[] min = new double[kolmin];
  22. if(a[i,j] < x) min[k++] = a[i,j];
  23. else min[k++] = x;
  24. foreach (double x in min)
  25. Console.WriteLine("x=" + x +);
  26. }
  27. Console.Readline();
  28. }

Решение задачи: «Найти минимальный элемент массива и его индексы каждой строке массива»

textual
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.     internal class Program
  9.     {
  10.         public static void Main()
  11.         {
  12.            Massiv mas= new Massiv();
  13.  
  14.            for (int i = 0; i < mas.LengthI; i++)
  15.            {
  16.                
  17.                for (int j = 0; j < mas.LengthJ; j++)
  18.                {
  19.  
  20.                    Console.WriteLine("a[" + i + "," + j + "]={0}",mas[i,j]);
  21.                }
  22.                
  23.            }
  24.          
  25.             //поиск минимального элемента
  26.             for (int i = 0; i < mas.LengthI; i++)
  27.             {
  28.                 double min = double.MaxValue;
  29.                 int indexI = 0, indexJ = 0;
  30.                 for (int j = 0; j < mas.LengthJ; j++)
  31.                 {
  32.                     if (min > mas[i, j])
  33.                     {
  34.                         min = mas[i, j];
  35.                         indexI = i;
  36.                         indexJ = j;
  37.  
  38.                     }
  39.  
  40.                 }
  41.                 Console.WriteLine("min is={0}, and indes is={1},{2}", min, indexI, indexJ); //вывод минимального
  42.             }
  43.  
  44.  
  45.             Console.ReadKey();
  46.         }
  47.     }
  48.  
  49.     class Massiv
  50.     {
  51.         public double[,] a;
  52.         public Massiv()
  53.         {
  54.             int n = 2;
  55.             int m = 2;
  56.             a = new double[n, m];
  57.             Random rand = new Random();
  58.             for (int i = 0; i < a.GetUpperBound(0); i++)
  59.             {
  60.                 for (int j = 0; j < a.GetUpperBound(1); j++)
  61.                 {
  62.  
  63.                     a[i, j] = rand.Next(-100, 100);
  64.                 }
  65.             }
  66.         }
  67.  
  68.         public Massiv(int n,int m)
  69.         {
  70.             a = new double[n, m];
  71.             Random rand = new Random();
  72.             for (int i = 0; i <= a.GetUpperBound(0); i++)
  73.             {
  74.                 for (int j = 0; j <= a.GetUpperBound(1); j++)
  75.                 {
  76.                     a[i,j]=rand.Next(-100, 100);
  77.                    
  78.                 }
  79.             }
  80.         }
  81.  
  82.      
  83.         // To enable client code to validate input
  84.         // when accessing your indexer.
  85.         public int LengthI
  86.         {
  87.             get { return a.GetLength(0); }
  88.         }
  89.  
  90.         public int LengthJ
  91.         {
  92.             get { return a.GetLength(1); }
  93.         }
  94.         // Indexer declaration.
  95.         // If index is out of range, the temps array will throw the exception.
  96.         public double this[int indexI,int indexJ]
  97.         {
  98.             get
  99.             {
  100.                 return a[indexI, indexJ];
  101.             }
  102.  
  103.             set
  104.             {
  105.                 a[indexI, indexJ] = value;
  106.             }
  107.         }
  108.     }
  109. }

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


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

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

11   голосов , оценка 3.818 из 5

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

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

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