ARRAY(массив)

Array(массив) 

Arrays မ်ားအား မ်ားေသာအားၿဖင့္ data structure မ်ားတြင္အသံုးၿပဳေလ့ရွိသည္။ထို data structure မ်ားသည္ တူညီေသာ data type ၏ element မ်ားအား အတူတကြေပါင္းစည္းရန္ၿဖစ္ၿပီး အနွဳတ္ကိန္းမဟုတ္ေသာ အကၡရာအစဥ္အလိုက္ၿဖင့္ ၿပန္လည္ေဖာ္ညြန္းသည္။
ပရိုဂမ္မ်ားတြင္ မၾကာခနအသံုးၿပဳေလ့ရွိသည္မ်ားမွာ one-dimensional array နွင့္ two-dimensional array တို႕ၿဖစ္သည္။ သို႕ေသာ္ C++ language တြင္ multidimensional array(многомерный массив) မ်ားနွင့္ပါ တြဲဖက္အလုပ္လုပ္နိဳင္သည္။

One-dimensional array(Одномерный массив)
One-dimensional array တြင္ data type တူညီေသာ variable မ်ားစြာပါ၀င္နိဳင္သည္။သို႕ေသာ္ နာမည္တစ္ခုတည္းၿဖင့္(array name) သာအသံုးၿပဳရမည္ၿဖစ္သည္။ထိုနာမည္တစ္ခုတည္းအားၿဖင့္ element တိုင္းအား access ၿပဳလုပ္ရာတြင္ number အစဥ္အလိုက္ access ၿပဳလုပ္သြားမည္ၿဖစ္သည္။
Array အားေၾကၿငာရာတြင္ ေအာက္ေဖာ္ၿပပါပံုစံအတိုင္းေၾကၿငာရမည္ၿဖစ္သည္။
Data_type array_name[element_number];


Element_number သည္ တိက်ေသာ ကိန္းဂဏာန္းတစ္ခု (သို႕) constant တစ္ခုၿဖစ္ရမည္။သို႕မွသာ complier သည္ complie ၿပဳလုပ္ခ်ိန္တြင္ ၎ပမာဏ အား array အတြက္ memory ေပၚတြင္ ေနရာေပးလိမ့္မည္ၿဖစ္သည္။ သို႕ေသာ္ complie ၿပဳလုပ္ၿပီးခ်ိန္တြင္ ၎၏ပမာဏအား ၿပန္လည္ၿပဳၿပင္ၿခင္း မၿပဳလုပ္နိဳင္ေတာ့္ေပ။accept ၿပဳလုပ္ရာတြင္ array ၏ space address မ်ားတြင္သာ အသံုးၿပဳၿပဳလုပ္သြားမည္ၿဖစ္သည္။

Example.Declaring arrays of different types
int i_arr[10];   //integer array of 10 element;
char liter[80]; //character array of 80 element;
double d_mas[100];  // array of 100 double real_numbers

array တစ္ခုအားေၾကၿငာလိုက္လွ်င္ပထမဦးစြာမသိမၿဖစ္သိရွိရမည္မွာ ပထမဆံုး element ၏ index သည္ အၿမဲတမ္း 0 သည္။ acceptable ၿပဳလုပ္ေသာ တန္ဖိုးမ်ား၏ index သည္ 0 မွ element_number – 1 ၏ range အတြင္းတြင္ ၿဖစ္သည္။
one-dimensional array element အား accessing ၿပဳလုပ္ရာတြင္ for-loop အသံုးၿပဳၿခင္းသည္ လြယ္ကူ အဆင္ေၿပေစသည္။

