المطلوب
أكتب برنامج يعرّف مصفوفة إسمها matrix تتألف من 3 أسطر و 3 أعمدة.
بعدها يطلب من المستخدم إدخال عدد صحيح لكل عنصر فيها.
في الأخير, يعرض للمستخدم ناتج جمع قيم العناصر الموجودة في كل عامود فيها.
الحل بلغة C#
using System;
class Program
{
static void Main(string[] args)
{
int[,] matrix = new int[3, 3];
int s;
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
Console.Write("Enter matrix[" + i + "][" + j + "]: ");
matrix[i, j] = Int32.Parse(Console.ReadLine());
}
}
Console.WriteLine();
for (int j = 0; j < matrix.GetLength(1); j++)
{
s = 0;
for (int i = 0; i < matrix.GetLength(0); i++)
{
s += matrix[i, j];
}
Console.WriteLine("The sum of all elements in column " + j + ": " + s);
}
Console.ReadKey();
}
}
سنحصل على النتيجة التالية في حال تم إدخال نفس القيم التي تم تعليمها باللون الأصفر عند التشغيل.
Enter matrix[0][0]: 1
Enter matrix[0][1]: 2
Enter matrix[0][2]: 3
Enter matrix[1][0]: 4
Enter matrix[1][1]: 5
Enter matrix[1][2]: 6
Enter matrix[2][0]: 7
Enter matrix[2][1]: 8
Enter matrix[2][2]: 9
The sum of all elements in column 0: 12
The sum of all elements in column 1: 15
The sum of all elements in column 2: 18
Enter matrix[0][1]: 2
Enter matrix[0][2]: 3
Enter matrix[1][0]: 4
Enter matrix[1][1]: 5
Enter matrix[1][2]: 6
Enter matrix[2][0]: 7
Enter matrix[2][1]: 8
Enter matrix[2][2]: 9
The sum of all elements in column 0: 12
The sum of all elements in column 1: 15
The sum of all elements in column 2: 18


محرر الويب
نظام الألوان
محول الوحدات
محلل عناوين الشبكات