lec-5 arrays adt
TRANSCRIPT
-
8/3/2019 Lec-5 Arrays ADT
1/18
Arrays
-
8/3/2019 Lec-5 Arrays ADT
2/18
C++ Style Data Structures: Arrays(1)
An ordered set (sequence) with a fixed
number of elements, all of the same
type,where the basic operation is
direct access to each element in the
array so values can be retrieved from
or stored in this element.
-
8/3/2019 Lec-5 Arrays ADT
3/18
C++ Style Data Structures: Arrays(2)
Properties:
Ordered so thereisa first element,asecond one,etc.
Fixed numberofelements fixed capacity
Elementsmust be thesame type(andsize);@ usearrays only forhomogeneous datasets.
Direct access: Accessan element by giving itslocation
The time to accesseachelement is thesame forall
elements,regardless of position. in contrast to sequentialaccess(where to accessan
element, onemust first accessall those that precedeit.)
-
8/3/2019 Lec-5 Arrays ADT
4/18
Declaring arrays in C++
whereelement_type is any type
array_name is the name of the array any valid identifier
CAPACITY (a positive integerconstant) is the numberofelementsin the array
score[0]
score[1]
score[2]score[3]
score[99]
.
.
.
.
.
.
element_type array_name[CAPACITY];
e.g., double score[100];
Theelements (orpositions) of the array are indexed 0, 1, 2,..
.,CAPACITY- 1.
Can't input thecapacity,
Why?
The compilerreserves a block of consecutive memorylocations,enough to hold CAPACITYvalues of type
element_type.
-
8/3/2019 Lec-5 Arrays ADT
5/18
indices numbered 0, 1, 2,...,CAPACITY - 1
Howwell does C/C++ implement an array ADT?
As an ADT In C++
ordered
fixed size
same typeelements
direct access
element_type is the type ofelements
CAPACITY specifies the capacity of the array
subscript operator[]
-
8/3/2019 Lec-5 Arrays ADT
6/18
an arrayliteral
Array Initialization
Example:
double rate[5] = {0.11, 0.13, 0.16, 0.18, 0.21};
Note 1: If fewervalues supplied than array's capacity,remainingelements
assigned 0.
double rate[5] = {0.11, 0.13, 0.16};
Note 2: It is an errorif more values are supplied than the declared size of the array.
How this erroris handled,however,will vary from one compilerto another.
rate
0
1
2
3
4
0.11 0.13 0.16 0 0
rate
0 1
2
3
40.11 0.13 0.16 0.18 0.21
In C++, arrays can be initialized when they are declared.Numeric arrays:
element_type num_array[CAPACITY] = {list_of_initial_values};
-
8/3/2019 Lec-5 Arrays ADT
7/18
Note 1: If fewervalues are supplied than the declared size of the array,
the zeroes used to fill un-initialized elements are interpreted asthe null character'\0' whose ASCII code is 0.
const int NAME_LENGTH = 10;char collegeName[NAME_LENGTH]={'C', 'a', 'l', 'v', 'i', 'n'};
vowel
0 1 2 3 4
A E I O U
char vowel[5] = {'A', 'E', 'I', 'O', 'U'};
Character Arrays:
Character arrays may be initialized in the same manneras numeric arrays.
declares vowel to be an array of 5 characters and initializes it as follows:
collegeName
0 1 2 3 4 5 6 7 8 9
C a l v i n \0 \0 \0 \0
-
8/3/2019 Lec-5 Arrays ADT
8/18
AddressesWhen an array is declared, the address of the first byte(orword) in the block of
memory associated with the array is called thebase address of the array.
Each array reference must be translated into an offsetfrom this base address.
Forexample, ifeachelement of array score will be stored in 8 bytes and the baseaddress ofscore is 0x1396. A statement such as
cout
-
8/3/2019 Lec-5 Arrays ADT
9/18
The value of array_name is actually thebase address ofarray_name
array_name + index is the address ofarray_name[index].
An array reference array_name[index]
is equivalent to
Forexample, the following statements of pseudocode areequivalent:
print score[3]print *(score + 3)
Note: No bounds checking of indices is done!
* is thedereferencingoperator
*refreturns thecontents of the memory location with address ref
*(array_name + index)
What will happen incase
ofgoing overboard
-
8/3/2019 Lec-5 Arrays ADT
10/18
Problems with Arrays
1.The capacity of Array can NOT change during program execution.
What is the problem?
Memory wastage
Out ofrangeerrors
2. Arrays are NOT self contained objects
What is the problem?
No way to find the last value stored.
Not a self contained object as perOOP principles.
-
8/3/2019 Lec-5 Arrays ADT
11/18
C++ Style MultidimensionalArrays
Most high level languages support arrays with more than one dimension.
2D arrays are useful when data has to be arranged in tabularform.
Higherdimensional arrays appropriatewhen several characteristics associated
with data.
Test 1 Test 2 Test 3 Test 4Student 1 99.0 93.5 89.0 91.0
Student 2 66.0 68.0 84.5 82.0
Student 3 88.5 78.5 70.0 65.0: : : : :: : : : :
Student-n 100.0 99.5 100.0 99.0
Forstorage and processing, use a two-dimensional array.
Example: A table of test scores forseveral different students on
several different tests.
-
8/3/2019 Lec-5 Arrays ADT
12/18
Declaring Two-Dimensional Arrays
Standard form of declaration:
element_type array_name[NUM_ROWS][NUM_COLUMNS];
Example:const int NUM_ROWS = 30,
NUM_COLUMNS = 4;
double scoresTable[NUM_ROWS][NUM_COLUMNS];
Initializationj List the initial values in braces,row by row;
j May use internal braces foreachrow to improvereadability.
Example:double rates[][] = {{0.50, 0.55, 0.53}, // first row
{0.63, 0.58, 0.55}}; // second row
..
.
[0][1][2][3]
[29]
[0] [[1] [2] [3]
-
8/3/2019 Lec-5 Arrays ADT
13/18
Processing Two-Dimensional ArraysjRemember: Rows (and) columns are numbered from zero!!
jUsedoubly-indexed variables:
scoresT
able[2][3] is theentry in row 2 and column 3oorow index column index
jUsenested loops to vary the two indices, most often in a rowwise manner.
Counting
from 0
-
8/3/2019 Lec-5 Arrays ADT
14/18
Higher-Dimensional Arrays
The methods for2D arrays extend in the obvious way to 3D arrays.
Example: To store and process a table of test scores forseveral different
students on several different tests for several different semesters
const int SEMS = 10, STUDENTS = 30, TESTS = 4;
typedef double ThreeDimArray[SEMS][STUDENTS][TESTS];
ThreeDimArray gradeBook;
gradeBook[4][2][3] is the score of 4th
semesterforstudent 2 on test 3
// numberof semesters, students and tests all counted from zero!!
-
8/3/2019 Lec-5 Arrays ADT
15/18
Arrays of Arrays
double scoresTable[30][4];
Declares scoresTable to be a one-dimensional array containing
30 elements,each ofwhich is a one-dimensional array of 4 real numbers; that is,
scoresTable is a one-dimensional array ofrows ,each ofwhichhas 4real values. We could declare it as
typedef double RowOfTable[4];
RowOfTable scoresTable[30];
...
[0][1][2][3]
[29]
[0] [[1] [2] [3]
.
..
[0] [[1] [2] [3][0]
[1]
[2]
[3]
[29]
-
8/3/2019 Lec-5 Arrays ADT
16/18
scoresTable[i] is the i-th row of the table
Address Translation:Address Translation:
The array-of-arrays structure of multidimensional arrays explains
address translation.
Suppose the base address ofscoresTable is 0x12348:
scoresTable[10] 0x12348 + 10*(sizeof RowOfTable)
In general, ann-dimensional array canbe viewed (recursively) as a
one-dimensional array whose elements are (n - 1)-dimensional arrays.
In any case:
scoresTable[i][j] should be thought of as (scoresTable[i])[j]that is, as finding the j-thelement ofscoresTable[i].
p
scoresTable[10][3]p base(scoresTable[10]) + 3*(sizeof double)
scoresTable[10][4]
...
...
[3]
[0][1]
[9]
[10]
...
= 0x12348 + 10 * (4 * 8) + 3 * 8
= 0x124a0
= 0x12348 + 10 * (4 * 8)
-
8/3/2019 Lec-5 Arrays ADT
17/18
Implementing Multidimensional ArraysMore complicated than one dimensional arrays.
Memory is organized as a sequence of memory locations, and is thus 1D
How to use a 1D structure to store a MD structure?
A B C D
E F G H
I J K L
A characterrequires a single byte
Compilerinstructed to reserve 12 consecutive bytes
Two ways to store consecutively i.e.rowwise and columnwise.
-
8/3/2019 Lec-5 Arrays ADT
18/18
Implementing Multidimensional Arrays
A B C D
E F G HI J K L
RowWise
A
B
C
D
E
F
G
HI
J
K
L
ColumnWise
A
E
I
B
F
J
C
GK
D
H
L
A B C D
E F G H
I J K L
A B C D
E F G H
I J K L