our notes for our youngers

August 23, 2009

basic of C# (part 15)

Filed under: Basic of C# — Tags: , , , — sevenlamp @ 1:49 PM

အခု ကြၽန္ေတာ္တို႔ C# မွာ သံုးလို႔ရတဲ႔ Array ေတြအေၾကာင္း ေလ့လာရေအာင္။ C# မွာ array အမ်ိဳးအစား ေတာ္ေတာ္စံုစံုလင္လင္ကို သံုးလို႔ရပါတယ္။ အရင္ဆံုး ကြၽန္ေတာ္တို႔ သိျပီးသား single dimension array ေလးကေန စလိုက္ရေအာင္။ ကြၽန္ေတာ္က integer value ၅ ခုသိမ္းဖို႔အတြက္ array ေလးတစ္ခု ေဆာက္ခ်င္ရင္ ခုလိုေၾကျငာရမွာပါ။

int[ ] arr = new int[5];

ျပီးရင္ အဲ့ဒီ array ထဲကို တန္ဖိုး ၅ခု ထည့္ခ်င္တယ္ဆိုရင္ ခုလိုေရးရမယ္။

  arr[0] = 2;
  arr[1] = 4;
  arr[2] = 6;
  arr[3] = 8;
  arr[4] = 0;

ေနာက္တစ္နည္းအားျဖင့္ array ေၾကျငာေနတုန္းမွာ တန္းဖိုးကိုပါ ထည့္ေပးလိုက္ခ်င္တယ္ဆိုရင္ လည္းရပါတယ္။ အဲ့ဒါကို array initialization လို႔ေခၚပါတယ္။ ဒီလိုမ်ိဳးပါ….

int[ ] arr = new int[ ] { 2, 4, 6, 8, 0 };

အခုလိုမ်ိဳး statement တစ္ေၾကာင္းထဲမွာ array declaration ေရာ initialization ပါ ေပါင္းလုပ္မယ္ဆိုရင္ ေနာက္တစ္မ်ိဳးက ဒီလိုလည္း ေရးလို႔ ရပါတယ္…

int[ ] arr = { 2, 4, 6, 8, 0 };

ဒါေပမယ့္ statement ၂ ေၾကာင္းခြဲေရးမယ္ဆိုရင္ေတာ့ new int[ ] ဆိုတဲ႔ အပိုင္းေလးကို မထည့္လို႔ မရပါဘူး။

  int[ ] arr;          // declaration statement
  arr = new int[ ] { 2, 4, 6, 8, 0 };// initialization statement

example ေလး တစ္ခုေလာက္ ေရးၾကည့္ရေအာင္..

      using System;
      class forlooptest
      {
          static void Main()
          {
              int[] arr1 = { 1, 3, 5, 7, 9 };
              int[] arr2 = { 2, 4, 6, 8, 0 };
              int[] arr3 = new int[5];
              for (int i = 0; i < arr1.Length; i++)
              {
                  arr3[i] = arr1[i] + arr2[i];
              }
              Console.Write("Result array : ");
              foreach (int v in arr3)
                  Console.Write(v + " ");
              Console.Read();
          }
      }

multi-dimension array ဆိုတာကေတာ့ dimension တစ္ခုထက္ပိုပါတဲ႔ array လို႔ ဆိုလိုတာေပါ့🙂 ။ dimension တစ္ခုစီကို ( , ) ေလးနဲ႔ ခံျပီးေရးေပးရပါတယ္။

int[,] arr = new int[3,2];

အခု ေၾကျငာလိုက္တဲ႔ array ေလးမွာ dimension ၂ ခုပါျပီး၊ ပထမ dimension မွာ ၃ ခန္း၊ ဒုတိယ dimension မွာ ၂ ခန္း၊ ယူသြားပါမယ္။
တန္ဖိုးထည့္မယ္ဆိုရင္…

arr[0,0] = 1;
arr[0,1] = 2;
arr[1,0] = 3;
arr[1,1] = 4;
arr[2,0] = 5;
arr[2,1] = 6;

multidimensional array ေတြကိုလည္း initialize လုပ္လို႔ရပါတယ္။ ဒီလိုမ်ိဳးပါ…

int[,] arr = { {1,2} , {3,4} , {5,6} } ;

