วันจันทร์ที่ 2 กันยายน พ.ศ. 2556

ใบงานที่ 7.5 การคูณแมตริกซ์

ใบงานที่ 7.5
การคูณแมตริกซ์







จงเขียนโปรแกรมเพื่อคูณแมตริกซ์ขนาด 2×2 และแสดงผลลัพธ์ออกทางหน้าจอ
ตัวอย่างผลการทำงาน
Enter matrix A
Enter element[1,1]: 1
Enter element[1,2]: 2
Enter element[2,1]: 3
Enter element[2,2]: 4
Enter matrix B
Enter element[1,1]: 5
Enter element[1,2]: 6
Enter element[2,1]: 7
Enter element[2,2]: 8
Matrix A*B is
  19 22
  43 50


จากนั้นคัดลอกโปรแกรมลงในช่องว่าง โดยไม่ต้องลอกเมท็อด ShowMatrix และ/หรือ
ReadMatrix หากนำมาใช้โดยไม่มีการเปลี่ยนแปลง


using System;
class Matrix
{
    static int[,] ReadMatrix(int nrows, int ncols)
    {
        int[,] m = new int[nrows, ncols];
        for (int i = 0; i < nrows; i++)
        {
            for (int j = 0; j < ncols; j++)
            {
                Console.Write("Enter element[{0},{1}]: ", i + 1, j + 1);

                m[i, j] = int.Parse(Console.ReadLine());
            }
        }
        return m;
    }
  
    static void Main()
    {
        int[,] A,B,i,j;

        Console.WriteLine("Enter matrix A");
        A = ReadMatrix(2, 2);
        Console.WriteLine();
        Console.WriteLine();
        Console.WriteLine("Enter matrix B");
        B = ReadMatrix(2, 2);

        int det = (A[0, 0] * B[0, 0]) + (A[0, 1] * B[1, 0]);
        int sum = (A[0, 0] * B[0, 1]) + (A[0, 1] * B[1, 1]);
        int cat = (A[1, 0] * B[0, 0]) + (A[1, 1] * B[1, 0]);
        int dog = (A[1, 0] * B[0, 1]) + (A[1, 1] * B[1, 1]);
        Console.WriteLine();
                Console.WriteLine("Matrix A*B is");
                Console.WriteLine("  {0}  {1}", det, sum );
                Console.Write("  {0}  {1}", cat, dog);
        Console.Read();
    }
}






ใบงานที่ 7.4 ดีเทอร์มิแนนท์ของแมตริกซ์

ใบงานที่ 7.4
ดีเทอร์มิแนนท์ของแมตริกซ์
ดีเทอร์มิแนนท์ (determinant) ของแมตริกซ์ขนาด 2×2 ซึ่งมีสูตรการคำนวณดังนี้


จงเขียนโปรแกรมเพื่อนำเข้าข้อมูลแมตริกซ์ขนาด 2×2 จากผู้ใช้ คำนวณดีเทอร์มิแนนท์และแสดงผลลัพท์
ตัวอย่างผลการทำงาน
Enter matrix A
Enter element[1,1]: 1
Enter element[1,2]: 2
Enter element[2,1]: 3
Enter element[2,2]: 4
The determenant of A is -2
จากนั้นคัดลอกโปรแกรมลงในช่องว่าง โดยไม่ต้องลอกเมท็อด ShowMatrix และ/หรือ
ReadMatrix หากนำมาใช้โดยไม่มีการเปลี่ยนแปลง

using System;
class Matrix
{
    static int[,] ReadMatrix(int nrows, int ncols )
    {
        int[,] m = new int[nrows, ncols];
        for (int i = 0; i < nrows; i++)
        {
            for (int j = 0; j < ncols; j++)
            {
                Console.Write("Enter element[{0},{1}]: ", i + 1, j + 1);
                m[i, j] = int.Parse(Console.ReadLine());
            }
        }
        return m;
    }
    static void Main()
    {
        int[,] A;
       
        Console.Write("Enter matrix ");
        Console.ReadLine();
        A = ReadMatrix(2,2);
        int det = A[0, 0] * A[1, 1] - A[0, 1] * A[1, 0];
        Console.Write("The determenant of A is {0}", det);
        Console.Read();
    }
}






ใบงานที่ 7.3 แมตริกซ์ทรานสโพส

