360 likes | 580 Views
حسابگری الگوریتمی. مبحث ششم: آرایه ها و رشته ها. فهرست مطالب. تعاریف اولیه مثال هایی از آرایه های یک بعدی آرايه هاي يک بعدی به عنوان آرگومان تابع روش های مرتب سازی و جستجوی آرایه ها آرایه های دوبعدی رشته ها عملیات رشته ها. آرایه. آرايه: مجموعه اي از عناصر همنوع نام ديگر: ليست
E N D
حسابگری الگوریتمی مبحث ششم: آرایه ها و رشته ها
آرایه ها و رشته ها فهرست مطالب • تعاریف اولیه • مثال هایی از آرایه های یک بعدی • آرايه هاي يک بعدی به عنوان آرگومان تابع • روش های مرتب سازی و جستجوی آرایه ها • آرایه های دوبعدی • رشته ها • عملیات رشته ها
تعاریف اولیه آرایه • آرايه: مجموعه اي از عناصر همنوع • نام ديگر: ليست • نامگذاري آرايه: مانند متغير معمولی • نحوة دسترسي به عناصر آرايه: استفاده از متغيري به نام انديس
تعاریف اولیه آرايه هاي يک بعدي • دستيابي به عناصر آرايه: با يک انديس • الگوي تعريف: • نوع آرايهنام آرايه [طول آرايه] • نوع آرايه: يکي از انواع C++ • نام آرايه: نامگذاري متغيرها • انديس آرايه از صفر شروع مي شود.
تعاریف اولیه آرايه هاي يک بعدي • ذخيره عناصر آرايه در حافظه بصورت متوالي • ميزان حافظه اختصاصي به آرايه (برحسب بايت): • ميزان حافظه آرايه = (طول نوع آرايه)*طول آرايه int x[6]; x[0] x[1] x[2] x[3] x[4] x[5]
تعاریف اولیه مقدار اوليه آرايه • مقدار اوليه عناصر آرايه عمومي (خارج از main()) صفر است. • مقدار اوليه عناصر آرايه محلي تعريف نشده است. • الگوي مقدار اوليه دادن به آرايه ها: • نوع آرايهنام آرايه [طول] = {مقادير}; int x[5]={1,2,3,4,5}; int x[10]={1,2,3}; int p[ ]={1,3,5,7,9};
مثال هایی از آرایه های یک بعدی مثال 6-1: تعيين بيشترين معدل و محل آن Example 6-1: #include <iostream.h> int main() { const int n = 10; float ave[n], amax = 0; int i, p; for(i = 0; i < n; i++){ cout << " enter an average: "; cin >> ave[i]; } amax = ave[0]; p = 0; for(i = 1; i < n; i++) if(ave[i] > amax) { amax = ave[i]; p = i; } cout << "\n max = " << amax << " position = " << p+1; return 0; } enter an average: 10.5 enter an average: 17.25 enter an average: 14 enter an average: 19.25 enter an average: 15.5 enter an average: 12.75 enter an average: 14.75 enter an average: 17.25 enter an average: 18.5 enter an average: 12.5 max = 19.25 position = 4
مثال هایی از آرایه های یک بعدی مثال 6-2: تعيين اعداد ورودي مثبت و منفي و تعداد آنها Example 6_2: #include <iostream.h> int main() { const int n = 10; int arr[n], i, c1 = 0, c2 = 0 ; cout << "\n Enter " << n << " numbers:\n"; for(i = 0; i < n; i++) cin >> arr[i]; cout << "\n negatives are: "; for(i = 0 ; i < n ; i++) if(arr[i] < 0){ cout << arr[i] << " "; c1++; } //end of if cout << "\n positives are: "; for(i = 0 ; i < n ; i++) if(arr[i] > 0){ cout << arr[i] << " "; c2++; }//end of if cout << "\n number of negative = " << c1; cout << "\n number of positive = " << c2; return 0; { Enter 10 numbers: 12 -12 4 5 0 -2 -0 4 -9 10 negatives are: -12 -2 -9 positives are: 12 4 5 4 10 number of negative = 3 number of positive = 5
آرايه هاي يک بعدی به عنوان آرگومان تابع تعریف • تعريف پارامترها در توابع داراي آرگومان آرايه: • آرايه با طول مشخص • آرايه با طول نامشخص (که بهتر است طول توسط آرگومان ديگر منتقل شود) • اشاره گر (فصل بعد) void func1 (int x[]); void func2 (int x[], int len); void main() { int x[10]; … func1 (x); … func2 (x,10); } void func1 (int x[10]) { … } void func2 (int x[], int len) { … }
آرايه هاي يک بعدی به عنوان آرگومان تابع مثال 6-3: تعيين نمره ای که بيش از همه تکرار شده Eٍxample 6-3: #include <iostream.h> void findt(float arr[], int k); int main() { const int k = 10; float arr[k] ; int i; cout << "\nEnter " << k << " mean :\n"; for (i = 0; i < k; i++) cin >> arr[i]; findt(arr, k); return 0; } //************************* void findt(float arr[], int k) { int max_count = -1 ; float max_value; int i, j , current_count; float current_value ; for (i = 0; i < k; i ++){ current_value = arr[i] ; current_count = 0 ; for(j = 0; j < k; j++) if (arr[j] == current_value) current_count++ ; if (current_count > max_count) { max_count = current_count ; max_value = current_value ; } } cout << "\n maximum iteration of ave "; cout << max_value << " is "<< max_count; } Enter 10 mean : 12.5 17.25 14 13.5 17.25 18.75 12.5 14.25 15.75 17.25 maximum iteration of ave 17.25 is 3
روش های مرتب سازی و جستجوی آرایه ها مرتب سازي آرايه ها • مرتب سازي صعودي x[0] < x[1] < x[2] < … < x[n] • مرتب سازي نزولي x[0] > x[1] > x[2] > … > x[n] • مرتب سازي حبابي • سهولت درک برنامه نويسي • کارآيي کمتر نسبت به ساير روشها • مقايسه عناصر با يکديگر و جابجايي آنها بر اساس نوع مرتب سازي • براي آرايه اي به طول n • n-1 بار مقايسه در يک مرحله • n-1 مرحله براي مقايسه داريم. 4 3 9 1 6آرايه اي به طول 5 مرحله اول 4 3 9 1 6 x[0] x[1] 4 9 3 1 6 x[1] x[2] 4 9 3 1 6 x[2] x[3] 4 9 3 6 1 x[3] x[4] مرحله دوم 9 4 3 6 1 x[0] x[1] 9 4 3 6 1 x[1] x[2] 9 4 6 3 1 x[2] x[3] 9 4 6 3 1 x[3] x[4] مرحله سوم 9 4 6 3 1 x[0] x[1] 9 6 4 3 1 x[1] x[2] 9 6 4 3 1 x[2] x[3] بعد از اين ديگر تغييري نداريم.
روش های مرتب سازی و جستجوی آرایه ها مثال 6-4: مرتب سازي حبابي تعدادي عدد Example 6-4: #include <iostream.h> void ginput(int [], int); void bubble(int [], int); void goutput(int [], int); int main() { const int k=7 ; int temp[7]; ginput(temp, k); bubble (temp, k); cout << “\nThe sorted data are :\n "; goutput(temp, k); return 0; } void ginput(int temp[], int len) { int i; for (i=0; i<len; i++) { cout << "enter number " << (i+1) << " : "; cin >> temp[i]; } } void bubble(int temp[], int len) { int i, j, item; for(i = len - 1 ; i > 0; i --) for(j = 0; j < i ; j++) if(temp[j] > temp[j + 1]) { item = temp[j] ; temp[j] = temp[j + 1]; temp[j + 1] = item ; } } void goutput(int temp[], int len) { int i; for(i=0 ; i < len; i++) cout << temp[i]; << “ ”; } enter number 1: 12 enter number 2: 14 enter number 3: 178 enter number 4: 43 enter number 5: 2124 enter number 6: 213 enter number 7: 92 The sorted data are : 12 14 43 93 178 213 2124
روش های مرتب سازی و جستجوی آرایه ها جستجو در آرايه • روش ترتيبي: • مقايسه عنصر مورد نظر با هريک از عناصر آرايه • اتمام جستجو: • يافتن عنصر مورد نظر • اتمام عناصر آرايه • روش دودويي: • آرايه بايد مرتب باشد. • ابتدا با عنصر وسط مقايسه مي شود در صورت عدم تساوي • اگر بزرگتر بود با عناصر بالايي آرايه مقايسه مي شود. • اگر کوچکتر بود با عناصر پاييني آرايه مقايسه مي شود. • ادامة جستجو تا يافتن عنصر يا اتمام آرايه
روش های مرتب سازی و جستجوی آرایه ها مثال 6-5: جستجويي ترتيبي در شماره دانشجويي Example 6-5: #include <iostream.h> void ginput(int [], int); int lsearch(int [], int, int); int main() { const int k = 5 ; int st[k], no; ginput(st, k); cout << "\nEnter a student # to search: "; cin >> no; if(lsearch(st, k, no) == -1) cout << "\n number " << no << " does not exist in list "; else cout << "\n number " << no << " exists in list."; return 0; } //************************** void ginput(int st[], int len) { int i; for(i = 0; i < len; i++) { cout << "enter student number " << i+1 << " : "; cin >> st[i]; } } //********************** int lsearch(int st[], int len, int no) { int i; for(i = 0; i < len; i++) if(st[i] == no) return i; return -1; } enter student number 1 : 121 enter student number 2 : 134 enter student number 3 : 215 enter student number 4 : 219 enter student number 5 : 123 Enter a student # to search: 215 Number 215 exists in list.
روش های مرتب سازی و جستجوی آرایه ها مثال 6-6: جستجويي دودويي در شماره دانشجويي Example 6-6: #include <iostream.h> void ginput(int [], int); void bubble(int [], int); int bsearch(int [], int, int); int main() { const int k = 5 ; int st[k], no; ginput(st, k); cout << "\nEnter a student # to search:"; cin >> no; bubble(st, k); if(bsearch(st, k, no) == -1) cout << "\n number " << no << " does not exist in list "; else cout << "\n number " << no << " exists in list."; return 0; } //********************** int bsearch(int st[], int len, int no) { int mid, low = 0, high = len - 1; while(low <= high){ mid = (low + high) / 2; if(no < st[mid]) high = mid - 1; else if(no > st[mid]) low = mid + 1; else return mid; } return -1; } enter student number 1 : 121 enter student number 2 : 134 enter student number 3 : 215 enter student number 4 : 219 enter student number 5 : 123 Enter a student # to search: 215 Number 215 exists in list.
آرایه های دو بعدی تعریف آرايه دو بعدي • دستيابي به عناصر : با دو انديس سطر و ستون • الگوي تعريف: • نوع آرايهنام آرايه [بعد 1][بعد 2] • بعد اول: تعداد سطر • بعد دوم: تعداد ستون • شروع هر انديس از صفر • نحوة ذخيره در حافظه : سطري y[0][0] y[1][0] y[2][0]
آرایه های دو بعدی مقدار اوليه آرايه چند بعدی • الگوي مقدار اوليه دادن به آرايه های چند بعدی: • نوع آرايهنام آرايه [بعد 1] [بعد 2] […] = {مقادير}; int y[2][3]={1,2,3,4,5,6}; int y[2][3]={{1,2,3},{4,5,6}}; int m[3][2][4]={{{1,2,3,4},{5,6,7,8}}, {{7,9,3,2},{4,6,8,3}}, {{7,2,6,3},{0,1,9,4}}};
آرایه های دو بعدی مثال 6-7: جدول ضرب Example 6-7: #include <iostream.h> int main() { int table[10][10], i, j ; for(i = 0; i < 10; i++) for(j = 0; j < 10; j++) table[i][j] = (i + 1)*(j + 1) ; for(i = 0; i < 10; i++) { for(j = 0; j < 10; j++) cout << table[i][j] << " " ; cout << endl ; } return 0; } • 2 3 4 5 6 7 8 9 10 • 2 4 6 8 10 12 14 16 18 20 • … • … • 9 18 27 36 45 54 63 72 81 90 • 10 20 30 40 50 60 70 80 90 100
آرایه های دو بعدی آرايه هاي دو بعدی به عنوان آرگومان تابع • تعريف پارامترها در توابع داراي آرگومان آرايه دو بعدي: • آرايه با طول مشخص • آرايه با طول نامشخص (طول سطر توسط آرگومان ديگر منتقل شود) • اشاره گر (فصل بعد) void f1 (int x[5][10]); void f2 (int x[][10], int row); void main() { int x[5][10]; … f1 (x); … f2 (x,5); } void f1 (int x[5][10]) { … } void func2 (int x[][10], int row) { … }
آرایه های دو بعدی مثال 6-8: يافتن بزرگترين عنصر هر سطر ماتريس Example 6-8: #include <iostream.h> void minput(int [][2], int); void mcal(int mat[][2], int); void main() { const int r = 3, c = 2; int mat[r][c]; minput(mat, r); mcal(mat, r); } //********************* void minput(int mat[][2], int r) { int i, j; for(i = 0; i < r; i++) for(j = 0; j < 2; j++) { cout << "enter mat[" << i << "][" << j << "]: "; cin >> mat[i][j]; } } //********************** void mcal(int mat[][2], int r) { int i, j, rmax; cout << " ROW \t\tMAX"; cout << "\n-------------------"; for(i = 0; i < r; i ++) { rmax = mat[i][0]; for(j = 1; j < 2; j++) if(mat[i][j] > rmax) rmax = mat[i][j]; cout << "\n " << i+1 << " \t\t " << rmax; } } enter mat[0][0]: 126 enter mat[0][1]: 112 enter mat[1][0]: 312 enter mat[1][1]: 152 enter mat[2][0]: 112 enter mat[2][1]: 424 ROW MAX ------------------------------- 1 126 2 312 3 424
آرایه های دو بعدی مثال 6-9: محاسبة حاصلضرب 2 ماتريس Example 6-9: #include <iostream.h> int main() { int mat1[2][3], mat2[3][4], mat3[2][4]={0} ; int i,j,k,l ; //read mat1 for(i=0 ; i<2 ; i++) for(j=0 ; j<3 ;j++) { cout << "enter mat1[" << i << "][" << j << "]: "; cin >> mat1[i][j]; } //read mat2 for(i=0 ; i<3 ; i++) for(j=0 ; j<4 ;j++) { cout << "enter mat2[" << i << "][" << j << "]: "; cin >> mat2[i][j]; } //multiply mat1 by mat2 for(i=0 ; i<2 ; i++) for(j=0 ; j<4 ;j++) { mat3[i][j]=0 ; for(k=0 ;k<3 ; k++) mat3[i][j] = mat3[i][j]+mat1[i][k]*mat2[k][j]; } cout << "\n the product of mat1 & mat2 " ; cout << " is :\n\n" ; for(i=0 ;i<2 ;i++) { for(j=0 ; j<4 ;j++) cout << mat3[i][j] << “\t"; cout << "\n" ; } return 0; } enter mat1[0][0]: 1 enter mat1[0][1]: -1 enter mat1[0][2]: 2 enter mat1[1][0]: 0 enter mat1[1][1]: 0 enter mat1[1][2]: -2 enter mat2[0][0]: 1 enter mat2[0][1]: 2 enter mat2[0][2]: -1 enter mat2[0][3]: -2 enter mat2[1][0]: 2 enter mat2[1][1]: 1 enter mat2[1][2]: 0 enter mat2[1][3]: -3 enter mat2[2][0]: 1 enter mat2[2][1]: -1 enter mat2[2][2]: -1 enter mat2[2][3]: 1 the product of mat1 & mat2 is : -1 1 3 -3 -2 2 2 -2
آرایه های دو بعدی مثال 6-10: يافتن کوچکترين عنصر آرايه Example 6-10: #include <iostream.h> int findmin(int [], int); void main() { int list[20], num, size=0 ; do{ cout << "type list[" << size << "] : "; cin >> list[size]; } while(list[size ++] != 0) ; size --; num = findmin(list, size) ; cout << "\n minimum is: " << num; } //*********************** int findmin(int arr[], int size) { int i, min1 ; min1 = arr[0] ; for(i = 0 ; i < size; i++) if(arr[i] < min1) min1 = arr[i] ; return(min1) ; } type list[0] : 27 type list[0] : 41 type list[0] : 15 type list[0] : 34 type list[0] : 32 type list[0] : 61 type list[0] : 123 type list[0] : 0 minimum is: 15
رشته ها تعریف • آرايه اي از کاراکترها • تعيين انتهاي رشته با کاراکتر NULL (‘\0’) • الگوي تعريف: • char s[20]; • مقدار اولية رشته: • رشته داخل کوتشين قرار گرفته به متغير نسبت داده شود. • هريک از کاراکترهاي رشته اي جداگانه اختصاص يابد. (تهي را نيز بايد تعيين نمود) char s1[ ] = “Programming”; char s2[12] = “Computer”; char s3[ ] = { ‘C’ , ‘+’ , ‘+’ , ‘\0’}; C o m p u t e r \0 ? ? ? p r o g r a m m i n g \0 C + + \0
رشته ها ورودي و خروجي رشته ها • استفاده از cin و cout • استفاده از تابع get( ) که عضو cin است. • الگوي استفاده: • cin.get (نام رشته, طول رشته) • cin.get (نام رشته, طول رشته, ‘جداکننده’) • در دستور اول enter مشخص کنندة انتهاي جمله است. • در دستور دوم کاراکتر تعيين شده مشخص کنندة انتهاي جمله است. • امکانات: با اين دستور رشته مي تواند شامل فاصله و Tab نيز باشد.
رشته ها مثال 6-11: تبديل حروف کوچک يک رشته به بزرگ Example 6-11: #include <iostream.h> void upper(char []); void main() { char s[21]; cout << “Enter a string: "; cin.get(s,20); upper(s); cout << “\nResult is: " << s; } //*********************** void upper(char s[]) { int i; for(i = 0; s[i]; i++) if(s[i] >= 'a' && s[i] <= 'z') s[i] -= 32; } Enter a string: changing characters to upper case Result is : CHANGING CHARACTERS
رشته ها مثال 6-12: تبديل يک کاراکتر به کاراکتر ديگر در يک رشته //Example 6-12: #include <iostream.h> #include <conio.h> void replace(char [], char, char); void main() { char string[50] ; char source_letter, target_letter ; int i ; cout << "\nEnter a string: "; cin.get(string,50) ; cout << "Enter source character: "; source_letter = getche() ; cout << "\nEnter target character: "; target_letter = getche() ; replace(string, source_letter, target_letter); cout << "\nThe result string is: "; cout << string ; } //************************ void replace(char string[], char source_letter, char target_letter){ int i; if(source_letter != target_letter) for(i = 0 ; string[i] ; i++) if(string[i] == source_letter) string[i] = target_letter; } Enter a string: changing one character of a string Enter source character: a Enter target character: e The result string is: chenging one cherecter of e string
رشته ها مثال 6-13: تعويض محتويات دو رشته با يکديگر Example 6-13: #include <iostream.h> void main() { char s1[81], s2[81], temp; int i, j; cout << "enter string <s1> : " ; cin.get(s1,81); cin.get(); cout << "enter string <s2> : " ; cin.get(s2,81); for(i = 0; s1[i] && s2[i]; i++) { temp = s1[i] ; s1[i] = s2[i] ; s2[i] = temp ; } if(s1[i]) { //s1 has more char j = i ; while(s1[i]) s2[i] = s1[i++] ; s2[i]='\0' ; s1[j]='\0' ; }//end of if else if (s2[i]) { //s2 has more char j = i ; while(s2[i]) s1[i] = s2[i++] ; s2[j]='\0' ; s1[i]='\0' ; } //end of else if cout << “\nnew content of s1 is: " << s1; cout << “\nnew content of s2 is: " << s2; } enter string <s1> : Computer programming enter string <s2> : C++ new content of s1 is: C++ new content of s2 is: Computer programming
رشته ها مثال 6-14: تعيين عملگر و عملوندها در عبارت محاسباتي Example 6-14: #include <iostream.h> void separate(char exp[], char oper[], int opnd[]); void main() { char expr[21], oper[21]; int opnd[21]; cout << "enter expression: "; cin.get (expr,21); separate(expr, oper, opnd); } //*************** void separate(char expr[], char oper[], int opnd[]) { int i, j = 0 , k = 0; for(i = 0; expr[i]; i ++) if(expr[i] >= '0' && expr[i] <= '9') opnd[j ++] = expr[i] - 48; else oper[k ++] = expr[i]; oper[k] = '\0'; cout << "operators are: “ << oper << endl; cout << "operands are: "; for(i = 0; i < j; i ++) cout << opnd[i] << " ";} enter expression: 2 * 4 / 3 - 4 + 2 operators are: * / - + operands are: 2 4 3 4 2
عملیات رشته ها انتساب رشته ها • انتساب رشته ها به صورت معمول صحيح نيست: • s2 = s1; • s = “Computer”; • روش صحيح: • strcpy (str1, str2); • فايل سرآيند مربوطه: string.h • در صورتيکه طول str2 بيشتر از طول str1 باشد در حافظه در ادامة str1 ذخيره مي شود.
عملیات رشته ها مقايسه رشته ها • مقايسه رشته ها به صورت معمول نيست: • s1 == s2 • روش صحيح: • strcmp(s1,s2) • فايل سرآيند مربوطه: string.h • خروجي تابع: • عدد صفر: دو رشته با هم مساوي هستند. • عدد منفي: s1 < s2 • عدد مثبت: s1 > s2
عملیات رشته ها الحاق دو رشته • الگوي استفاده: • strcat(s1, s2); • فايل سرآيند مربوطه: string.h • رشتة s2 در انتهاي رشتة s1 قرار مي گيرد.
عملیات رشته ها مثال 6-16: جستجوي يک نام در يک ليست Example 6-16: #include <iostream.h> #include <string.h> void bubble(char [][21], int); int bsearch(char [][21], char [], int); void main() { const int n = 5; int i; char name[21], arr [n][21]; for(i = 0; i < n; i ++){ cout << "Enter name " << (i + 1) << " : " ; cin.get(arr[i], 20); cin.get(); } bubble(arr, n); cout << "Enter one name for search :" ; cin.get(name, 20) ; if(bsearch(arr, name, n) == -1) cout << "Name " << name << " does not exist in table." ; else cout << "Name " << name << " exists in table." ; } //******************** void bubble(char arr[5][21], int n) { int i, j; char temp[21]; for(i = n - 1; i > 0; i --) for(j = 0; j < i; j++) if(strcmp(arr[j], arr[j + 1]) > 0){ strcpy(temp, arr[j]); strcpy(arr[j], arr[j + 1]); strcpy(arr[j + 1], temp); } } //******************* int bsearch(char arr[5][21], char name[21], int n) { int mid, low = 0, high = n - 1; while(low <= high){ mid = (low + high) / 2; if(strcmp(name, arr[mid]) < 0) high = mid - 1; else if(strcmp(name, arr[mid]) > 0) low = mid + 1; else return mid; } return -1; } Enter name1 : Ali Enter name 2 : Mohammad Enter name 3 : Hussein Enter name 4 : Mahdi Enter name 5 : Hassn Enter one name for search : Javad Name Javad does not exist in table.