Unit 5: Pointers and Arrays - Tin học đại cương (IT1110) | Trường Đại học Bách khoa Hà Nội

Unit 5: Pointers and Array

lOMoARcPSD| 27879799
Pointers and Arrays
lOMoARcPSD| 27879799
Simulate call-by-reference
lOMoARcPSD| 27879799
lOMoARcPSD| 27879799
Directly and indirectly referencing a variable
lOMoARcPSD| 27879799
lOMoARcPSD| 27879799
lOMoARcPSD| 27879799
lOMoARcPSD| 27879799
lOMoARcPSD| 27879799
lOMoARcPSD| 27879799
lOMoARcPSD| 27879799
lOMoARcPSD| 27879799
Array Terminology
An array is composed of elements
Elements in an array have a common name
The list as a whole is referenced through the
common name
In the scope of the course elements of an array are
of the same type — the base type
Elements of an array are referenced by subscripng
or indexing the common name
lOMoARcPSD| 27879799
lOMoARcPSD| 27879799
Restricons
Subscripts are denoted as expressions within
brackets: [ ]
Base type can be any standard, librarydened,
or programmer - dened type
The index type is integer and the index range
must be 0 ... n-1 where n is a
programmerdened constant expression.
lOMoARcPSD| 27879799
Examples of array declaraon
Suppose const int N = 20; const int M = 40;
const int MaxStringSize = 80; const int
MaxListSize = 1000;
Then the following are all correct array declaraons
int A[10]; // array of 10 ints char B[MaxStringSize]; // array of
80 chars double C[M*N]; // array of 800 oats int
Values[MaxListSize]; // array of 1000 ints Raonal D[N-15]; // array
of 5 Raonals int F[5]={0,1,2,3,4}
lOMoARcPSD| 27879799
lOMoARcPSD| 27879799
Basic Operaons on one dimensional
arrays
Input data
Print the list
Find smallest (greatest) value, indexes of
smallest elements of the list
Search the list with a key
Sort the list in ascending (descending) order
Insert an element
Delete an element7
lOMoARcPSD| 27879799
lOMoARcPSD| 27879799
lOMoARcPSD| 27879799
20
| 1/58

Preview text:

lOMoAR cPSD| 27879799 Pointers and Arrays lOMoAR cPSD| 27879799 Simulate call-by-reference lOMoAR cPSD| 27879799 lOMoAR cPSD| 27879799
Directly and indirectly referencing a variable lOMoAR cPSD| 27879799 lOMoAR cPSD| 27879799 lOMoAR cPSD| 27879799 lOMoAR cPSD| 27879799 lOMoAR cPSD| 27879799 lOMoAR cPSD| 27879799 lOMoAR cPSD| 27879799 lOMoAR cPSD| 27879799 Array Terminology
• An array is composed of elements
• Elements in an array have a common name
The list as a whole is referenced through the common name
• In the scope of the course elements of an array are
of the same type — the base type
• Elements of an array are referenced by subscripting or indexing the common name lOMoAR cPSD| 27879799 lOMoAR cPSD| 27879799 Restrictions
• Subscripts are denoted as expressions within brackets: [ ]
• Base type can be any standard, librarydefined, or programmer - defined type
• The index type is integer and the index range
must be 0 ... n-1 where n is a
programmerdefined constant expression. lOMoAR cPSD| 27879799 Examples of array declaration
Suppose const int N = 20; const int M = 40;
const int MaxStringSize = 80; const int MaxListSize = 1000;
Then the following are all correct array declarations
int A[10]; // array of 10 ints char B[MaxStringSize]; // array of
80 chars double C[M*N]; // array of 800 floats int
Values[MaxListSize]; // array of 1000 ints Rational D[N-15]; // array
of 5 Rationals int F[5]={0,1,2,3,4} lOMoAR cPSD| 27879799 lOMoAR cPSD| 27879799
Basic Operations on one dimensional arrays • Input data • Print the list
• Find smallest (greatest) value, indexes of smallest elements of the list
• Search the list with a key
• Sort the list in ascending (descending) order • Insert an element • Delete an element7 lOMoAR cPSD| 27879799 lOMoAR cPSD| 27879799 lOMoAR cPSD| 27879799 20