DataStructure - Arrays

Embed Size (px)

Citation preview

  • 8/12/2019 DataStructure - Arrays

    1/38

    Data Structures & Algorithms

    Lecture # 2

    BS CS&IT Evening Program, FJ

    Campus

  • 8/12/2019 DataStructure - Arrays

    2/38

    Today Topics

    What are Arrays?

    Arrays Data Structures

    One Dimensional Array

    Operations on Arrays Inserting Deleting

    Traversing Searching Reversing

  • 8/12/2019 DataStructure - Arrays

    3/38

    What are Arrays?

    Array can be defined as:

    a group of data items of the same type

    under one name orarray is a collection ofelements or items or components that havethe same data type.

    For example array of 10 integers, arrays of

    names, etcetera. It can also be defined asa list of two or more variables with thesame name.

  • 8/12/2019 DataStructure - Arrays

    4/38

    Why we use Array?

    For example you want to find smallest numbersamongst four integer items and suppose that you donot know how to use array or you do not desire to useit. You may tackle the problem in the followingmanner:

    Define four variables to hold four integers data items,i.e.,

    int a=10, b=20, c=30; d=5;

    After declaring these variables, you would be requiredto write a quite few statements to find the smallestnumber in them. Since there are four variables,therefore, you would need to use four compound ifstatements for the solution, such as:

  • 8/12/2019 DataStructure - Arrays

    5/38

    Why we use Array?

    if (a

  • 8/12/2019 DataStructure - Arrays

    6/38

    Why we use Array?

    Assuming if you would have to find smallest numberamongst 100 data items, off course you would berequired to write 100 compound ifstatements andeach compound condition made of 99 simpleconditions. Further, it would not be possible to do

    other application such as sorting data, finding anyparticular number in large amount of data with theuse simple data structures. Therefore, the use ofarrays in programming languages is unavoidable.Processing on the data such as finding any number,calculating mean, average and sorting data are some

    of the fascinating applications which can beimplemented very easily with the help of array.

  • 8/12/2019 DataStructure - Arrays

    7/38

    One Dimensional Array

    One dimensional array is most common form of the array used byprogrammers to solve different kinds of problems.

    The general format of the one-dimensional array is as follows:data_type array_name[size];

    wheredata_type: is any valid C++ data type (int, float, etc.)

    array_name: is user defined identifier. User_definedname is followed by the square

    brackets which makes difference between ordinary variable

    and array type variable.

    Size: shows the actual length of the array, i.e., number oflocations or no data items.

  • 8/12/2019 DataStructure - Arrays

    8/38

    Examples of One Dimensional Array

    int a[10]; a is an integer array of10 elements

    float f[10] f is a float array of 10elements

    double d[10] d is a double arrayof 10 elements.

  • 8/12/2019 DataStructure - Arrays

    9/38

    Examples of One Dimensional Array

    Suppose you want to store identity-numbers of 20students, the following statement would be used todeclare a one dimensional integer array to hold data;

    int student_id_no[20];

    where int is type of the array, andstudent_id_no is name of the array (user defined).

    Computer will allocate 20 locations forstudent_id_no.

    C++ starts array locations from zero, hence, firstlocation is indicated as zero

  • 8/12/2019 DataStructure - Arrays

    10/38

    Diagrammatically Representation ofArray

    0 1 2 3 4 5 6 7 8 9

    Index

    Number

    101 103 105 107 109 111 113 115 117 119

    MemoryAddress

    LB UB

  • 8/12/2019 DataStructure - Arrays

    11/38

    One Dimensional Array

    Maximum number of elements in a One-DimensionalArray is computed as under:

    Maximum number of elements = UB LB + 1UB Upper Bound LB Lower BoundIn pervious diagram UB = 9 and LB = 0So, Maximum number of elements = 9 0 +1 = 10

    Each element of the array also has a unique memoryaddress

    The starting address of array is called Base Address.

    Elements of an array occupy a continuous block ofmemory

  • 8/12/2019 DataStructure - Arrays

    12/38

    One Dimensional Array

    The memory address of an element with index k iscomputed as:

    L(X(k)) = L0+ C * (k 1)

    L0is a Base Address, C memory size of each element in byte

    Suppose that Base address is 101 and size of element is 2bytes, what will be the memory address for index 5(Previous diagram)

    L0= 101, C = 2, k = 5

    L(X(k)) = L0+ C * (k 1)L(X(5)) = 101 + 2 * (5 1)= 101 + 2 * 4 = 101 + 10 = 111

  • 8/12/2019 DataStructure - Arrays

    13/38

    Operations on 1-D Array

    Traversal: Processing each element in the array

    Search: Finding the location of an element with a givenvalue

    Insertion: Adding a new element to an array

    Deletion: Removing an element from an array

    Sorting: Organizing the elements in some order

    Merging: Combining two arrays into a single array

    Reversing: Reversing the elements of an array

  • 8/12/2019 DataStructure - Arrays

    14/38

    Algorithms of Operations on 1-DArray

    Traverse_Array

    Algorithm for print the each element ofarray

    1. Start

    2. Repeat step-3 For i=1 to Max by 1

    3. Print arr[i]

    4. End

  • 8/12/2019 DataStructure - Arrays

    15/38

    Algorithms of Operations on 1-DArray

    Insert_Item(pos, num)

    Algorithm for inserting an element atgiven position of the array

    1. Start

    2. Repeat step-3 For i=Max to Pos+1 by -1

    3. arr[i] = arr[i-1]

    4. Arr[Pos] = num5. End

  • 8/12/2019 DataStructure - Arrays

    16/38

    Algorithms of Operations on 1-DArray

    Delete_Item(pos)

    Algorithm for delete an element from thegiven position of the array

    1. Start

    2. Repeat step-3 For i=Pos to Max-1 by 1

    3. arr[i] = arr[i+1]

    4. Arr[Max] = 05. End

  • 8/12/2019 DataStructure - Arrays

    17/38

    Algorithms of Operations on 1-DArray

    Search_Item(num)

    Algorithm for search Pos of the given element in thearray

    1. Start

    2. Repeat step 3 For i=1 to Max by 1

    3. If arr[i] = num then

    Print I

    Return

    End If4. Print Element Not Found!

    5. End

  • 8/12/2019 DataStructure - Arrays

    18/38

    Algorithms of Operations on 1-DArray

    Reverse_Array

    Algorithm for reverse the entire array

    1. Start

    2. Repeat step 3 to 5 For i=1 to Max/2 by 1

    3. temp = arr[i]

    4. arr[i] = arr[Max-i+1]

    5. arr[Max-i+1] = temp6. End

  • 8/12/2019 DataStructure - Arrays

    19/38

    Two Dimensional Array

    A two-dimensional array is a collection of elements placedin m rows and n columns.

    The syntax used to declare a 2-D array includes twosubscripts, of which one specifies the number of rows and

    the other specifies the number of columns of an array.data_type array_name[rows][columns];where

    data_type: is any valid C++ data type (int, float, etc.)

    array_name: is user defined identifier. User defined

    name is followed by the square

    Rows: Specifies the number of rows of an arrayColumn: Specifies the number of columns of an array

  • 8/12/2019 DataStructure - Arrays

    20/38

    Examples of One Dimensional Array

    int a[3][4];a is an integer arraycontaining 3 rows and 4 columns

    float f[3][4]; f is a float arraycontaining 3 rows and 4 columns

    double d[3][4]; d is a double arraycontaining 3 rows and 4 columns

  • 8/12/2019 DataStructure - Arrays

    21/38

    Diagrammatically Representation ofArray

    1 2 3 4

    1 12 1 -9 23

    2 14 7 11 121

    3 6 78 15 34

    Rows

    Columns

  • 8/12/2019 DataStructure - Arrays

    22/38

    Two Dimensional Array

    Maximum number of elements in a Two-DimensionalArray is computed as under:

    Maximum number of elements = M x NM Number of rows N Number of columnsIn pervious diagram M = 3 and N = 4So, Maximum number of elements =3 x 4 = 12

    Each element of the array also has a unique memoryaddress

    The starting address of array is called Base Address.

    Elements of an array occupy a continuous block ofmemory

  • 8/12/2019 DataStructure - Arrays

    23/38

    Two Dimensional arrays are represented in memory intwo ways. Row-major order

    Representation of Two-DimensionalArrays in memory

    12 1 -9 23 14 7 11 121 6 78 15 34

    502 504 506 508 510 512 514 516 518 520 522 524MemoryAddress

    Values

    1stRow 2ndRow 3rdRow

    12 14 6 1 7 78 -9 11 15 23 121 34

    502 504 506 508 510 512 514 516 518 520 522 524MemoryAddress

    Values

    1stCol

    Column-major order

    Note: Each integer occupies two bytes.

    2ndCol 3rdCol 4thCol

  • 8/12/2019 DataStructure - Arrays

    24/38

    Row-Major Order

    The memory address of an element with index i (row) andj (Col) is computed as:

    L(X[i][j]) = L0+ [(i 1) * N + (j 1)] * d

    L0is a Base Address, d memory size of each element in byte

    i is a row, j is a col and N is number of columns of an array

    Suppose that Base address is 502 and size of element is 2bytes, what will be the memory address for position x[2][2](Previous diagram)

    L0= 502, d = 2, i = 2, j = 2, N = 4

    L(X[i][j]) = L0+ [(i 1) * N + (j 1)] * dL(X[2][2]) = 502 + [(2 1) * 4 + (2 1)] * 2

    = 502 + [4 + 1] * 2 = 502 + 5 * 2 = 502 + 10 = 512

  • 8/12/2019 DataStructure - Arrays

    25/38

    Col-Major Order

    The memory address of an element with index i (row) andj (Col) is computed as:

    L(X[i][j]) = L0+ [(i 1) + (j 1) * M] * d

    L0is a Base Address, d memory size of each element in byte

    i is a row, j is a col and M is number of rows of an array

    Suppose that Base address is 502 and size of element is 2bytes, what will be the memory address for position x[2][2](Previous diagram)

    L0= 502, d = 2, i = 2, j = 2, M = 3

    L(X[i][j]) = L0+ [(i 1) + (j 1) * M] * dL(X[2][2]) = 502 + [(2 1) + (2 1) * 3] * 2

    = 502 + [1 + 3] * 2 = 502 + 4 * 2 = 502 + 8 = 510

  • 8/12/2019 DataStructure - Arrays

    26/38

    Operations on 2-D Array

    Traversal: Processing each element in the array

    Search: Finding the location of an element with a givenvalue

    Insertion: Adding a new element to an array

    Deletion: Removing an element from an array

    Sorting: Organizing the elements in some order

    Merging: Combining two arrays into a single array

    Reversing: Reversing the elements of an array

  • 8/12/2019 DataStructure - Arrays

    27/38

    Algorithms of Operations on 2-DArray

    Traverse_Array

    Algorithm for print the each element ofarray

    1. Start

    2. Repeat step-3 For i=1 to rMax by 1

    3. Repeat step-4 For j=1 to cMax by 1

    4. Print arr[i][j]5. End

  • 8/12/2019 DataStructure - Arrays

    28/38

    Algorithms of Operations on 2-DArray

    Insert_Item_Row_Major(r, c, num)Algorithm for inserting an element at given position of thearray

    1. Start2. Repeat step-3 For i=rMax to r by -13. Repeat step-4 For j=cMax to 1 by -14. if i > r or j > c then

    if j = 1 thenarr[i][j] = arr[i-1][cMax]

    elsearr[i][j] = arr[i][j-1]

    end if

    end if5. Arr[r][c] = num6. End

  • 8/12/2019 DataStructure - Arrays

    29/38

    Algorithms of Operations on 2-DArray

    Delete_Item_Row_Major(r, c)Algorithm for delete an element from the given position of thearray

    1. Start2. Repeat step-3 For i=r to rMax by 13. Repeat step-4 For j=1 to cMax by 14. if i r or j >= c then

    if j = cMax thenarr[i][j] = arr[i+1][1]

    elsearr[i][j] = arr[i][j+1]

    end if

    end if5. Arr[rMax][cMax] = 06. End

  • 8/12/2019 DataStructure - Arrays

    30/38

  • 8/12/2019 DataStructure - Arrays

    31/38

    Common Matrix Operations

    Traverse

    Addition

    Multiplication

    Transposition

    Evaluation of the determinant of asquare matrix

  • 8/12/2019 DataStructure - Arrays

    32/38

    Algorithms for Matrix Addition

    Matrix_Addition(m1, m2)

    Algorithm for add two matrices

    1. Start

    2. Repeat step 3 For i=1 to rMax by 1

    3. Repeat step 4 For j=1 to cMax by 1

    4. Mat[i][j] = m1[i][j] + m2[i][j]

    5. End

  • 8/12/2019 DataStructure - Arrays

    33/38

    Algorithms for Matrix Multiplication

    Matrix_Multiplication(m1, m2)

    Algorithm for multiply two matrices

    1. Start

    2. Repeat step 3 For i=1 to rMax by 1

    3. Repeat step 4 For j=1 to cMax by 1

    4. Repeat step 5 For k=1 to rMax

    5. Mat[i][j] += m1[i][k] * m2[k][j]6. End

  • 8/12/2019 DataStructure - Arrays

    34/38

    Algorithms for Matrix Transposition

    Matrix_Transpose(m)

    Algorithm for transpose the matrix

    1. Start

    2. Repeat step 3 For i=1 to rMax by 1

    3. Repeat step 4 For j=1 to cMax by 1

    4. Mat[i][j] = m[j][i]

    5. End

  • 8/12/2019 DataStructure - Arrays

    35/38

    Multi-Dimensional Array

    A three-dimensional array can be thought of as an array ofarrays of arrays.

    Following figure shows a 3-D array, which is a collection of

    three 2-D arrays each containing 4 rows and 2 columnsint arr[3][4][2];

    3 9

    1 8

    6 5

    4 0

    2 8

    0 6

    4 7

    1 5

    3 2

    8 6

    1 6

    4 5

    1st2-D Array 2nd2-D Array 3rd2-D Array

  • 8/12/2019 DataStructure - Arrays

    36/38

    Arrays and Polynomials

    Polynomials like5X4+ 2X3+ 7X2+ 10X 8

    Arithmetic operations like addition andmultiplication of polynomials are common

    Need a way to represent the polynomial in anarray

    Each element of the array should consist oftwo values, namely coefficient and exponent

  • 8/12/2019 DataStructure - Arrays

    37/38

    Arrays and Polynomials

    To create a struct (User Defined Data Type) forPolynomials

    struct Poly{

    int coeff;

    int exp;};

    Poly P[Max];

    P[0].coeff = c;

    P[0].exp = e;

  • 8/12/2019 DataStructure - Arrays

    38/38

    Next Lecture

    What are Stacks?

    Representation of Stacks

    Operations on Stacks

    Push

    Pop

    Evaluation of Expressions

    Infix to Postfix Conversion