Untitled

 avatar
unknown
plain_text
3 years ago
2.9 kB
6
Indexable
using System;

namespace ConsoleApp8
{


    class Sample
    {

        int[,] a;
       
        public int[,] CreateMatrix(int a, int b)
        {
            this.a = new int[a, b];
            for(int i = 0; i < a; i++)
            {
                for(int j = 0; j < b; j++)
                {
                    Console.Write("enter value of a[{0},{1}]: ", i,j);
                    int c = Int32.Parse(Console.ReadLine());
                    this.a[i, j] = c;
                }
            }
            Print(a, b);
            return this.a;
        }

        public int[,] SwapLine(int a,int b , int m, int n)
        {
            int[] temp = new int[n];
            int[] temp2 = new int[n];

            // lấy giá trị của 2 dòng cần swap
            for(int i = 0; i < m; i++)
            {
                for(int j = 0; j < n; j++)
                {
                    // nếu số hàng hiện tại đúng là hàng cần lấy dữ liệu 
                    if (i == a)
                    {
                        temp[j] = this.a[i, j];
                        // gán dữ liệu vô biến tạm
                    }

                    if(i == b)
                    {
                        temp2[j] = this.a[i, j];
                    }
                }
            }

            // swap giá trị 2 hàng
            for (int i = 0; i < m; i++)
            {
                for(int j = 0; j < n; j++)
                {
                    // swap value 
                    if (i == a)
                    {
                        this.a[i, j] = temp2[j];
                    }

                    if (i == b)
                    {
                        this.a[i, j] = temp[j];
                    }
                }
            }

            Print(m, n);

            return this.a;
        }
        
        public void Print(int m, int n)
        {
            for(int i = 0; i <m; i++)
            {
                for(int j = 0; j < n; j++)
                {
                    Console.Write(this.a[i, j] + " ");
                }
                Console.WriteLine();
            }
        }

    }
    class Program
    {
        // function of bài 7
       

        static void Main(string[] args)
        {
            int a, b;
            Console.Write("Nhap so dong: ");
            a = Int32.Parse(Console.ReadLine());
            Console.Write("Nhap so cot: ");
            b = Int32.Parse(Console.ReadLine());

            Sample matrix = new Sample();
            matrix.CreateMatrix(a, b);

            Console.Write("Nhap 2 dong can doi: ");
            int m, n;
            m = Int32.Parse(Console.ReadLine());
            n = Int32.Parse(Console.ReadLine());

            matrix.SwapLine(m, n, a, b);
            


        }


    }


}
Editor is loading...