View
225
Download
0
Category
Preview:
Citation preview
8/9/2019 L02 - Arrays
1/18
Basic Building BlocksArray
Structure
Classes
Arrays, structures, and classes are collection of data items with thefollowing differences:
Arrays Structures/Classes
Elements are of the same type. Elements may be of different type.
Elements are accessed through an
index
Elements are accessed through
name.
Arrays, structures, and classes are stored using consecutive memory
locations for the whole array or structure/class.
8/9/2019 L02 - Arrays
2/18
Ordered Lists
One of the most common data object is an orderedlist.
An ordered list has elements with definite ordering.The simplest example of an ordered list is an ordered
pair which has only two elements. Examples of ordered lists:
1. Months of the years (Jan, Feb, Mar, Apr, May,.,Dec)
2. English Alphabets (A, B, C, , Z)3. Words in a sentence (This is a book on data
structures.)
4. Names of the students in a class stored in the order
of their roll numbers.
8/9/2019 L02 - Arrays
3/18
Operations Defined on an Ordered List
or Vector
1. Find the length of list.
2. Check if the list is empty.
3. Traverse the list in some order.
4. Get the ith elements in the list.
5. Change the value of the ith element.
6. Insert a new element at the ith position.7. Delete the ith element.
8/9/2019 L02 - Arrays
4/18
Representation of an Ordered List
The easiest way to represent an orderedlist is by an one-dimensional array where
we store the ith element of the list in thearray element with index i.
This is called sequential or linear
mapping mapping a one-dimensionalvector onto a one-dimensional space.
8/9/2019 L02 - Arrays
5/18
class LinearList {public:
LinearList(int s); // constructor~ LinearList () {delete [ ] ListArray; } // destructorbool add (int value);
bool remove (int index);bool change(int index, int value);bool get(int index, int & value);void printAll();
bool isFull() {return MaxSize == size;}
bool isEmpty() {return size == 0; }int length() {return size;}private:
int MaxSize; // max List sizeint *ListArray;int size; // no. of elements in the List
};
Linear List Using Arrays
8/9/2019 L02 - Arrays
6/18
LinearList :: LinearList(int s){
MaxSize = s;ListArray = new int[MaxSize];size = 0;
}
void LinearList :: printALL(){
for (i = 0; i < size; i++)cout
8/9/2019 L02 - Arrays
7/18
bool LinearList :: change(int index, int value) {if (index < size && index >= 0) {
ListArray[index] = value;return true;
}else
return false;
}
bool LinearList :: get(int index, int & value) {if (index < size && index >= 0) {
value = ListArray[index];return true;}else
return false;
}
8/9/2019 L02 - Arrays
8/18
bool LinearList::add(int value){
if (! isFull() ) {for (int i = size; i >= 1; i--) {
if (ListArray[i-1] > value)ListArray[i] = ListArray[i -1];
elsebreak;
}ListArray[i] = value;size++;
return true;}else return false;
}
8/9/2019 L02 - Arrays
9/18
bool LinearList::remove(int index){
if (index < size && index >= 0) {for (int i = index; i < size - 1; i++)
ListArray[i] = ListArray[i +1];size--;
return true;}else return false;
}
8/9/2019 L02 - Arrays
10/18
Representation of SingleDimensional Arrays
Whole array is stored in a single contiguous memoryblock.
Address of the first element is called the base addressand it is also the start address of array.
The address of the ith element is given by thefollowing formula:
Addressi = base_address + i * size_of_element
Arrays are very efficient way of organizing data sinceaccessing array elements requires O(1).
elem0 elem1 elem2 elemi elemn-1
8/9/2019 L02 - Arrays
11/18
float sum (float list [], int n){
float temp = 0;
int i;for (i = 0; i < n; i++)
temp = temp + list[i];
return temp;
}
8/9/2019 L02 - Arrays
12/18
void add (int a[][], int b[][],
int c[][],int rows, int cols)
{
int i, j;for (i=0; i
8/9/2019 L02 - Arrays
13/18
const int N = 100;
void add (int a[][N], int b[][N],
int c[][N],int rows, int cols)
{
int i, j;for (i=0; i
8/9/2019 L02 - Arrays
14/18
Two Dimensional Array
543451060
99827622
643823
6351
Users view (abstraction)
543451060998276226438236351
Systems view
(implementation)
Offset of a[i][j]?
8/9/2019 L02 - Arrays
15/18
Two Dimensional Array
543451060
99827622
643823
6351
Users view (abstraction)
Offset of a[i][j]?Number of elements in each row?
ith
rowtotal elements in the previous rows?
jth element in ith row
Offset (in units)
N
i * N
i * N + j
8/9/2019 L02 - Arrays
16/18
Row major C, Pascal, C++, etc.
Column Major FORTRAN
Ada - indefinite
Two Dimensional Array
8/9/2019 L02 - Arrays
17/18
Row Major
Slice along the 1st dimension to get an
array of N-1 dimensions
Continue until you are left with onedimension only.
Offset of a[d0] [d1] [dn-1]
Multi-Dimensional Arrays
A[D0] [D1][Dn-1]
(((d0*D1 + d1)*D2 + d2)*D3 + ) + dn-1
8/9/2019 L02 - Arrays
18/18
Symmetric Matrices
Recommended