using System.Collections.Generic;
public class MatrixTransposer
public static void TransposeMatrix()
Console.WriteLine("Enter the number of rows:");
if (!int.TryParse(Console.ReadLine(), out int numRows) || numRows <= 0)
Console.WriteLine("Invalid number of rows. Please enter a positive integer.");
List<object[]> matrix = new List<object[]>();
for (int i = 0; i < numRows; i++)
Console.WriteLine($"Enter the elements for row {i + 1} (comma-separated, can include strings, numbers, true/false, or function names):");
string rowInput = Console.ReadLine();
if (string.IsNullOrWhiteSpace(rowInput))
matrix.Add(new object[0]);
string[] elements = rowInput.Split(',');
object[] row = new object[elements.Length];
for (int j = 0; j < elements.Length; j++)
string element = elements[j].Trim();
if (string.IsNullOrEmpty(element))
if (double.TryParse(element, out double number))
else if (bool.TryParse(element, out bool boolean))
row[j] = boolean ? 1.0f : 0.0f;
else if (element.StartsWith("()=>"))
string functionBody = element.Substring(4);
if (functionBody.StartsWith("\"") && functionBody.EndsWith("\""))
row[j] = functionBody.Substring(1, functionBody.Length - 2);
row[j] = Convert.ToSingle(new System.Data.DataTable().Compute(functionBody, null));
Console.WriteLine($"Error executing function '{element}': {ex.Message}");
row[j] = element.ToUpper();
List<object[]> transformedMatrix = TransformMatrix(matrix);
Console.WriteLine("\nTransformed Matrix:");
PrintMatrix(transformedMatrix);
List<object[]> transposedMatrix = Transpose(transformedMatrix);
Console.WriteLine("\nTransposed Matrix:");
PrintMatrix(transposedMatrix);
public static List<object[]> TransformMatrix(List<object[]> matrix)
List<object[]> transformedMatrix = new List<object[]>();
foreach (object[] row in matrix)
object[] transformedRow = new object[row.Length];
for (int i = 0; i < row.Length; i++)
transformedRow[i] = null;
else if (row[i] is string)
transformedRow[i] = ((string)row[i]).ToUpper();
transformedRow[i] = (float)(int)row[i];
else if (row[i] is double)
transformedRow[i] = (float)(double)row[i];
transformedRow[i] = ((bool)row[i]) ? 1.0f : 0.0f;
transformedRow[i] = row[i];
transformedMatrix.Add(transformedRow);
return transformedMatrix;
public static List<object[]> Transpose(List<object[]> matrix)
if (matrix == null || matrix.Count == 0)
return new List<object[]>();
foreach (object[] row in matrix)
if (row != null && row.Length > maxRowLength)
maxRowLength = row.Length;
List<object[]> transposedMatrix = new List<object[]>();
for (int j = 0; j < maxRowLength; j++)
object[] newRow = new object[matrix.Count];
for (int i = 0; i < matrix.Count; i++)
if (matrix[i] != null && j < matrix[i].Length)
newRow[i] = matrix[i][j];
transposedMatrix.Add(newRow);
public static void PrintMatrix(List<object[]> matrix)
foreach (object[] row in matrix)
Console.WriteLine("null");
for (int i = 0; i < row.Length; i++)
Console.Write(row[i] != null ? row[i].ToString() : "null");
public static void Main(string[] args)