Hỏi đáp

Chia sẻ kiến thức, cùng nhau phát triển

giúp em bài C# với em đang cần gấp ạ ><

3 năm trước 568 lượt xem 3 bình luận 3 năm trước

CÂU HỎI CẦN GIÚP: SẮP XẾP CỘT GIẢM DẦN (MẢNG 2 CHIỀU C#)

using System;

namespace on_tap
{
    internal class Program
    {
        public static void NhapDuLieu(out int iDong, out int iCot, int[,] iMaTran)
        {
            int i, j;
            Console.Write("NHAP MA TRAN\n");
            Console.Write("Nhap so dong: ");
            iDong = int.Parse(Console.ReadLine());
            Console.Write("Nhap so cot: ");
            iCot = int.Parse(Console.ReadLine());
            for(i = 0; i < iDong; i++)
            {
                for(j = 0; j < iCot; j++)
                {
                    Console.Write("NHap vao vi tri iMaTran [{0},{1}]: ", i, j);
                    iMaTran[i,j] = int.Parse(Console.ReadLine());
                }    
            }    
        }
        public static void XuatDuLieu(int iDong, int iCot, int[,] iMaTran)
        {
            int i, j;
            Console.Write("MA TRAN BAN VUA NHAP\n");
            for(i = 0; i < iDong; i++)
            {
                for(j = 0; j < iCot; j++)
                {
                    Console.Write(iMaTran[i, j] + "\t");
                }
                Console.WriteLine();
            }    
        }
        public static void SapXepDongTangDan(int iDong, int iCot, int[,] iMaTran)
        {
            int i, j, k, iTam;
            for(k = 0; k < iDong; k++)
            {
                for(i = 0; i <= iCot - 1; i++)
                {
                    for(j = iCot - 1; j > i; j--)
                    {
                        if(iMaTran[k,j] < iMaTran[k, j-1])
                        {
                            iTam = iMaTran[k, j];
                            iMaTran[k, j] = iMaTran[k, j - 1];
                            iMaTran[k, j - 1] = iTam;
                        }    
                    }    
                }    
            }    
        }
        public static void SapXepCotGiamDan(int iDong, int iCot, int[,] iMaTran)
        {
            //Giúp mình phần này với ạ
        }
            static void Main(string[] args)
        {
            int[,] iMaTran;
            iMaTran = new int[50, 50];
            int iDong, iCot;
            NhapDuLieu(out iDong, out iCot, iMaTran);
            XuatDuLieu(iDong, iCot, iMaTran);
            SapXepDongTangDan(iDong, iCot, iMaTran);
            XuatDuLieu(iDong, iCot, iMaTran);
            Console.ReadKey();

        }
    }
}
 

 

Bình luận

Để bình luận, bạn cần đăng nhập bằng tài khoản Howkteam.

Đăng nhập
Hoang Vu đã bình luận 3 năm trước
public static void SapXepCotGiamDan(int iDong, int iCot, int[,] iMaTran)
{
//Giúp mình phần này với ạ
int i, j, k, iTam;
for (j = 0; j <= iCot - 1; j++)
{
for (i = 0; i <= iDong - 1; i++)
{
for (k = i + 1; k <= iDong - 1; k++)
{
if (iMaTran[i, j] < iMaTran[k, j])
{
iTam = iMaTran[i, j];
iMaTran[i, j] = iMaTran[k, j];
iMaTran[k, j] = iTam;
}
}
}
}
}

Câu hỏi mới nhất