MATRIX AND STRING(Двумерные массивы,строка)

Two-dimensional arrays(Двумерные массивы)

Two-dimensional arrays အား matrix(матрица) ဟုေခၚေလ့ရွိၿပီး ၎ သည္ one-dimensional array အား bracket နွစ္ထပ္ၿဖင့္ ကိန္းနွစ္ထပ္ၿဖင့္ ေဖာ္ၿပၿခင္းၿဖစ္သည္။
data_type name_array[size1][size2];
size1 သည္ row အေရအတြက္ၿဖစ္ၿပီး size2 သည္ coloum အေရအတြက္ၿဖစ္သည္။

Example.Declaring the matrix.

double  matr[100][10];
int  i_matrix[10][20];


Example.Enter an integer matrix from the keyboard.
#include <iostream>
usi ng namespace std;
#include <stdlib.h>
const int ROW = 5;
const int COL = 4;
int main ()
{ int i, j;
int x [ROW] [COL];
for (i = 0; i <ROW; i++)               / / ROW - the number of lines
      {  for (j = 0; j <COL; j++)       / / COL - the number of columns
             {     cout << "x [" << i << "] [" << j << "]";
cin >> x [i] [j];
              }
}
   return 0;
}



Example.Formation matrix with random number.
#include <iostream>
using namespace std;
#include <iomanip.h>       
#include <stdlib.h>
const int ROW = 5;
const int COL = 4;
int main ()
{int i, j;
int x [ROW] [COL];
randomize ();
for (i = 0; i <ROW; i + +)
{        for (j = 0; j <COL; j + +)
{        x [i] [j] = random (10) - 5;
cout << setw (4) << x [i] [j];
}
cout << endl;
}
return 0;
}

Matrix အား output ထုတ္ရန္အတြက္ loop အားအသံုးၿပဳရမည္ၿဖစ္သည္။

Example. Displaying two dimensional array
for (i = 0; i<ROW; i++)
{        for (j = 0; j<COL; j++) cout << setw (4) << x [i] [j];
cout << endl;
}

Multidimensional array အား initialization ၿပဳလုပ္ၿခင္းသည္ one-dimensional အား initialization ၿပဳလုပ္ၿခင္းနွင့္ အတူတူပင္ၿဖစ္သည္။
Example.Initialization of two-dimensional array.
Int x[5][3]=  {{1,2,3},       //line1
                   {4,5,6},         //line2
                   {7,8,9},         //line3
                   {10,11,12},    //line4
                   {13,14,15}}   //line5.


Processing of matrix

Matrix နွင့္ အလုပ္လုပ္ေတာ့္မည္ဆိုလွ်င္ ေအာက္ေဖာ္ၿပပါ သံုးမ်ိဳးမွ တစ္ခုအား မၾကာခန အသံုးၿပဳရန္ လိုအ ပ္ လိမ့္မည္ ၿဖစ္သည္။
  • Work with the matrix as a whole,
  • Working with rows (columns) matrix,
  • Working with the diagonal elements of the matrix.


Working with the matrix as a whole
Working with the matrix as a whole ဆိုသည္မွာ matrix တစ္ခုလံုးအား loop ၿဖင့္ပတ္၍ အလုပ္လုပ္ၿခင္း ၿဖစ္သည္။
Example.Find the maximum element of the matrix.
( matrix တစ္ခုလံုးမွ အၾကီးဆံုးကိန္းအား ရွာေဖြရန္)
……….
max = arr[0][0];
for(i=0;i<n;i++)
for(j=0;j<k;j++)
if(max<arr[i][j]) max = arr[i][j];
……….

Working with rows (columns) of the matrix
Working with rows (columns) of the matrix ဆိုသည္မွာ loop ၏ အစမွစတင္၍ loop ၏ အၿပင္ ဘက္နား တြင္သာ လုပ္ေဆာင္ခ်င္းၿဖစ္သည္။ ဆိုလိုသည္မွာ သက္ဆိုင္ရာ row(သို႕)colum အလိုက္ loop ပတ္ၿခင္း ၿဖစ္သည္။

Example.Search maximal elements or each row of the matrix.
(row တစ္ခုခ်င္းဆီ၏ အၾကီးဆံုးကိန္းအားရွာေဖြေပးရန္)
……….
int arr[n][k];
int max[n];
……….
for(i=0;i<n;i++)
          { max[i]=arr[i][0];
                   For(j=0;j<k;j++)if (max[i]<arr[i][j]) max[i] = arr[i][j];
          }
