Programming Basics SQL HTML CSS JavaScript Python C++ Java JavaFX Swing Problem Solving English English Conversations Computer Fundamentals Learn Typing

تحديات برمجيةالتحدي الخامس - حل التمرين الثالث بلغة C#

المطلوب

أكتب برنامج يقوم بترتيب جميع القيم الموجودة في مصفوفة أرقام ثنائية جاهزة من الأصغر إلى الأكبر.
قم بعرض القيم الموجودة في المصفوفة قبل الترتيب و بعد الترتيب.


الحل بلغة C#

using System;
class Program
{
static void Main(string[] args)
{
int[,] matrix = {
{5, 2, 4},
{1, 7, 3},
{9, 8, 6}
};
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
int temp;
int[] array = new int[rows * cols];
Console.WriteLine("Matrix before sorting");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
Console.Write(matrix[i, j] + " ");
}
Console.WriteLine();
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
array[j + (i * rows)] = matrix[i, j];
}
}
for (int i = 0; i < array.Length - 1; i++)
{
for (int j = i + 1; j < array.Length; j++)
{
if (array[j] < array[i])
{
temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
matrix[i, j] = array[j + (i * rows)];
}
}
Console.WriteLine("\nMatrix after sorting");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
Console.Write(matrix[i, j] + " ");
}
Console.WriteLine();
}
Console.ReadKey();
}
}
using System; class Program { static void Main(string[] args) { int[,] matrix = { {5, 2, 4}, {1, 7, 3}, {9, 8, 6} }; int rows = matrix.GetLength(0); int cols = matrix.GetLength(1); int temp; int[] array = new int[rows * cols]; Console.WriteLine("Matrix before sorting"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { Console.Write(matrix[i, j] + " "); } Console.WriteLine(); } for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { array[j + (i * rows)] = matrix[i, j]; } } for (int i = 0; i < array.Length - 1; i++) { for (int j = i + 1; j < array.Length; j++) { if (array[j] < array[i]) { temp = array[j]; array[j] = array[i]; array[i] = temp; } } } for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { matrix[i, j] = array[j + (i * rows)]; } } Console.WriteLine("\nMatrix after sorting"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { Console.Write(matrix[i, j] + " "); } Console.WriteLine(); } Console.ReadKey(); } }

سنحصل على النتيجة التالية عند التشغيل.

Matrix before sorting 
5 2 4 
1 7 3 
9 8 6 

Matrix after sorting 
1 2 3 
4 5 6 
7 8 9 
		

الدورات

أدوات مساعدة

أقسام الموقع

دورات
مقالات كتب مشاريع أسئلة