Programming -2 برمجة -2 المحاضرة-5 Lecture-5.

Slides:



Advertisements
Similar presentations
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved Introduction Arrays –Structures of related data items –Static entity (same size throughout program)
#include using namespace std; void main() { int a[3]={10,11,23}; for(int i=0;i
Array, Pointer and Reference ( V ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Esempi Puntatori e Stringhe1 // This program puts values into an array, sorts the values into // ascending order, and prints the resulting array. #include.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 3 Arrays & Pointers.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
1 Lecture 8 Arrays Part II Sorting Arrays Sorting data  Important computing application  Virtually every organization must sort some data Massive.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
1 Arrays and Vectors Chapter 7 Arrays and Vectors Chapter 7.
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
 2003 Prentice Hall, Inc. All rights reserved. Outline 1 fig04_03.cpp (1 of 2) 1 // Fig. 4.3: fig04_03.cpp 2 // Initializing an array. 3 #include 4 5.
String as Arrays, Array Sorting C++ Programming Technologies.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 5: Pointer Outline Chapter 5 Pointer continue Call by reference Pointer arithmatic Debugging.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
1 Structure of Simple C++ Program Chapter 1 09/09/13.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
C++ Programming Lecture 18 Pointers – Part II The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Searching Arrays Linear search Binary search small arrays
Chapter 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Arrays (Static) Introduction Data Structures Arrays Declaring Arrays
Arrays Outline 1 Introduction 2 Arrays 3 Declaring Arrays
Command Line Arguments
Programming Fundamental
Engineering Problem Solving with C++, Etter
C++ Programming Lecture 15 Arrays – Part II
Basic Array Definition
Sorting Algorithms.
CSC 113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Multi-dimensional Array
آرايه ها اصول كامپيوتر 1.
C++ Programming Lecture 15 Arrays – Part II
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Arrays Kingdom of Saudi Arabia
Arrays November 8, 2017.
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
Arrays as Lists with examples.
CISC181 Introduction to Computer Science Dr
C++ Programming Lecture 16 Arrays – Part III
4.1 Introduction Arrays A few types Structures of related data items
Arrays Kingdom of Saudi Arabia
Chapter 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Engineering Problem Solving with C++, Etter
Arrays of Two-Dimensions
7 Arrays.
C++ Pointers and Strings
CHAPTER 2 Arrays and Vectors.
Chapter 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Chapter 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Capitolo 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Arrays Arrays A few types Structures of related data items
CHAPTER 2 Arrays and Vectors.
Lecture 2 Arrays & Pointers May 17, 2004
C++ Programming Lecture 18 Pointers – Part II
Pointers & Functions.
Chapter 3 Arrays Dr. A. PHILIP AROKIADOSS Assistant Professor
CS-161 Computer Programming Lecture 15 & 16: Arrays II
Lecture 2 Arrays & Pointers September 7, 2004
CISC181 Introduction to Computer Science Dr
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
Vectors.
4.9 Multiple-Subscripted Arrays
4.1 Introduction Arrays A few types Structures of related data items
ADT LIST So far we have seen the following operations on a “list”:
Presentation transcript:

Programming -2 برمجة -2 المحاضرة-5 Lecture-5

Arrays تابع المصفوفات

المحتويات النصوص Strings تمرير المصفوفات إلى الدوال ترتيب المصفوفات مصفوفات ذات البُعدين Two-Dimensional Arrays أمثلة وملاحظات

Strings النصوص النص في لغة C++ هو عبارة عن مصفوفة من الحروف + null character null character يُكتب ‘\0’ (سلاش زيرو) ويُستخدم لإنهاء النص يمكن إنشاء النص عن طريق المصفوفات كالتالي: char string1[ ] = “hello”; char string2[6] = “hello”; char string3[ ] = {‘h’,’e’,’l’,’l’,’o’,’\0’}; char string4[6] = {‘h’,’e’,’l’,’l’,’o’,’\0’}; char string5[6] = {‘h’,’e’,’l’,’l’,’o’}; جميع التعريفات التي بالأعلى صحيحة يمكن كتابة(‘\0’) null character بشكل صريح أو عدم كتابته, في حالة عدم كتابته سوف يضيفه الكومبايلر تلقائياً لاحظي أن حجم المصفوفة هو عبارة عن عدد الحروف + ‘\0’

حجم المصفوفة = 6 (عدد الحروف 5 + 0’\’ = (6 تابع . . . من الأفضل عند إنشاء مصفوفة من الحروف عدم كتابة الحجم حتى لا تحصل مشكلة عند نسيان null character حجم المصفوفة = عدد الحروف + null character مثال: (عدم كتابة null) char string1[] = "hello"; حجم المصفوفة = 6 (عدد الحروف 5 + 0’\’ = (6 مثال: (كتابة null) char string1[] = {‘h’,’e’,’l’,’l’,’o’,\0’}; جميع النصوص تنتهي بـ null ('\0') سواء تم كتابته بشكل صريح أو لا, لأن الكومبايلر سوف يضيفه في حالة عدم كتابته

تابع . . . قراءة عناصر مصفوفة الحروف من لوحة المفاتيح: char string1[10]; cin>> string1; الكومبايلر سيضع الحروف التي أدخلها المستخدم في string1 يتوقف عن وضع الحروف في المصفوفة عند أول مسافة فارغة مدخلة يضيف null بعد آخر حرف أدخله المستخدم يجب تفادي إدخال عناصر أكبر من حجم المصفوفة لاحتمال حدوث خطأ! طباعة عناصر مصفوفة الحروف: cout<< string1; سيتم طباعة عناصر المصفوفة حتى يصل إلى null , فتتوقف عملية الطباعة

مثال: برنامج يوضح طريقة قراءة وطباعة مصفوفات الحروف #include <iostream> using namespace std; int main() { (المسافات تعتبر أحرف) // حجم المصفوفة هو 20 char string1[ ] = “What is your name? "; char string2[10]; cout<< string1; cin>> string2; cout<< “Hello " << string2 << endl; system(“pause"); return 0; } // end main What is your name? Amal Hello Amal

تمرير المصفوفات إلى الدوال لتمرير مصفوفة إلى دالة, نكتب اسم المصفوفة بدون الأقواس [ ]في المناداة إعلان الدالة وتعريف الدالة سيكون بالشكل التالي: مثال: مصفوفة حجمها 10 ونوعها int, دالة اسمها f1,نوع بياناتها المسترجعة void int a[10]; تعريف المصفوفة: f1(a); مناداة الدالة وإرسال المصفوفة إليها: void f1(int a[10]); إعلان الدالة: void f1(int a[10]) تعريف الدالة: { … }

مثال: تمرير مصفوفة إلى دالة وطباعة عناصرها #include <iostream> using namespace std; void printArray(int a[4]); int main() { int a[4] = {1, 2, 3, 4}; cout<< "Array elements are: "; printArray(a); system("pause"); return 0; } // end main void printArray(int a[4]) for(int i = 0; i < 4; i++) cout<< a[i] << " "; cout<< endl; } 9 Array elements are: 1 2 3 4

ملاحظات كتابة اسم المصفوفة وحجمها في إعلان الدالة اختياري بمعنى أنه يمكن كتابة إعلان الدالة في المثال السابق كالتالي: void printArray(int a[4]); void printArray(int []); كتابة حجم المصفوفة في رأس الدالة اختياري بمعنى أنه يمكن كتابة رأس الدالة في المثال السابق كالتالي: void printArray(int a[4]) void printArray(int a[]) { { for(int i = 0; i < 4; i++) for(int i = 0; i < 4; i++) cout<< a[i] << " "; cout<< a[i] << " "; cout<< endl; cout<< endl; } } يمكن تمرير/إرسال حجم المصفوفة إلى الدالة كمتغير, مثال في السلايد القادم ...

مثال: تمرير مصفوفة وحجمها إلى دالة وطباعة عناصرها #include <iostream> using namespace std; void printArray(int a[], int arraySize); int main() { const int arraySize = 4; int a[arraySize] = {1, 2, 3, 4}; cout<< "Array elements are: "; printArray(a, arraySize); system("pause"); return 0; } // end main void printArray(int a[], int arraySize) for(int i = 0; i < arraySize; i++) cout<< a[i] << " "; cout<< endl; } 11 Array elements are: 1 2 3 4

مثال: تمرير مصفوفة وحجمها إلى دالة وتغيير قيم عناصر المصفوفة #include <iostream> using namespace std; void printArray(int a[], int arraySize); void modifyArray(int a[], int arraySize); int main() { const int arraySize = 4; int a[arraySize] = {1, 2, 3, 4}; // طباعة قيم عناصر المصفوفة قبل التغيير cout<< "Original array elements are: "; printArray(a, arraySize); // إرسال المصفوفة إلى الدالة لتغيير قيم عناصرها modifyArray(a, arraySize); // طباعة قيم عناصر المصفوفة بعد التغيير cout<< "Modified array elements are: "; system("pause"); return 0; } // end main 12

Original array elements are: 1 2 3 4 // دالة طباعة قيم عناصر المصفوفة void printArray(int a[], int arraySize) { for(int i = 0; i < arraySize; i++) cout<< a[i] << " "; cout<< endl; } // دالة تغيير قيم عناصر المصفوفة void modifyArray(int a[], int arraySize) a[i] *= 2; 13 Original array elements are: 1 2 3 4 Modified array elements are: 2 4 6 8

ملاحظات نلاحظ أن تغيير قيم عناصر المصفوفة حصل داخل دالة اسمها modifyArray وعند طباعة المصفوفة من داخل الدالة الرئيسية main, تم طباعة قيم العناصر بعد التغيير بمعنى أن التغيير ثبت على المصفوفة الأصلية التي بداخل الـ main مع أن عملية تغيير قيم عناصر المصفوفة حصلت داخل دالة أخرى هذه العملية تسمى pass by reference (تمرير بـ المرجع), تعني أنه عند إرسال مصفوفة إلى دالة ما, وأجرت هذه الدالة تغييراً على قيم عناصر المصفوفة, هذا التغيير سوف يؤثر على المصفوفة الأصلية الموجودة في الـ main

أمثلة على المصفوفات (1) (2)

أمثلة على المصفوفات (3) (4)

ترتيب المصفوفات ترتيب المصفوفات Bubble Sort هو إعادة ترتيب قيم عناصر المصفوفات تصاعدياً أو تنازلياً هناك عدة خوارزميات لترتيب العناصر, أشهرها Bubble Sort Bubble Sort طريقة عمل هذه الخوارزمية هي مقارنة كل عنصرين متجاورين, وتبديل قيمهما إذا احتاجت إلى إعادة ترتيب. خطوات ترتيب المصفوفة تصاعدياً: 1- نمر على عناصر المصفوفة جميعا. 2- عندما نجد عنصر أصغر من العنصر الذى يسبقه(قبله) نقوم بتبديلهما. 3- نمر هكذا على جميع عناصر المصفوفة حتى ننهي الدورة الاولى. 4- نعيد المرور على عناصر المصفوفة مرة أخرى و نرى إن كان هناك أحد العناصر أصغر مما يسبقه و نبدلهما حتى ننهي هذه الدورة. 5- نعيدها مرارا و تكرارا حتى ننتهي تماما من ترتيبها.

Bubble Sort Code #include <iostream> #include <iomanip> using namespace std; void main() { const int arraySize = 10; // size of array a int a[ arraySize ] = { 2, 10, 4, 8, 6, 12, 89, 68, 45, 37 }; int temp; // temporary location used to swap array elements cout << "Data items in original order\n"; for ( int i = 0; i < arraySize; i++ ) cout << setw( 4 ) << a[ i ]; for ( int pass = 0; pass < arraySize - 1; pass++ ) for ( int j = 0; j < arraySize - 1; j++ ) if ( a[ j ] > a[ j + 1 ] ) temp = a[ j ]; a[ j ] = a[ j + 1 ]; a[ j + 1 ] = temp; } cout << "\nData items in ascending order\n"; for ( int k = 0; k < arraySize; k++ ) cout << setw( 4 ) << a[ k ]; cout << endl; system("pause"); Bubble Sort Code

تابع المثال السابق . . . Output: Data items in original order 2 10 4 8 6 12 89 68 45 37 Data items in ascending order 2 4 6 8 10 12 37 45 68 89

Two-Dimensional Arrays مصفوفات ذات البُعدين عبارة عن جدول ذو صفوف وأعمدة الإعلان عن المصفوفات ذات البُعدين: كتابة نوع البيانات, اسم المصفوفة, عدد الصفوف, وعدد الأعمدة dataType arrayName[i][j]; حيث أن: i عدد الصفوف, j عدد الأعمدة مثال: int array[3][4]; مصفوفة ذات بُعدين تتكون من 3 صفوف و 4أعمدة 0 الصف 1 الصف 2 الصف 0 العمود 1 العمود 2 العمود 3 العمود a[ 0 ][ 0 ] a[ 1 ][ 0 ] a[ 2 ][ 0 ] a[ 0 ][ 1 ] a[ 1 ][ 1 ] a[ 2 ][ 1 ] a[ 0 ][ 2 ] a[ 1 ][ 2 ] a[ 2 ][ 2 ] a[ 0 ][ 3 ] a[ 1 ][ 3 ] a[ 2 ][ 3 ] العمود الصف اسم المصفوفة

تابع . . . إعطاء القيم لعناصر المصفوفات ذات البُعدين: نكتب قيم الصفوف بين أقواس المجموعة ونفصل بينها بفاصلة (يمكن عدم كتابة الأقواس لأن الكومبايلر يهملها) الترتيب مهم! الصف الأول ثم الصف الثاني وهكذا ... مثال: مصفوفة تتكون من صفين وعمودين, قيم الصف الأول (1,2) , قيم الصف الثاني (3,4) int a[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } }; OR int a[ 2 ][ 2 ] = { 1, 2, 3, 4 }; 0 الصف 1 الصف 0 العمود 1 العمود 0 الصف 1 2 3 4 1 الصف

تابع . . . طباعة عنصر محدد في المصفوفة ذات البُعدين: int b[ 2 ][ 2 ] = { 1, 0, 3, 4 }; cout<< b[0][1]; سيطبع 0على الشاشة لا يمكن دمج رقم الصف والعمود داخل قوس واحد! مثل: cout<< b[ 0, 1 ]; Error!!! إرسال المصفوفة ذات البُعدين إلى دالة: مثل المصفوفات ذات البُعد الواحد, نكتب اسم المصفوفة بدون الأقواس في المناداة يجب كتابة عدد الأعمدة في الإعلان ورأس الدالة لا داعي لتحديد عدد الصفوف واسم المصفوفة في الإعلان لا داعي لتحديد عدد الصفوف في رأس الدالة 0 العمود 1 العمود 1 0 3 4 0 الصف 1 الصف

تابع . . . قراءة عناصر المصفوفة ذات البُعدين: int a[5][3]; for(int i = 0; i < 5; i++) for(int j = 0; j < 3; j++) cin>> a[i][j]; طباعة عناصر المصفوفة ذات البُعدين: cout<< a[i][j];

أمثلة X int a[2][3]={{1,2,0},{3,4,5}}; OR int a[2][3]={{1,2},{3,4,5}}; 1 2 0 3 4 5 int b[2][3]={{1,2,0},{3,4,5,6}}; X int c[2][3]={{1},{2,5}}; 1 0 0 2 5 0 int d[2][3]={1,2,5}; 1 2 5 0 0 0 int e[2][4]={{1,2},5}; 1 2 0 0 5 0 0 0

مثال: تعريف مصفوفات ذات البُعدين وإرسالها إلى الدوال #include <iostream> using namespace std; void printArray( int [][ 3 ] ); int main() { int array1[ 2 ][ 3 ] = { { 1, 2, 3 }, { 4, 5, 6 } }; int array2[ 2 ][ 3 ] = { 1, 2, 3, 4, 5 }; int array3[ 2 ][ 3 ] = { { 1, 2 }, { 4 } }; cout<< "Values in array1 are:" << endl; printArray( array1 ); cout<< "Values in array2 are:" << endl; printArray( array2 ); cout<< "Values in array3 are:" << endl; printArray( array3 ); return 0; // indicates successful termination } // end main

// function to output array with two rows and three columns void printArray( int a[][ 3 ] ) { for ( int i = 0; i < 2; i++ ) { // for each row for ( int j = 0; j < 3; j++ ) // output column values cout<< a[ i ][ j ] << ' '; cout<< endl; // start new line of output } // end outer for structure } // end function printArray Values in array1 are: 1 2 3 4 5 6 Values in array2 are: 4 5 0 Values in array3 are: 1 2 0 4 0 0