Необходимо написать код сложения строки и столбца матрицы - C#
Формулировка задачи:
Элементы матрицы вводятся либо пользователем вручную с клавиатуры либо генерируются с помощью генератора случайных чисел. Выбор одного из способов предоставляется пользователю.
Дан массив из n* n элементов, сложите строку i и со столбцом j. Номера i и j вводятся пользователем с клавиатуры. Результат разместите в новом одномерном массиве.
Решение задачи: «Необходимо написать код сложения строки и столбца матрицы»
textual
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Text;
- using System.Threading.Tasks;
- namespace MatrixMultiplication
- {
- class Program
- {
- static void Main(string[] args)
- {
- double[] result = new double[1];
- Matrix matrix;
- int row;
- int column;
- Console.Write("Введите количество строк в матрице: ");
- row = (int)Math.Round((double)CorrectInput());
- Console.Write("Введите количество столбцов в матрице: ");
- column = (int)Math.Round((double)CorrectInput());
- matrix = new Matrix(row, column);
- Console.Write("Хотите ввести данные самостоятельно? (Yes/No)");
- switch (Console.ReadLine())
- {
- case "Yes":
- {
- int totalItems = matrix.Row*matrix.Column;
- for (int i = 0; i < matrix.Row; i++)
- {
- for (int j = 0; j < matrix.Column; j++)
- {
- Console.Write("Пустых ячеек: " + totalItems + " Введите значение ячейки: ");
- matrix.SetItem(Convert.ToDouble(Console.ReadLine()), i, j);
- totalItems--;
- }
- }
- break;
- }
- case "No":
- {
- Random ran = new Random();
- for (int i = 0; i < matrix.Row; i++)
- {
- for (int j = 0; j < matrix.Column; j++)
- {
- matrix.SetItem(ran.Next(1000), i, j);
- }
- }
- break;
- }
- }
- PaintMatrix(matrix);
- Console.Write("Какую строку хотите сложить: ");
- row = (int)Math.Round((double)CorrectInput());
- Console.Write("Какой столбец хотите сложить: ");
- column = (int)Math.Round((double)CorrectInput());
- result = Summation(matrix, row, column);
- Console.WriteLine("Сложив строку " + row + " с колонкой "+ column +" получаем: " + result[0]);
- Console.WriteLine("Нажмите любую клавишу чтобы выйти.");
- Console.ReadLine();
- }
- static double? CorrectInput()
- {
- double? input = null;
- do
- {
- try
- {
- input = Convert.ToDouble(Console.ReadLine());
- }
- catch (Exception)
- {
- Console.WriteLine("Введите число!");
- }
- }
- while (input == null);
- return input;
- }
- static double[] Summation(Matrix matrix, int row, int collumn)
- {
- double result = 0;
- for (int i = 0; i < matrix.Column; i++)
- {
- result += matrix.GetItem(row, i);
- }
- for (int i = 0; i < matrix.Row; i++)
- {
- result += matrix.GetItem(i, collumn);
- }
- return new double[] {result};
- }
- static void PaintMatrix(Matrix matrix )
- {
- for (int i = 0; i < matrix.Row; i++)
- {
- for (int j = 0; j < matrix.Column; j++)
- {
- Console.Write(matrix.GetItem(i,j) + " ");
- }
- Console.WriteLine();
- }
- }
- }
- class Matrix
- {
- private double[,] mass;
- public int Row { get; set; }
- public int Column { get; set; }
- public Matrix(int row, int column)
- {
- this.Row = row;
- this.Column = column;
- mass = new double[row, column];
- }
- public void SetItem(double value, int row, int column)
- {
- if (row >= this.Row || column >= this.Column)
- return;
- mass[row, column] = value;
- }
- public double GetItem(int row, int column)
- {
- if (row >= this.Row || column >= this.Column)
- return default(double);
- return mass[row, column];
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д