Необходимо написать код сложения строки и столбца матрицы - C#

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

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

Элементы матрицы вводятся либо пользователем вручную с клавиатуры либо генерируются с помощью генератора случайных чисел. Выбор одного из способов предоставляется пользователю. Дан массив из n* n элементов, сложите строку i и со столбцом j. Номера i и j вводятся пользователем с клавиатуры. Результат разместите в новом одномерном массиве.

Решение задачи: «Необходимо написать код сложения строки и столбца матрицы»

textual
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace MatrixMultiplication
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             double[] result = new double[1];
  16.             Matrix matrix;
  17.             int row;
  18.             int column;
  19.             Console.Write("Введите количество строк в матрице: ");
  20.             row = (int)Math.Round((double)CorrectInput());
  21.             Console.Write("Введите количество столбцов в матрице: ");
  22.             column = (int)Math.Round((double)CorrectInput());
  23.             matrix = new Matrix(row, column);
  24.  
  25.             Console.Write("Хотите ввести данные самостоятельно? (Yes/No)");
  26.             switch (Console.ReadLine())
  27.             {
  28.                 case "Yes":
  29.                 {
  30.                     int totalItems = matrix.Row*matrix.Column;
  31.                     for (int i = 0; i < matrix.Row; i++)
  32.                     {
  33.                         for (int j = 0; j < matrix.Column; j++)
  34.                         {
  35.                             Console.Write("Пустых ячеек: " + totalItems + " Введите значение ячейки: ");
  36.                             matrix.SetItem(Convert.ToDouble(Console.ReadLine()), i, j);
  37.                             totalItems--;
  38.                         }
  39.                     }
  40.                     break;
  41.                 }
  42.                 case "No":
  43.                 {
  44.                     Random ran = new Random();
  45.                     for (int i = 0; i < matrix.Row; i++)
  46.                     {
  47.                         for (int j = 0; j < matrix.Column; j++)
  48.                         {
  49.                             matrix.SetItem(ran.Next(1000), i, j);
  50.                         }
  51.                     }
  52.                     break;
  53.                 }
  54.             }
  55.             PaintMatrix(matrix);
  56.  
  57.             Console.Write("Какую строку хотите сложить: ");
  58.             row = (int)Math.Round((double)CorrectInput());
  59.             Console.Write("Какой столбец хотите сложить: ");
  60.             column = (int)Math.Round((double)CorrectInput());
  61.             result = Summation(matrix, row, column);
  62.             Console.WriteLine("Сложив строку " + row + " с колонкой "+ column +" получаем: " + result[0]);
  63.            
  64.             Console.WriteLine("Нажмите любую клавишу чтобы выйти.");
  65.             Console.ReadLine();
  66.         }
  67.  
  68.         static double? CorrectInput()
  69.         {
  70.             double? input = null;
  71.             do
  72.             {
  73.                 try
  74.                 {
  75.                     input = Convert.ToDouble(Console.ReadLine());
  76.                 }
  77.                 catch (Exception)
  78.                 {
  79.                     Console.WriteLine("Введите число!");
  80.                 }
  81.                
  82.             }
  83.             while (input == null);
  84.  
  85.             return input;
  86.         }
  87.  
  88.         static double[] Summation(Matrix matrix, int row, int collumn)
  89.         {
  90.             double result = 0;
  91.             for (int i = 0; i < matrix.Column; i++)
  92.             {
  93.                 result += matrix.GetItem(row, i);
  94.             }
  95.             for (int i = 0; i < matrix.Row; i++)
  96.             {
  97.                 result += matrix.GetItem(i, collumn);
  98.             }
  99.             return new double[] {result};
  100.         }
  101.  
  102.         static void PaintMatrix(Matrix matrix )
  103.         {
  104.             for (int i = 0; i < matrix.Row; i++)
  105.             {
  106.                 for (int j = 0; j < matrix.Column; j++)
  107.                 {
  108.                     Console.Write(matrix.GetItem(i,j) + " ");
  109.                 }
  110.                 Console.WriteLine();
  111.             }
  112.         }
  113.  
  114.     }
  115.  
  116.     class Matrix
  117.     {
  118.         private double[,] mass;
  119.         public int Row { get; set; }
  120.         public int Column { get; set; }
  121.  
  122.         public Matrix(int row, int column)
  123.         {
  124.             this.Row = row;
  125.             this.Column = column;
  126.             mass = new double[row, column];
  127.         }
  128.  
  129.         public void SetItem(double value, int row, int column)
  130.         {
  131.             if (row >= this.Row || column >= this.Column)
  132.                 return;
  133.             mass[row, column] = value;
  134.         }
  135.  
  136.         public double GetItem(int row, int column)
  137.         {
  138.             if (row >= this.Row || column >= this.Column)
  139.                 return default(double);
  140.  
  141.             return mass[row, column];
  142.         }
  143.     }
  144. }

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


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

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

10   голосов , оценка 4.1 из 5

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

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

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