ใบงานที่ 7.3
แมตริกซ์ทรานสโพส
ให้ผู้เรียนศึกษาใบความรู้ที่ 7.2 จากนั้นสร้างโปรเจ็กต์และเขียนโปรแกรมจากโจทย์ที่กำหนดให้ต่อไปนี้
เพิ่มเมท็อดชื่อ TransposeMatrix ลงในโปรแกรมจากแบบฝึกหัดที่แล้ว เพื่อคำนวณทรานสโพส
แมตริกซ์ (การสลับเปลี่ยนแถวและคอลัมน์) จากแมตริกซ์ที่รับเข้ามา ตัวเมท็อดจะรับแมตริกซ์ในรูป
พารามิเตอร์แบบอาเรย์สองมิติและส่งค่ากลับเป็นอาเรย์ตัวใหม่ที่เก็บค่าแมตริกซ์ที่ถูกทรานสโพสแล้ว
using System;
class Matrix {

  // คัดลอกเมท็อด ShowMatrix และ ReadMatrix
  // จากแบบฝึกหัดที่แล้วมาปะในตำแหน่งนี้

  static int[,] TransposeMatrix(int[,] m) {
    int[,] mt = new               int[____________________,____________________];
for (int i = 0; i < m.GetLength(0); i++)
for (int j = 0; j < m.GetLength(1); j++)
______________________________________________________;
return mt;
}
static void Main() {
int num_rows, num_cols;
int[,] A, At;
Console.Write("How many rows? ");
num_rows = int.Parse(Console.ReadLine());
Console.Write("How many columns? ");
num_cols = int.Parse(Console.ReadLine());
A = ReadMatrix(num_rows, num_cols);
Console.WriteLine("Matrix A is");
ShowMatrix(A);
Console.WriteLine("Transpose of Matrix A is");
At = TransposeMatrix(A);
ShowMatrix(At);
}
}


ตัวอย่างผลการทำงาน
How many rows? 2
How many columns? 3
Enter element[1,1]: 1
Enter element[1,2]: 2
Enter element[1,3]: 3
Enter element[2,1]: 4
Enter element[2,2]: 5
Enter element[2,3]: 6
Matrix A is
  1 2 3
  4 5 6
Transpose of Matrix A is
  1 4
  2 5
  3 6



using System;
class Matrix
{
    static void ShowMatrix(int[,] m)
    {
        for (int i = 0; i < m.GetLength(0); i++)
        {
            for (int j = 0; j < m.GetLength(1); j++)
            {
                Console.Write("{0,4}", m[i, j]);
            }
            Console.WriteLine();
        }
    }
    static int[,] ReadMatrix(int nrows, int ncols)
    {
        int[,] m = new int[nrows, ncols];
        for (int i = 0; i < nrows; i++)
        {
            for (int j = 0; j < ncols; j++)
            {
                Console.Write("Enter element[{0},{1}]: ", i + 1, j + 1);

                m[i, j] = int.Parse(Console.ReadLine());
            }
        }
        return m;
    }
    static int[,] TransposeMatrix(int[,] m)
    {
        int[,] mt = new int[m.GetLength(1), m.GetLength(0)];
        for (int i = 0; i < m.GetLength(0); i++)
            for (int j = 0; j < m.GetLength(1); j++)
                mt[j, i] = m[i, j];
                return mt;
    }
    static void Main()
    {
        int num_rows, num_cols;
        int[,] A, At;
        Console.Write("How many rows? ");
        num_rows = int.Parse(Console.ReadLine());
        Console.Write("How many columns? ");
        num_cols = int.Parse(Console.ReadLine());
        A = ReadMatrix(num_rows, num_cols);
        Console.WriteLine("Matrix A is");
        ShowMatrix(A);
        Console.WriteLine("Transpose of Matrix A is");
        At = TransposeMatrix(A);
        ShowMatrix(At);
        Console.Read();
    }
}







ใบงานที่ 7.2 กำหนดค่าให้แมตริกซ์

ใบงานที่ 7.2
กำหนดค่าให้แมตริกซ์
ให้ผู้เรียนศึกษาใบความรู้ที่ 7.2 จากนั้นสร้างโปรเจ็กต์และเขียนโปรแกรมจากโจทย์ที่กำหนดให้ต่อไปนี้
โปรแกรมด้านล่างจะถามขนาดของแมตริกซ์จากผู้ใช้และเรียกเมท็อดชื่อ ReadMatrix เพื่อสร้างแม
ตริกซ์ตามขนาดที่กำหนดและอ่านข้อมูลของแมตริกซ์มาทีละค่าจากผู้ใช้ จากนั้นจึงเรียกเมท็อด
ShowMatrix จากใบงานที่ 7.1 เพื่อแสดงแมตริกซ์ออกทางหน้าจอ
using System;
class Matrix {
  // คัดลอกเมท็อด ShowMatrix จากแบบฝึกหัดที่แล้ว
  // มาปะในตำแหน่งนี้

