Добавление единицы в массив по координатам - C#
Формулировка задачи:
Имеется двумерный массив, он заполнен нулями.
Пользователь вводит координаты x,y.
x = i - строка
y = j - столбец
Допустим пользователь ввел x = 2; y = 3. Значит в массив во 2-ую строку 3-ий столбец, нужно вместо нуля вставить единицу.
Пример:
0 0 0 0
0 0 1 0
0 0 0 0
0 0 0 0
Подскажите как реализовать
Мои наработки самой программы:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int x, y, n, i = 0, j = 0;
Console.WriteLine("Введите количество точек:");
n = Convert.ToInt32(Console.ReadLine());
int[,] C = new int[n, n];
Random rnd = new Random();
Console.WriteLine("Введите координаты точки (x,y):");
for (i = 0; i < n; i++)
{
x = Convert.ToInt32(Console.ReadLine());
y = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Матрица:");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
C[i, j] = rnd.Next(0);
Console.Write(C[i, j] + " ");
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}Решение задачи: «Добавление единицы в массив по координатам»
textual
Листинг программы
class Program
{
static void output(int[,] m)
{
for (int i = 0; i <= m.Rank; i++)
{
for (int j = 0; j <= m.Rank; j++)
Console.Write(m[i, j] + "\t");
Console.Write("\n");
}
}
static void Main(string[] args)
{
Console.Write("N = ");
int n = int.Parse(Console.ReadLine());
int[,] matr = new int[n, n];
Console.WriteLine("Исходная матрица:"); output(matr);
Console.Write("x = "); int x = int.Parse(Console.ReadLine()) - 1;
Console.Write("y = "); int y = int.Parse(Console.ReadLine()) - 1;
matr[x, y] = 1;
Console.WriteLine("Результат:"); output(matr);
Console.ReadLine();
}
}