………
အထက္ေဖာ္ၿပပါ ပုစၦာ၏လုပ္ေဆာင္ခ်က္တြင္ matrix အားအလုပ္လုပ္ရာတြင္ column မ်ားၿဖင့္သာ အလုပ္လုပ္ ေစလိမ့္မည္ၿဖစ္သည္။
Matrix အား column ၿဖင့္အလုပ္လုပ္ခ်င္းနွင့္ row နွင့္အလုပ္လုပ္ခ်င္း၏ ၿခားနားခ်က္သည္ လည္းကြာၿခားမွဳ႕ သိပ္မရွိလွေပ။သက္ဆိုင္ရာ row နွင့္ column ေနရာတြင္ သက္ဆိုင္ေသာ ကိန္းဂဏန္းမ်ား အစားသြင္းေပးၿခင္းၿဖင့္ အဆင္ေၿပေစနိဳင္သည္။

 Example.Find the maximum element of each column of the matrix.
(column တစ္ခုခ်င္းဆီ၏ အၾကီးဆံုးကိန္းအားရွာေဖြေပးရန္)
……….
int arr[n][k];
int max[k];
……….
for(j=0;j<k;j++)
          { max[j]=arr[0][j];
                   For(i=0;i<n;i++)if (max[j]<arr[i][j]) max[j] = arr[i][j];
          }
………

The diagonal matrix elements(диагональные элементы матрицы)
Diagonal matrix အားအသံုးၿပဳလိုပါက အသံုးၿပဳမည့္ matrix သည္ square matrix ၿဖစ္ရမည္ၿဖစ္သည္။ (တစ္နည္အားၿဖင့္ row အေ၇အတြက္နွင့္ coloum အေရအတြက္တူညီရမည္ဟုဆိုလိုသည္)


Main diagonal ၏ အေပၚတြင္တည္ရွိေသာ element မ်ားအား main elements (principal) မ်ားဟုေခၚဆိုၿပီး main diagonal ၏ ေအာက္တြင္တည္ရွိေနေသာ element မ်ားအား other element (incidential) မ်ား ဟုေခၚဆိုသည္။
Main diagonal ၏ element မ်ားအတြက္ equality သည္ i==j ၿဖစ္သည္။အကယ္၍ i>j ဟူေသာ condition အားအသံုးၿပဳလွ်င္ main diagonal ေအာက္ရွိ element မ်ားသာ အလုပ္လုပ္လိမ့္မည္ၿဖစ္သည္။ထို႕အတူ i<j ဟူေသာ condition အားအသံုးၿပဳလွ်င္ main diagonal အေပၚရွိ element မ်ား သာ အလုပ္လုပ္လိမ့္မည္ၿဖစ္သည္။
အကယ္၍ i==n-j-1 ,(n- the order of the matrix) အားအသံုးၿပဳလွ်င္Main diagonal တြင္ ရွိေသာ element မ်ားအတြက္ၿဖစ္သည္။ i>(n-j-1) condition အားအသံုးၿပဳလွ်င္ other diagonal ေအာက္ရွိ element မ်ားအတြက္ ၿဖစ္ၿပီး i<(n-j-1) ဆိုလွ်င္ other diagonal အေပၚရွိ element မ်ားအတြက္ၿဖစ္သည္။

Example. Find the average of negative elements,located below the main diagonal of a square matrix 9*9.
# include <iostream>
using namespace std;
# include <iomanip.h>
# include <stdlib.h>
const int ROW = 9;
const int COL = 9;
int main ()
{int i, j, n = 0;
float mas [ROW] [COL];
float average = 0;
randomize ();
for (i = 0; i <ROW; i + +)
{        for (j = 0; j <COL; j + +)
                   {mas [i] [j] = (float) random (20) -10;
            cout << setw (6) << mas [i] [j];
}
cout << endl;
}
for (i = 0; i <ROW; i + +)
for (j = 0; j <i; j + +)
                   if (mas [i] [j] <0)
                             {  n++;
                       average += mas[i][j];
                              }
              average = average / n;
              cout << "\ n arithmetic mean =" << average << endl;
     return 0;
}


String (character array)

Input and output
String အား zero (0) byte ၿဖင့္ဆံုးေသာ character type array တစ္ခုဟုေၿပာနိဳင္ပါသည္။
String အား element မ်ား access ၿပဳလုပ္ခ်င္းသည္ array အား element မ်ား access ၿပဳလုပ္သကဲ့သို႕ နွင့္အတူတူပင္ၿဖစ္သည္။
Char sym=str[2];      //variable ‘sym’ တြင္ element 3 ခုပါ၀င္လိမ့္မည္ၿဖစ္သည္။
//အဘယ္ေၾကာင့္ဆိုေသာ ေနာက္ဆံုး element ၿဖစ္ေသာ 0 //အားထည့္၍တြက္ခ်က္ထားၿခင္းၿဖစ္သည္။