  static int[,] ReadMatrix(int nrows, int ncols) {
    int[,] m = new int[_______,_______];
    for (int i = 0; i < nrows; i++) {
      for (int j = 0; j < ncols; j++) {
        Console.Write("Enter element[{0},{1}]: ", i+1, j+1);
        _______________ = int.Parse(Console.ReadLine());
      }
    }
    return m;
  }
  static void Main() {
    int num_rows, num_cols;
    int[,] A;

    Console.Write("How many rows? ");
    num_rows = int.Parse(Console.ReadLine());
    Console.Write("How many columns? ");
    num_cols = int.Parse(Console.ReadLine());
    A = ReadMatrix(num_rows, num_cols);
    Console.WriteLine("Matrix A is");
    ShowMatrix(A);
  }
}


ตัวอย่างผลการทำงาน
How many rows? 2
How many columns? 3
Enter element[1,1]: 9
Enter element[1,2]: 8
Enter element[1,3]: 7
Enter element[2,1]: 6
Enter element[2,2]: 5
Enter element[2,3]: 4
Matrix A is
   9 8 7
   6 5 4


using System;
class Matrix
{
    static void ShowMatrix(int[,] m)
    {
        for (int i = 0; i < m.GetLength(0); i++)
        {
            for (int j = 0; j < m.GetLength(1); j++)
            {
                Console.Write("{0,4}", m[i, j]);
            }
            Console.WriteLine();
        }
    }
    static int[,] ReadMatrix(int nrows, int ncols)
    {
        int[,] m = new int[nrows, ncols];
        for (int i = 0; i < nrows; i++)
        {
            for (int j = 0; j < ncols; j++)
            {
                Console.Write("Enter element[{0},{1}]: ", i + 1, j + 1);
                m[i, j] = int.Parse(Console.ReadLine());
            }
        }
        return m;
    }
    static void Main()
    {
        int num_rows, num_cols;
        int[,] A;
        Console.Write("How many rows? ");
        num_rows = int.Parse(Console.ReadLine());
        Console.Write("How many columns? ");
        num_cols = int.Parse(Console.ReadLine());
        A = ReadMatrix(num_rows, num_cols);
        Console.WriteLine("Matrix A is");
        ShowMatrix(A);
        Console.Read();
    }
}






ใบงานที่ 7.1 แสดงค่าในแมตริกซ์

ใบงานที่ 7.1
แสดงค่าในแมตริกซ์
ให้ผู้เรียนศึกษาใบความรู้ที่ 7.1 และใบความรู้ที่ 7.2 จากนั้นสร้างโปรเจ็กต์และเขียนโปรแกรมจากโจทย์ที่
กำหนดให้ต่อไปนี้
เราจะเขียนเมท็อดชื่อ ShowMatrix เพื่อแสดงข้อมูลภายในแมตริกซ์ออกทางหน้าจอ โดยตัวเมท็อดจะรับแมตริกซ์มาในรูปของพารามิเตอร์แบบอาเรย์สองมิติ จงเติมส่วนที่ขาดไปของเมท็อดเพื่อให้โปรแกรม
ทำงานได้อย่างสมบูรณ์ตามตัวอย่างผลลัพธ์
using System;
class Matrix {
  static void ShowMatrix(int[,] m) {
    for (int i = 0; i < ___________________; i++) {
      for (int j = 0; j < ___________________; j++) {
        Console.Write("{0,4}", _________________);
      }
      Console.WriteLine();
    }
  }

  static void Main() {
    int[,] A = {
      { 5, 3, 8},
      { 2, 6, 10},
      { 1, 8, 25},
      {12, 3, 30}
     };
     ShowMatrix(A);
  }
}
ตัวอย่างผลการทำงาน
  5  3  8
  2  6  10
  1  8  25
  12 3  30


using System;
class Matrix
{
    static void ShowMatrix(int[,] m)
    {
        for (int i = 0; i < m.GetLength(0); i++)
        {
            for (int j = 0; j < m.GetLength(1); j++)
            {
                Console.Write("{0,4}", m[i,j]);
            }
            Console.WriteLine();
        }
    }

    static void Main()
    {
        int[,] A = {
      { 5, 3,  8},
      { 2, 6, 10},
      { 1, 8, 25},
      {12, 3, 30}
    };
        ShowMatrix(A);
        Console.Read();
    }
}