يمكن إنشاء برنامج لإجراء عمليات حسابية بين صفين في جدول في لغة البرمجة السي شارب #C باستخدام الكود التالي:
“`csharp
// Introduction to C# programming with Unity: Exercise 2.1
using System;
class Program
{
static void Main()
{
int[,] matrix = new int[3, 3]; // Create a 3×3 matrix
int row1, row2, column1, column2; // Variables to store the rows and columns to operate on
char operation; // Variable to store the operation to perform
// Fill the matrix with some test values
matrix[0, 0] = 1;
matrix[0, 1] = 2;
matrix[0, 2] = 3;
matrix[1, 0] = 4;
matrix[1, 1] = 5;
matrix[1, 2] = 6;
matrix[2, 0] = 7;
matrix[2, 1] = 8;
matrix[2, 2] = 9;
// Print the matrix
Console.WriteLine(“Original matrix:”);
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(matrix[i, j] + ” “);
}
Console.WriteLine();
}
// Ask the user for the rows and columns to operate on
Console.Write(“Enter the first row to operate on: “);
row1 = int.Parse(Console.ReadLine());
Console.Write(“Enter the first column to operate on: “);
column1 = int.Parse(Console.ReadLine());
Console.Write(“Enter the second row to operate on: “);
row2 = int.Parse(Console.ReadLine());
Console.Write(“Enter the second column to operate on: “);
column2 = int.Parse(Console.ReadLine());
// Ask the user for the operation to perform
Console.Write(“Enter the operation to perform (+ or -): “);
operation = char.Parse(Console.ReadLine());
// Perform the operation
switch (operation)
{
case ‘+’:
matrix[row1 – 1, column1 – 1] += matrix[row2 – 1, column2 – 1];
break;
case ‘-‘:
matrix[row1 – 1, column1 – 1] -= matrix[row2 – 1, column2 – 1];
break;
default:
Console.WriteLine(“Invalid operation.”);
break;
}
// Print the result
Console.WriteLine(“Result:”);
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(matrix[i, j] + ” “);
}
Console.WriteLine();
}
}
}
“`
تحتوي الشفرة على مصفوفة 3×3 يتم ملؤها بقيم اختبارية. ثم يُطلب من المستخدم تحديد صفين وعمودين في المصفوفة للقيام بالعملية الحسابية بينهما. يتم ثم طرح أو جمع القيمة في المصفوفة حسب العملية المحددة من قبل المستخدم. وأخيرًا ، يتم طباعة المصفوفة الناتجة.