Example.Organization of input element of array
# include <iostream>
# include <iomanip.h>
using namespace std;
const int M = 10;               / / Number of elements in the array, declared as a constant
void main ()
{  float x [M]; / / declaration of a real array of ten elements called Comrade
    for (int i = 0; i <M; i + +)
{ cout << "x [" << i << "]:";
   cin >> x [i];                   / / input array from a keyboard
           }
for (int i = 0; i <M; i + +) cout << setw (5) << x [i]; / / output Array to the screen
cout << endl;
}
အထက္ေဖာ္ၿပပါ ပုစၦာအတိုင္း keyboard မွရိုက္မထည့္ဘဲနွင့္ လိုအပ္သည့္ range အတိုင္း random input ၿပဳလုပ္နိဳင္သည္။ထိုသို႕ၿပဳလုပ္ရန္ random_number အားအသံုးၿပဳရမည္ၿဖစ္သည္။

Example.Input element of array at random and output on the screen.
# include <iostream>
using namespace std;
# include <stdlib.h>
const int N = 100;
int main ()
{        int arr [N];
randomize ();                    / / initialize the random number generator
for (int i = 0; i <N; i + +)
{        arr [i] = random (100);
cout << arr [i] << '';
           }
   return 0;
}
သည္တြင္ arr အမည္ၿဖင့္ array တစ္ခုၿဖစ္ၿပီး N element အေရအတြက္ပါ၀င္မည္ၿဖစ္ၿပီး 0 မွ N-1 range အတြင္း ၿဖစ္သည္။random number မ်ားသည္ 0-99 အတြင္းရွိ integer number ၿဖစ္မည္ၿဖစ္သည္။အကယ္၍ array တြင္ အေပါင္းကိန္းမ်ားအၿပင္ အနွတ္ကိန္းမ်ားပါ အသံုးၿပဳရန္လိုအပ္ပါက random() ဖန္ရွင္ argument အား လိုသလိုေၿပာင္းလဲအသံုးၿပဳနိဳင္သည္။
arr[i]= random(100)-50;
// အနွတ္ကိန္း 50 မွ စ ၍ အေပါင္းကိန္း 50 အတြင္းရွိ ကိန္းမ်ားအားအသံုးၿပဳလိမ့္မည္ၿဖစ္သည္။

Example.Given 30-element integer array.Create another array of disintegrations.Expanding it first_number element of the original array and then odd number.
(element 30 ရွိေသာ integer array တစ္ခုၿပဳလုပ္ၿပီး ေနာက္ထပ္ array တစ္ခုထဲတြင္ ထိုမူရင္း array မွ ပထမဆံုး စံုကိန္းအားထည့္ၿပီး ထို႕ေနာက္တြင္ မကိန္းအားထည့္ပါ)။
#include <iostream>
using namespace std;
#include <stdlib.h>
 const int M = 30;
 int main ()
 {       int i, j = 0;
           int mas1 [M];                                           / / original array
           int mas2 [M];                                           / / New array
           randomize ();
           for (i = 0; i <M; i + +)
           { mas1 [i] = random (10);
              cout << mas1 [i] << '';
           }
cout << endl;
           for (i = 0; i <M; i + +)
           if (mas1 [i]% 2 == 0) {mas2 [j] = mas1 [i];
                                                j + +;
              }
for (i = 0; i <M; i + +)
              if (mas1 [i]% 2! = 0) {mas2 [j] = mas1 [i];
                                          j + +;
                                         }
              for (i = 0; i <M; i + +)
              cout << mas2 [i] << '';
cout << endl;
return 0;
      }

Initialization of the array(Инициализация массива)
Array အား Initialization ၿပဳလုပ္ခ်င္းဆိုသည္မွာ bracket အတြင္းရွိတန္ဖိုးမ်ားအား တိုက္ရိုက္ ထည့္သြင္းၿခင္းပင္ ၿဖစ္သည္။
int digit[10]={0,1,2,3,4,5,6,7,8,9];

အကယ္၍ initialization ၿပဳလုပ္ရာတြင္ array ၏ စုစုေပါင္းပမာဏထက္နည္းေနပါက က်န္ရွိေနေသာ တန္ဖိုးမ်ားအား 0 နွင့္တူညီသြားလိမ့္မည္ၿဖစ္သည္။ဆိုလိုသည္မွာ ေအာက္ေဖာ္ၿပပါ ေၾကၿငာခ်က္အားၾကည့္ပါ။
int digit[10]={0,1,2,3,4};