String အား initialization ၿပဳလုပ္လွ်င္ေအာက္ပါအတိုင္းၿပဳလုပ္ရမည္ၿဖစ္သည္။
char HDD[]=”Western digital”;
အထက္ေဖာ္ၿပပါ HDD string ၏ အလ်ားသည္ 16 bytes ၿဖစ္သည္။၎ string ထဲတြင္သာပါ၀င္ေသာ အကၡရာ 15 လံုးသာမဟုတ္ပဲ string တြင္မၿမင္သာေသာ 0 နွင့္ဆံုးေနေသာ ပမာဏပါ ထည့္သြင္းတြက္ခ်က္ရေလ့ရွိသည္။ ထို႕ေၾကာင့္ string အား initialize ၿပဳလုပ္လိုပါက programmer သည္ 0 bytes ၿဖင့္အဆံုးသတ္ေၾကာင္းအား မၿဖစ္မေနသိရွိရမည္ၿဖစ္သည္။
ေအာက္ေဖာ္ၿပပါအတိုင္းေၾကၿငာရမည္ၿဖစ္သည္။
char MyABCs[]=”ABC”;
တိက်စြာေၾကၿငာရလွ်င္
char MyABCs[]={‘A’,’B’,’C’,’0’};
Initialization string ၏ length အားတိုင္းတာ၇န္အတြက္ sizeof() – function အားအသံုးၿပဳနိဳင္သည္။
const int len_HDD=sizeof(HDD);
သည္တြင္ HDD သည္ “Western digital” ဟူ၍ initialize ၿပဳလုပ္ထားေသာ string တစ္ခုၿဖစ္ၿပီး len_HDD သည္ string ၏ဆိုဒ္အား byte ၿဖင့္တိုင္းတာထားေသာ constant တစ္ခုၿဖစ္သည္။(ထိုသို႕ တိုင္းတာရာတြင္ 0 ပါထည့္သြင္းတြက္ခ်က္မည္ၿဖစ္မည္)။

Example. Declaration and input string.
# include <iostream>
using namespace std;
# include <iomanip.h>
int main ()
{        const int MAX_LEN = 80 / / maximum length line
char str [MAX_LEN];
cout << "Enter a string:";
cin >> setw (79) >> str;
cout << "The entered string is:" << str << endl;
return 0;
}

setw(79) အားအသံုးၿပဳထားေသာေၾကာင့္ buffer အား overflow ၿပဳလုပ္သြားမည္ၿဖစ္ၿပီး backet ထဲရွိ ကိန္းလံုးပမာဏအတိုင္း string ထဲတြင္ ေနရာေက်ာ္လြန္သြားလိမ့္မည္ၿဖစ္သည္။
အကယ္၍ string ထဲတြင္ space မ်ားပါ၀င္ပါက ၎ အား read ၿပဳလုပ္ရန္အတြက္ cin.get() ၿဖင့္ အသံုးၿပဳနိဳင္သည္။ cin.get –operator အားအသံုးၿပဳရန္အတြက္ ေအာက္ပါအတိုင္းအသံုးၿပဳရမည္ၿဖစ္သည္။
cin.get(str,MAX_LEN);
str string အား keyboard မွရိုက္ထည့္မည္ဆိုပါက ၎၏ပမာဏသည္ (MAX_LEN – 1) ထက္ပိုမို အသံုး မၿပဳ နိဳင္ေပ။

Example. Enter a string that contains spaces.
#include <iostream>
using namespace std;
int main ()
{        const int MAX_LEN = 80;               / / Maximum length of string
char str [MAX_LEN];
cout << "Enter a string:";
cin.get (str, MAX_LEN);
cout << "The entered string is:" << str << endl;
return 0;
}

String အား input ထည့္သြင္းရန္အတြက္ getline နည္းအားၿဖင့္လည္းအသံုးၿပဳနိဳင္သည္။

Example. Enter a string that contains spaces.
#include <iostream>
using namespace std;
int main ()
{        const int MAX_LEN = 8 0;               / / Maximum length of string
char str [MAX_LEN];
cout << "Enter a string:";
cin.getline (str, MAX_LEN);
cout << "The entered string is:" << str << endl;
return 0;
}

