Is there a way to find empty lines insinde of a matrix and insert values inside these empty space? I have a method that allows me to insert and erase values inside these matrix however when I erase something by id (the entire line) it creates an empty line and the insertion is always on the next line. What I need is method to search for empty space inside the matrix and to reajust this matrix. Here's the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace menu
{
class Program
{
private static int id = 1;
enum dataInsert { Id, Name, surname, address };
static void Main(string[] args)
{
string[,] matrix = new string[10, 4];
do { insertStudent(matrix);
Console.WriteLine();
insertStudent(matrix);
deleteByid(matrix);
listStudent(matrix);
} while (true);
}
static int generateId()
{
return id++;
}
static void insertStudent(string[,] matrix)
{
int n = generateId();
for (int j = 1; j < matrix.GetLength(1); j++)
{
matrix[n - 1, 0] = Convert.ToString(n);
do
{
Console.Write($"Insert {Enum.GetName(typeof(dataInsert), j)}: ");
matrix[n - 1, j] = Console.ReadLine();
} while (String.IsNullOrEmpty(matrix[n - 1, j]));
}
}
static int searchId(string[,] matrix)
{
int choosenId, index = -1;
do
{
Console.Write("Insert ID: ");
} while (!int.TryParse(Console.ReadLine(), out choosenId));
for (int i = 0; i < matrix.GetLength(0); i++)
{
if (Convert.ToString(choosenId) == matrix[i, 0])
{
index = i;
}
}
return index;
}
static void deleteByid(string[,] matrix)
{
int pos = searchId(matrix);
if (pos != -1)
{
for (int i = pos; i < pos + 1; i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
matrix[i, j] = null;
}
}
}
else { Console.WriteLine("ID does not exist"); }
}
static void listStudent(string[,] matrix)
{
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
Console.Write($"[{i}],[{j}]: {matrix[i, j]}\t");
}
Console.WriteLine("\n\t");
}
}
}
}