ထိုအတိုင္းေၾကၿငာပါက integer array type ၿဖစ္ေသာ digit တြင္ကိန္းဂဏာန္း 10 လံုးရွိသည္ၿဖစ္ၿပီး ပထမကိန္း 5 လံုးသာ initialize ၿပဳလုပ္ေသာေၾကာင့္ က်န္ရွိေနေသာကိန္းမ်ားသည္ zero(0) အၿဖစ္သို႕ initialize ၿပဳလုပ္သြားလိမ့္မည္ၿဖစ္သည္။ထိုသို႕အားၿဖင့္ array တစ္ခုလံုး၏ element မ်ားအား 0 အေနၿဖင့္ initialize ၿပဳလုပ္ရန္ ေအာက္ပါအတိုင္းအသံုးၿပဳနိဳင္သည္။
int digit[10]={0};
သို႕ေသာ္လည္း အကယ္၍ array element 10 လံုးထက္ပိုမိုမ်ားၿပားစြာ initialize ၿပဳလုပ္ပါက complier သည္ error message ၿပလိမ့္မည္ၿဖစ္သည္။

Initialization ၿပဳလုပ္သည့္အခ်ိန္တြင္ array element ၏ ပါ၀င္မွဳ႕ပမာဏအား ေအာက္ပါအတိုင္း တြက္ခ်က္နိဳင္သည္။
ဥပမာ array name အား MyMas။ ထို႕ေၾကာင့္ sizeof(MyMas) ၿဖင့္ ထို array ၏ပမာဏအား byte ၿဖင့္ ေဖာ္ၿပတြက္ခ်က္ၿခင္း ၿပဳလုပ္နိဳင္သည္။ ဆက္လက္၍ sizeof(MyMas[0]) ၿဖင့္ element တစ္ခု၏ byte ပမာဏအားတြက္ခ်က္နိဳင္သည္။ထို႕ေၾကာင့္ array တြင္ပါ၀င္ေသာ element ပမာဏအာ းေအာက္ပါအတိုင္း ေဖာ္ၿပနိဳင္သည္။
#define N((sizeof(MyMas)/sizeof(MyMas[0]))
အကယ္၍ #define အားအသံုးမၿပဳလိုပါက constant ကိန္းတစ္ခုခုအားသတ္မွတ္၍အသံုးၿပဳနိဳင္သည္။
const intN=((sizeof(MyMas)/sizeof(MyMas[0]));
အကယ္၍ array အား constant အေနၿဖင့္ အသံုးၿပဳရန္လိုအပ္ပါကလည္းေအာက္ပါတိုင္း အသံုးၿပဳနိဳင္သည္။
const int ConstArr[]={5,4,3,2,1};
ထို႕သို႕ေၾကၿငာၿပီးလွ်င္ array ၏ မည္သည့္ element ကိုမွေၿပာင္းလဲ၍ရေတာ့္မည္မဟုတ္ေပ။

Methods for sorting arrays(Методы сортировки массивов)

Sorting a simple choice(Сортировка простым выбором)
ထိုနည္းသည္ အလြယ္ဆံုးေသာ sorting algorithms ၿဖစ္သည္။array အား sorting စီရာတြင္ အငယ္ဆံုးၿဖစ္ေသာ ကိန္းအား array ၏ပထမဆံုးေနရာတြင္ ထိုကိန္းနွင့္ေၿပာင္းလဲအစားထိုးၿခင္းပင္ၿဖစ္သည္။ ထို႕ေနာက္တြင္မွ က်န္ရွိေနေသာ arry အပိုင္းမွာ ဆက္လက္၍ sorting ၿပဳလုပ္သြားၿခင္းၿဖစ္သည္။ထိုနည္းတြင္ ပထမဦးစြာ အငယ္ဆံုးကိန္းအားရွာေဖြၿပီး array ၏ ပထမေနရာတြင္ ေၿပာင္းလဲထားရွိမည္ၿဖစ္သည္။ထိုသို႕ၿဖင့္ array element မ်ား ေနာက္ဆံုးအငယ္ဆံုးထိေရာက္သြားသည္အထိ ထပ္ခါထပ္ခါ အၾကိမ္ၾကိမ္ၿပဳလုပ္သြားမည္ၿဖစ္သည္။

Example.Suppose we are given an array of 142,23,97,19,2,4. Sort-out it.
1 passage:     2,23,97,19,142,4
2 passage:     2,4,97,19,142,23
3 passage:     2,4,19,97,142,23
4 passage:     2,4,19,23,142,97
5 passage:     2,4,19,23,97,142

Program sorting:
# include <iostream>
using namespace std;
# include <iomanip.h>
# include <conio.h>
# include <stdlib.h>
void main ()
{ const int N = 6;
int mas [N];
 int i, j, j_min, min;
randomize ();
clrscr ();
for (i = 0; i <N; i + +)
{        mas [i] = random (100) -50;
cout << setw (4) << mas [i];
}
cout << endl;
for (i = 0; i <N-1; i + +)
{        min = mas [i];
j_min = i;
for (j = i +1; j <N; j + +) / / iterates through the array in a reduced
               if (mas [j] <min) {min = mas [j];
                                          j_min = j;
                                          }
mas [j_min] = mas [i];
mas [i] = min;
}
for (i = 0; i <N; i + +)
cout << setw (4) << mas [i];
cout << endl;
}

Method of bubble sort(Метод пузырьковой сортировки)
Bubble sort နည္းသည္ class တစ္ခုလံုးအား sorting စီၿခင္းပင္ၿဖစ္သည္။simple နည္းထက္စာလွ်င္ ၎နည္းလမ္းသည္ ေႏွးေကြးစြာအလုပ္လုပ္လိမ့္မည္ၿဖစ္သည္။သို႕ေသာ္လည္း bubble sort algoritm နည္းသည္ တစ္မ်ိဳးထူးၿခားသည္ အသြင္အၿပင္ရွိသည္ၿဖစ္သည္။၎မွာ array ၏ element အားလဲလွယ္ေၿပာင္းလဲ sorting စီရာတြင္ အနီးကပ္ၿဖစ္ေနေသာ array element နွစ္ခုအတြင္းသာ ေၿပာင္းလဲၿခင္းၿဖစ္သည္။

Example.Sortin array 142,23,97,19,2,4 by bubble method.
1 passge:     23, 142, 97, 19, 2, 4
                   23, 97, 142, 19, 2, 4
                   23, 97, 19, 142, 2, 4
                   23, 97, 19, 2, 142, 4
                   23, 97, 19, 2, 4, 142
2 passage:    23, 19, 97, 2, 4, 142
                   23, 19, 2, 97, 4, 142
                   23, 19, 2, 4, 97, 142
3 passage:   19, 23, 2, 4, 97, 142
                  19, 2, 23, 4, 97, 142
                  19, 2, 4, 23, 97, 142
4 passage:   2, 19, 4, 23, 97, 142
                  2, 4, 19, 23, 97, 142
Program sorting:
# include <iostream>
using namespace std;
# include <iomanip.h>
# include <conio.h>
# include <stdlib.h>
void main ()
{        const int N = 6;
int mas [N];
int i, j, wrk;
randomize ();
clrscr ();
for (i = 0; i <N; i + +)
{        mas [i] = random (100) -50;
cout << setw (4) << mas [i];
}
cout << endl;
for (i = 0; i <N-1; i + +) / / i - counter passes
for (j = 0; j <N-1; j + +)
if (mas [j]> mas [j +1])
{        wrk = mas [j];
                     mas [j] = mas [j +1];
                     mas [j +1] = wrk;
           }
for (i = 0; i <N; i + +)
cout << setw (4) << mas [i];
cout << endl;
}


translated by zmk@miet51


No comments:

Post a Comment