Skip to content

Instantly share code, notes, and snippets.

@jpolivra
Created October 17, 2023 00:28
Show Gist options
  • Save jpolivra/f05a6ce3fe54f95bbd844f85f0953007 to your computer and use it in GitHub Desktop.
Save jpolivra/f05a6ce3fe54f95bbd844f85f0953007 to your computer and use it in GitHub Desktop.
Abstract data types: matrices.
using System;
public class AbstractDataTypes
{
public static void Main(string[] args)
{
// Matrix example
// 5 8 3 2 9
// 9 0 1 4 2
// 7 8 1 2 0
// 6 2 6 9 1
// 3 9 7 2 2
// Main matrix diagonal: 5 0 1 9 2
int[,] Matrix = new int[5, 5];
int[] ResultantVector = new int [5];
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
Console.Write($"Input the element ({i + 1},{j + 1}): ");
Matrix[i, j] = int.Parse(Console.ReadLine());
}
}
ResultantVector = ExtractMainDiagonal(Matrix);
Console.Write("\n\nMain diagonal: ");
for(int i = 0; i < ResultantVector.Length; i++)
{
Console.Write($"{ResultantVector[i], 7}");
}
Console.WriteLine("\n");
Console.ReadKey();
}
// Functions
static int[] ExtractMainDiagonal(int[,] M)
{
int[] Result = new int[5];
for(int i = 0; i < 5; i++)
{
Result[i] = M[i,i];
}
return Result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment