array ADT

Views:
 
Category: Education
     
 

Presentation Description

No description available.

Comments

Presentation Transcript

Outline : 

Outline Introduction to Abstract Data Types (ADTs) Arrays

Array ADT : 

Array ADT Collection of data elements A fixed-size sequence of elements, all of the same type Basic Operations Direct access to each element in the array by specifying its position so that values can be retrieved from or stored in that position

Different types of Array : 

Different types of Array One-dimensional array: only one index is used. Two dimensional array: Two indexes are used. Multi-dimensional array: array involving more than one index Static array: the compiler determines how memory will be allocated for the array Dynamic array: memory allocation takes place during execution

One-Dimensional Static Array : 

One-Dimensional Static Array Syntax: ElementType arrayName [CAPACITY]; ElementType arrayName [CAPACITY] = { initializer_list }; Example: int b [10]; int b [10]={1,2,3,4,5,6,7,8,9,10};

Array and ADT : 

Array and ADT Collection of data elements A fixed-size sequence of elements, all of the same type Basic Operations Direct access to each element in the array by specifying its position so that values can be retrieved from or stored in that position An Array as an ADT C++ Array Fixed size ----------------------specify the capacity of the array Ordered-------------------------indices are numbered 0,1,2,…,capacity-1 Same type of elements-------specify the element type Direct access-------------------subscript operator[]

Example of array : 

Example of array Consider array a,b,c,d to store collection of 10 integers declared by: int capacity=10 int a[capacity], b[capacity]={1,2,3,4,5,6,7,8,9,10}, c[capacity]={1,2,3}, d[capacity]={0}; char name[capacity]=“John Doe”;

Two Dimensional Arrays: : 

Two Dimensional Arrays: The two-dimensional array consists of rows and columns. It is also called table or matrix. The elements of two dimensional array are referenced by two subscripts or index values. A matrix with the same rows and columns is called square matrix.

Slide 8: 

in C++ a two dimensional array `temp’ of integer data type having 3 rows and 4 columns is declared as: int temp[2][3]. Total number of elements in two dimensional array m rows and n columns is m x n elements as in above example 3 rows and 4 columns array having 2x 3=6 elements.

Slide 9: 

as Representation of Two dimensional Array in memory Two dimensional arrays are represented in memory in two ways. Row major order Column major order

Slide 10: 

Row major Order: In row major order, a two dimensional array is represented in memory by row order. - Ex for 2 rows and three columns is as: {x[1,1],x[1,2],x[1,3]},{x[2,1],x[2,2],x[2,3]} Column major Order: In column major , a two dimensional array is represented in memory by column order. - Ex for 2 rows and three columns is as: {x[1,1],x[2,1]},{x[1,2],x[2,2],x[1,3],x[2,3]}

Multidimensional Arrays : 

Multidimensional Arrays Consider a table of test scores for several different students

Array Output Function : 

Array Output Function Void display(int array[], int num_values) { for (int I = 0; i<num_values; i++) cout<< array[i] << “ ”; }

Multidimensional Arrays : 

Multidimensional Arrays Syntax: ElementType arrayName [num_rows][num_columns]; int scoretable [num_students][num_tests]; int scoretable[2][5]={{80,80,80,80,80},{60,60,60,60,60}}; If you want to change the score of first student’s 3rd test score to 100, you just need to do: Scoretable[0][2]=100

Multidimensional Arrays : 

Multidimensional Arrays Consider multiple pages of the student grade booktypedef double ThreeDimArray[NUM_ROWS][NUM_COLS][NUM_RANKS]; . . .