multidimensional array ေတြကို looping ပတ္မယ္ဆိုရင္ေတာ့ dimension အေရအတြက္ေပၚမူတည္ျပီး loopingေတြ ေရးရမွာပါ။

for (int i = 0; i <= arr.GetUpperBound(0); i++)
{
for (int j = 0; j <= arr.GetUpperBound(1); j++)
      		Console.Write(arr[i, j]+" ");
Console.WriteLine();
}

GetUpperBound function ေလးက demension တစ္ခုရဲ႕ upperbound တန္ဖိုးကို return ျပန္ပါတယ္။ ဒါေၾကာင့္ ကိုယ္သိခ်င္တဲ႔ dimension index ေလးကို parameter ထည့္ေပးရပါတယ္။
အခုကြၽန္ေတာ္တို႔ matrix 2 ခု ေျမွာက္တဲ႔ program ေလး ေရးၾကည့္ရေအာင္။ နည္းနည္းေတာ့ ရွည္ျပီး ႐ႈပ္မယ္ထင္တယ္😀

using System;
class Program
{
    static int GetInteger()
    {
        try
        {
            return Convert.ToInt32(Console.ReadLine());
        }
        catch
        {
            return 1;
        }
    }
    static void Main(string[] args)
    {
        int r1, r2, c1, c2;
        int[,] matrix1;
        int[,] matrix2;
        int[,] matrix3;
        Console.WriteLine("First Matrix\n************");
        Console.Write("row : ");
        r1 = GetInteger();
        Console.Write("col : ");
        c1 = GetInteger();
        matrix1 = new int[r1, c1];

        Console.WriteLine();
        Console.WriteLine("Second Matrix\n************");
        Console.Write("row : ");
        r2 = GetInteger();
        Console.Write("col : ");
        c2 = GetInteger();
        matrix2 = new int[r2, c2];

        if (!((r1 == 1 && c1 == 1) || (r2 == 1 && c2 == 1) || (c1 == r2)))
        {
            Console.WriteLine("\nOut of result!");
            Console.Read();
            return;
        }

        Console.WriteLine();
        for (int i = 0; i <= matrix1.GetUpperBound(0); i++)
        {
            for (int j = 0; j <= matrix1.GetUpperBound(1); j++)
            {
                Console.Write("Enter value for first matrix : row {0}, column {1} : ", i, j);
                matrix1[i, j] = GetInteger();
            }
        }
        Console.WriteLine();
        for (int i = 0; i <= matrix2.GetUpperBound(0); i++)
        {
            for (int j = 0; j <= matrix2.GetUpperBound(1); j++)
            {
                Console.Write("Enter value for second matrix : row {0}, column {1} : ", i, j);
                matrix2[i, j] = GetInteger();
            }
        }

        if (c1 != r2)
        {
            if (r1 == 1 && c1 == 1)
            {
                matrix3 = new int[r2, c2];
                for (int i = 0; i <= matrix2.GetUpperBound(0); i++)
                    for (int j = 0; j <= matrix2.GetUpperBound(1); j++)
                        matrix3[i, j] = matrix2[i, j] * matrix1[0, 0];
            }
            else
            {
                matrix3 = new int[r1, c1];
                for (int i = 0; i <= matrix1.GetUpperBound(0); i++)
                    for (int j = 0; j <= matrix1.GetUpperBound(1); j++)
                        matrix3[i, j] = matrix1[i, j] * matrix2[0, 0];
            }
        }
        else
        {
            matrix3 = new int[r1, c2];
            for (int i = 0; i <= matrix1.GetUpperBound(1); i++)
            {
                for (int j = 0; j <= matrix1.GetUpperBound(0); j++)
                {
                    for (int k = 0; k <= matrix2.GetUpperBound(1); k++)
                        matrix3[j, k] += matrix1[j, i] * matrix2[i, k];
                }
            }
        }

        Console.WriteLine("\nResult Matrix\n*************");
        for (int i = 0; i <= matrix3.GetUpperBound(0); i++)
        {
            for (int j = 0; j <= matrix3.GetUpperBound(1); j++)
            {
                Console.Write(matrix3[i, j] + " ");
            }
            Console.WriteLine();
        }
        Console.Read();
    }
}

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: