Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures Lecture 2: Array Azhar Maqsood NUST Institute of Information Technology (NIIT)

Similar presentations


Presentation on theme: "Data Structures Lecture 2: Array Azhar Maqsood NUST Institute of Information Technology (NIIT)"— Presentation transcript:

1 Data Structures Lecture 2: Array Azhar Maqsood NUST Institute of Information Technology (NIIT)

2 Features of c++ you need to know Variables Variables Parameter Passing Parameter Passing Pointers Pointers Classes and Objects Classes and Objects Inheritance Inheritance Others Others

3 Variables You must be very comfortable with the notion of a variable as an abstraction for a region of a memory. A variable has attributes such as name, type, value, address size, lifetime and scope. You must be very comfortable with the notion of a variable as an abstraction for a region of a memory. A variable has attributes such as name, type, value, address size, lifetime and scope.

4 Parameter Passing There are two parameter passing mechanisms in C++: pass-by-value and pass-by-reference. It is essential that you understand the behavioral difference between the two methods as well as the performance implications of using each of them. There are two parameter passing mechanisms in C++: pass-by-value and pass-by-reference. It is essential that you understand the behavioral difference between the two methods as well as the performance implications of using each of them.

5 Pointers Mastering the use of pointers is essential when programming in C++. The key to understanding pointers is to : Mastering the use of pointers is essential when programming in C++. The key to understanding pointers is to :  recognize that a pointer variable has exactly the same set of attributes as any other C++ variable.  It is crucial that you keep straight the distinctions between the :  value of a pointer,  the address of a pointer  the object to which a pointer points.

6 Classes and Objects A C++ class encapsulates a set of A C++ class encapsulates a set of  Values  The values are represented by the member variables of the class  operations.  The operations by the member functions of the class.  In C++ a class definition introduces a new type.  The instances of a class type are called objects.  Special role of the constructor and the destructor member functions of a class and when the C++ compiler invokes each of them.

7 Inheritance In C++ one class may be derived from another. The derived class inherits In C++ one class may be derived from another. The derived class inherits  all the member variables and the member functions of the base class or classes.  In addition, inherited member functions can be overridden in the derived class and new member variables and functions can be defined.  You should understand how the compiler determines the code to execute when a particular member function is called.

8 Inheritance In C++ one class may be derived from another. In C++ one class may be derived from another.  The derived class inherits all the member variables and the member functions of the base class or classes.  In addition, inherited member functions can be overridden in the derived class and new member variables and functions can be defined.  You should understand how the compiler determines the code to execute when a particular member function is called.

9 Other Features features such as features such as  templates,  exceptions  run-time type information  and more as we learn further

10 Features of c++ you need to know Variables Variables Parameter Passing Parameter Passing Pointers Pointers Classes and Objects Classes and Objects Inheritance Inheritance Others Others

11 In Today’s lecture How to Input arrays How to Input arrays How to process arrays How to process arrays How to insert an item in an array How to insert an item in an array How to pass an array How to pass an array Structures Structures Their basic implementation Their basic implementation

12 The first Data Structure An Array! An Array! The simplest form of an Array is a one dimensional array that may be defined as a finite ordered set of homogenous elements For Example For Example int a[100];

13 Basic Operations Extraction Extraction  A function that accepts an array “a” and an index “i”, and returns an element of the array.  Example a[i] Storing Storing  It accepts array “a” an index “i” and an element x.  Example  a[i]=x

14 One Dimensional Array range = upper - lower+1 range = upper - lower+1 Neither the upper bound nor the lower bound can be changed and as well as the range can be changed during the program execution. Neither the upper bound nor the lower bound can be changed and as well as the range can be changed during the program execution. Lower Bound [0] Upper Bound Range

15 Implementation of 1Dimenional Array int b[100]; int b[100]; Reserves 100 successive locations, each large enough to contain a single integer. Reserves 100 successive locations, each large enough to contain a single integer. The address of the first of these locations is called the base Address: base(b) The address of the first of these locations is called the base Address: base(b) Reference to element b[0] is to the element at location base(b) Reference to element b[0] is to the element at location base(b) Reference to b[1] is to the element at Reference to b[1] is to the element at base(b) + 1* esize Hence b gives you the starting memory address of the array b b gives you the starting memory address of the array b

16 Memory view of an array 23478 2 3 4 7 8 a[0] a[1] a[2] a[3] a[4] int a[5] //Help me give output of this program void main(void) { int a[5] = { 2,3,4,7,8 }; cout << a[3] << endl; cout << a <<endl; cout << *(a+1) <<endl; cout << *a+1 <<endl; } 0x4 0x8 0xC 0x10 0x14

17 *a +1 without brackets leads to adding 1 to contents (value) of a[0]

18 Array of Variable Length A[0] A[1] A[2] A[3] 5HELLO 6BICSE4

19 Array of Variable length cont. A[0] A[1] A[2] A[3] HELLO\0 BICSE2\0 COTTON\0 pen\0 char *A[4]

20 Character arrays (Strings) Review ALI\0 A L I \0 Garbage A[0] A[1] A[2] A[3] A[4]

21 How to determine length of the string #include #include int len_str(char str[25]); int len_str_while(char s[25]); void main(void) { char l[25]; char l[25]; cin >> l; cout << len_str(l) << endl << len_str_while(l); } //function with for loop int len_str(char s[25]) { for (int i = 0; s[i] != '\0'; i++); return i; }

22 Cont… //another method using while loop int len_str_while(char s[25]) { int i=0; while (s[i] != '\0') {i++;} return i; }

23 Two dimensional Arrays

24 #include #include void mult_matrices(int a[][2], int b[][2], int result[][2]); void print_matrix(int a[][2]); void main(void) { int p[2][2] = { {10, 20}, {30,40 } }; int p[2][2] = { {10, 20}, {30,40 } }; int q[2][2] = { {50, 60}, {70, 80} }; int q[2][2] = { {50, 60}, {70, 80} }; int r[2][2]; int r[2][2]; print_matrix(p); print_matrix(p); print_matrix(q); print_matrix(q); mult_matrices(p, q, r); mult_matrices(p, q, r); print_matrix(r); print_matrix(r);} Main Program Why New Array?

25 Print a Matrix void print_matrix(int a[][2]) { int i, j; int i, j; for (i=0; i<2; i++) for (i=0; i<2; i++) { for (j=0; j<2; j++) for (j=0; j<2; j++) { cout << "\t" << a[i][j]; cout << "\t" << a[i][j]; } cout << endl; cout << endl; } } Why mention 2?

26 Multiply a Matrix void mult_matrices(int a[][2], int b[][2], int result[][2]) { int i, j, k; int i, j, k; for(i=0; i<2; i++) for(i=0; i<2; i++) { for(j=0; j<2; j++) for(j=0; j<2; j++) { result[i][j] = 0; result[i][j] = 0; for(k=0; k<2; k++) for(k=0; k<2; k++) { result[i][j] = result[i][j] + (a[i][k] * b[k][j]); result[i][j] = result[i][j] + (a[i][k] * b[k][j]); } } }} Why passed so many variables? Are these Variables Called by reference or By value?

27 Submission Date 13th March, 2008 13th March, 2008 Before 1500 Hrs Before 1500 Hrs Late submission rules apply Late submission rules apply


Download ppt "Data Structures Lecture 2: Array Azhar Maqsood NUST Institute of Information Technology (NIIT)"

Similar presentations


Ads by Google