အလုပ္လုပ္သည့္ နည္းလမ္းသည္ အတူတူနီးပါးပင္ၿဖစ္သည္။ၿခားနားခ်က္အေနၿဖင့္ getline သည္ ၀င္ေရာက္ လာေသာ string အား (MAX_LEN – 1) ထက္ပိုမို၍ read လုပ္လိမ့္မည္မဟုတ္ေပ။ထို႕အတူ ၎အား write ၿပဳလုပ္ရာတြင္ သတ္မွတ္ထားေသာ variable မ်ားတြင္သာ ေရးသားမည္ၿဖစ္ၿပီး zero byte ေနရာတြင္ newline သို႕ transfer ၿပဳလုပ္သည့္ သေကၤတၿဖစ္ေသာ ‘\n’ အားေၿပာင္းလဲသြားလိမ့္မည္ၿဖစ္သည္။ ထိုသို႕ ၿပဳလုပ္ၿပီးလွ်င္ newline character သည္လည္း stream မွ remove ၿဖစ္သြားလိမ့္မည္ၿဖစ္သည္။

Example. Try typing a few lines.
#include <iostream>
using namespace std;
int main ()
{        const int MAX_LEN = 80;              / /Maximum length of string
char str [MAX_LEN];
cout << "Enter a string:";
cin.get (str, MAX_LEN);
cout << "The entered string is:" << str << endl;  / / Output of the input string
cout << "Enter a string:";
cin.get (str, MAX_LEN);
cout << "The entered string is:" << str << endl;               / / Empty string
cout << "Enter a string:";
cin.get (str, MAX_LEN);
cout << "The entered string is:" << str << endl;               / / Empty string
return 0;
}

Character ‘\n’ အား stream မွ delete ၿပဳလုပ္ရန္အတြက္ get() method အား parameter မပါဘဲနွင့္ အသံုးၿပဳနိဳင္သည္။

Example. Input some strings.
#include <iostream>
using namespace std;
void main ()
{        const int MAX_LEN = 80;    / / Maximum length of the string
char str [MAX_LEN];
cout << "Enter a string:";
cin.get (str, MAX_LEN);
cout << "The entered string is:" << str << endl;  / / Output of the input string
cin.get ();
cout << "Enter a string:";
cin.get (str, MAX_LEN);
cout << "The entered string is:" << str << endl;  / / Empty string
cin.get ();
cout << "Enter a string:";
cin.get (str, MAX_LEN);
cout << "The entered string is:" << str << endl;  / / Empty string
cin.get ();
}

Function working with strings

Example.Copying string.
#include <iostream>
using namespace std;
#include <string.h>
int main ()
{        const int MAX _ LEN = 80;               / / Maximum length of the string
int i;
char s a [] = " You are responsible for all of those who tamed";
char s2 [MAX_LEN];
for (i = 0; i <strlen (s1); i + +) / / Strlen (s1) -determine the length of string
s2 [i] = s1 [i];
s2 [i] = '\ 0';
cout << s2 << endl;
return 0;
}

Example .Copying a string using the function strcpy ().
#include <iostream>
using namespace std;
#include <string.h>
int main ()
{        const int MAX _ LEN = 80;  / / Maximum length of the string
char s1[] = "Not in a world of perfection";
char s2 [MAX_LEN];
strcpy (s 2, s 1);      / / Function to copy string s1 to string s2
cout << s2 << endl;
return 0;
}

Example.Combining strings using the strcat ().
#include <iostream>
using namespace std;
#include <string.h>
int main ()
{        const int MAX _ LEN = 80;  / / Maximum length of the string
char s1[MAX _ LEN] = "shines above the glacier cold star";
char s2[]="but there is no warmer than the picture on the ground";
strcat (s1, s2);          / / Function combining strings s1 and string s2
cout << s1 << endl;
return 0;
}

Example .Search substring in a string using the function strstr().
#include <iostream>
using namespace std;
#include <string.h>
int main ()
{          const int MAX _ LEN = 80;  / / Maximum length of the string
char s1[] = 'Find the string in a string using the strstr (). ";
char s2[] = "string"
char *s3 = strstr (s1, s2);    / / The search function in the string s1 s2 substring
cout << s3 << endl;
return 0;
}

( string တြင္အသံုးၿပဳနိဳင္ေသာ function အနည္းငယ္အား ဆက္လက္ေလ့လာလိုလွ်င္

translated by zmk@miet51

No comments:

Post a Comment