SlideShare uma empresa Scribd logo
1 de 50
ARRAYS IN
DATASTRUCTURES USING ‘C’


              Dr. C. Saritha
          Lecturer in Electronics
        SSBN Degree & PG College
             ANANTAPUR
Overview
 What is Array?
 Types of Arrays.
 Array operations.
 Merging of arrays.
 Arrays of pointers.
 Arrays and Polynomials.
ARRAY
 An  array is a linear data structure. Which
  is a finite collection of similar data items
  stored in successive or consecutive
  memory locations.
 For example an array may contains all
  integer or character elements, but not
  both.
 Each  array can be accessed by using array
  index and it is must be positive integer value
  enclosed in square braces.
 This is starts from the numerical value 0 and
   ends at 1 less than of the array index value.
 For example an array[n] containing n
  number of elements are denoted by
  array[0],array[1],…..array[n-1]. where ‘0’ is
  called lower bound and the ‘n-1’ is called
  higher bound of the array.
Types of Arrays
   Array can be categorized into different
    types. They are

              One dimensional array
              Two dimensional array
              Multi dimensional array
One dimensional array:-
  One   dimensional array is also called as
   linear array. It is also represents 1-D
   array.
  the one dimensional array stores the data
   elements in a single row or column.
  The syntax to declare a linear array is as
   fallows
         Syntax: <data type> <array name>
   [size];
 Syntax   for the initialization of the linear array
  is as fallows
 Syntax:
     <data type><array name>[size]={values};
 Example:


       int arr[6]={2,4,6,7,5,8};

                                           Values

                                    array name
Memory representation of the one
dimensional array:-
      a[0] a[1] a[2] a[3] a[4] a[5]

          2     4     6     7     5     8

       100 102       104 106 108 110
 The memory blocks a[0],a[1],a[2],a[3 ],a[4] ,
  a[5] with base addresses 1,102,104,106,108,
  110 store the values 2,4,6,7,5,8 respectively.
 Here  need not to keep the track of the
  address of the data elements of an array to
   perform any operation on data element.
 We can track the memory location of any
  element of the linear array by using the
  base address of the array.
 To calculate the memory location of an
  element in an array by using formulae.
     Loc (a[k])=base address +w(k-lower
  bound)
 Here   k specifies the element whose
  location to find.
 W means word length.
 Ex: We can find the location of the
  element 5, present at a[3],base address is
  100, then
          loc(a[3])=100+2(3-0)
                   =100+6
                   =106.
Two dimensional array:-
 A two dimensional array is a collection of
  elements placed in rows and columns.
 The syntax used to declare two
  dimensional     array     includes   two
  subscripts, of which one specifies the
  number of rows and the other specifies
  the number of columns.
 These two subscripts are used to
  reference an element in an array.
 Syntax    to declare the two dimensional
  array is as fallows
 Syntax:
      <data type> <array name> [row size]
  [column size];

 Syntax   to initialize the two dimensional
  array is as fallows
 Syntax:
       <data type> <array name> [row size]
  [column size]={values};
 Example:
  int num[3][2]={4,3,5,6,,8,9};
           or
  int num[3][2]={{4,3},{5,6},{8,9}};

                              values
                              column size
                              row size
                              array name
                              data type
Representation of the 2-D
array:-
 Rows            columns
            0th column 1st column

  0th row   a[0][0]    a[0][1]
            a[1][0]    a[1][1]
  1st row   a[2][0]    a[2][1]
  2nd row
Memory representation of 2-D
 array:-
 Memory    representation of a 2-D array is
 different from the linear array.
 in 2-D array possible two types of memory
 arrangements. They are




              Row major arrangement
 Row major arrangement:

  0th row     1st row      2nd row
   4      3    5      6     8      9
   502 504 506 508 510 512
 Column major arrangement:

      0th column      1st column
      4      5   8   3      6    9
    502 504 506 508 510 512
 We   can access any element of the array
  once we know the base address of the array
  and number of row and columns present in
  the array.
 In general for an array a[m][n] the address
  of element a[i][j] would be,
 In row major arrangement

           Base address+2(i*n+j)
 In column major arrangement

            Base adress+2(j*m+i)
 Ex:
   we can find the location of the element 8
  then an array a[3][2] , the address of
  element would be a[2][0] would be
 In row major arrangement
        loc(a[2][0])=502+2(2*2+0)
                   =502+8
                   =510
 In column major arrangement
           loc(a[2][0])=502+2(0*3+2)
                      =502+4
                      =506
Multi dimensional arrays:-
 An  array haves 2 or more subscripts, that
  type of array is called multi dimensional
  array.
 The     3 –D array is called as
  multidimensional array this can be thought
  of as an array of two dimensional arrays.
 Each element of a 3-D array is accessed
  using subscripts, one for each dimension.
 Syntax for the declaration and
  initialization as fallows Syntax
 <data type><array name>[s1][s2][s3]
                             ={values};

 Ex:
 int a[2][3][2]={
               { {2,1},{3,6},{5,3} },
                { {0,9},{2,3},{5,8} }
                 };
Memory representation of 3-D
array:-
 In multi dimensional arrays   permits only
 a row major arrangement.

     0th 2-D array    1st 2-D array
 2 1 3 6 5 3 0 9 2 3 5 8
 10 12 14 16 18 20 22 24 26 28 30 32
 For  any 3-D array a [x][y][z], the element
  a[i][j][k] can be accessed as
          Base address+2(i*y*z +j*z+ k)
 Array a can be defined as int a [2][3][2] ,
  element 9 is present at a[1][0][1]
 Hence address of 9 can be obtained as
          =10+2(1*3*2+0*2+1)
          =10+14
          =24
ARRAY OPERATIONS
 There are several operations that can be
 performed on an array. They are
          Insertion
          Deletion
          Traversal
          Reversing
          Sorting
          Searching
Insertion:
 Insertion is nothing but adding a new
  element to an array.
 Here through a loop, we have shifted the
  numbers, from the specified position, one
  place to the right of their existing
  position.
 Then we have placed the new number at
  the vacant place.
 Ex:


        for (i=4;i>= 2;i++)
          {
             a[i]=a[i-1];
             }
           a[i]=num;
 Before insertion :

        11   13   14       4        0

      0     1     2        3        4
 After insertion:

        11   12       13       14       4

        0     1      2   3      4
 Fig: shifting the elements to the right while
  Insuring an element at 2nd position
Deletion:
 Deletion   is nothing but process of remove
  an element from the array.
 Here we have shifted the numbers of
         placed after the position from where
  the number is to be deleted, one place to
  the left of their existing positions.
 The place that is vacant after deletion of
  an element is filled with ‘0’.
 Ex:


        for (i=3;i<5;i++)
        {
           a[i-1]=a[i];
            }
          a[i-1]=0;
 Before deletion:


              11   12     13   14   4
            0     1       2    3    4
 After deletion:


              11     13   14   4    0
             0     1     2     3    4
 Fig: shifting the elements to the left while
  deleting 3rd element in an array.
Traversal:
 Traversal is nothing but display the
  elements in the array.
 Ex:
      for (i=0;i<5;i++)
      {
          Printf (“%dt”, a[i]);
         }

          11    12    14     4     0
Reversing:
 This is the process of reversing the elements
  in the array by swapping the elements.
 Here swapping should be done only half
  times of the array size.
 Ex:
   for (i=0;i<5/2;i++)
      {
       int temp=a[i];
       a[i]=a[5-1-1];
        a[5-1-i]=temp;
         }
 Before swapping:




             11   12   13   14    0
             0 1       2    3     4
 After swapping:


            0     14   13   12   11
            0     1 2      3     4
Fig: swapping of elements while reversing an
  array.
Sorting:
 Sorting
        means arranging a set of data
 in some order like ascending or
 descending order.
 Ex:    for (i=0;i<5;i++)
        {
          for (j=i+1;j<5;j++)
          {
            if (a[i]>a[j])
             {
               temp=a[i];
               a[i]=a[j];
               a[j]=temp;
                 }}}
 Before sorting:


           17   25    13    2      1
          0      1     2    3     4
 After sorting:



       1        2    13    17    25
       0        1      2     3     4
Searching:
 Searching  is the process of finding the
  location of an element with a given
  element in a list..
 Here searching is starts from 0th element
  and continue the process until the given
  specified number is found or end of list is
  reached.
 Ex:
   for (i=0;i<5;i++)
   {
     if (a[i]==num)
     {
       Printf(“n element %d is present at %dth
  position”,num,i+1);
        return;
}}if (i==5)
Printf (“the element %d is not present in the
  array ”,num);
11 12 13 14   4   13



11 12 13 14 4     13


11 12 13 14   4   13
Merging of arrays
 Merging   means combining two sorted list
  into one sorted list.
 Merging of arrays involves two steps:

     They are
         sorting the arrays that are to be
         merged.
        Adding the sorted elements of both
         the arrays a to a new array in sorted
         order.
 Ex:
 Before merging:

        1st array            2nd array
        1       3   13       2     8     11

 After merging:


            1       2    3   8   11 13
Arrays of pointers
A   pointer variable always contains an
  address.
 An array of pointer would be nothing but
  a collection of addresses.
 The address present in an array of pointer
  can be address of isolated variables or
  even the address of other variables.
 An   array of pointers widely used for
  stoning several strings in the array.
 The rules that apply to an ordinary array
  also apply to an array of pointer as well.
 The elements of an array of pointer are
  stored in the memory just like the elements
  of any other kind of array.
 Memory representation of the array of
  integers and an array of pointers
  respectively.
 Fig1:Memory representation of an array of
 integers and integer variables I and j.
  a[0] a[1] a[2] a[3]        i     j
      3    4    5     6      1      9
    100 102 104 106 200 312
 Fig2:Memory representation of an array of
  pointers.
 b[0] b[1] b[2] b[3] b[4] b[5]
  100     102   104   106   200    312

 8112 8114 8116 8118 8120 8122
Arrays and polynomials

 Polynomials   like 5x4+2 x3+7x2+10x-8
  can be maintained using an array.
 To achieve each element of the array
  should have two values coefficient and
  exponent.
 While   maintaining the polynomial it is
  assumes that the exponent of each
  successive term is less than that of the
  previous term.
 Once we build an array to represent
  polynomial we can use such an array to
  perform common polynomial operations
  like addition and multiplication.
Addition of two polynomials:
 Here  if the exponents of the 2 terms
  beinf compared are equal then their
  coefficients are added and the result is
  stored in 3rd polynomial.
 If the exponents of the 2 terms are not
  equal then the term with the bigger
  exponent is added to the 3 rd
  polynomial.
 If the term with an exponent is present in
  only 1 of the 2 polynomials then that
  term is added as it is to the 3rd
  polynomial.
 Ex:
 1st polynomial is 2x6+3x5+5x2
 2nd polynomial is 1x6+5x2+1x+2
 Resultant polynomial is
         3x6+3x5+10x2+1x+2
Multiplication of 2 polynomials:
 Here   each term of the coefficient of the 2 nd
  polynomial is multiplied with each term of the
  coefficient of the 1st polynomial.
 Each term exponent of the 2 nd polynomial is
  added to the each tem of the 1st polynomial.
 Adding the all terms and this equations placed
  to the resultant polynomial.
 Ex:
 1st polynomial is

           1x4+2x3+2x2+2x
 2nd polynomial is

           2x3+3x2+4x
 Resultant polynomial is

           2x7+7x6+14x5+18x4+14x3+8x2
THE END

Mais conteúdo relacionado

Mais procurados

SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSGokul Hari
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm03446940736
 
Queue data structure
Queue data structureQueue data structure
Queue data structureanooppjoseph
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked ListNinad Mankar
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arraysNeeru Mittal
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayimtiazalijoono
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm KristinaBorooah
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary searchZia Ush Shamszaman
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structureMAHALAKSHMI P
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureBalwant Gorad
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data StructureJeanie Arnoco
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data StructureMeghaj Mallick
 

Mais procurados (20)

Sorting
SortingSorting
Sorting
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
 
Array in c
Array in cArray in c
Array in c
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Queues
QueuesQueues
Queues
 
Arrays
ArraysArrays
Arrays
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
stack & queue
stack & queuestack & queue
stack & queue
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
 
Binary Search
Binary SearchBinary Search
Binary Search
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 

Destaque

02 Arrays And Memory Mapping
02 Arrays And Memory Mapping02 Arrays And Memory Mapping
02 Arrays And Memory MappingQundeel
 
Phased array radar antennas - Anten mảng pha
Phased array radar antennas - Anten mảng phaPhased array radar antennas - Anten mảng pha
Phased array radar antennas - Anten mảng phaTuấn Trần
 
Antenna synthesis
Antenna synthesisAntenna synthesis
Antenna synthesisAJAL A J
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsAakash deep Singhal
 
Unit iv microcontrollers final
Unit iv microcontrollers finalUnit iv microcontrollers final
Unit iv microcontrollers finalSARITHA REDDY
 
Fundamentals of Array Antenna
Fundamentals of Array AntennaFundamentals of Array Antenna
Fundamentals of Array AntennaYong Heui Cho
 
Active Phased Array Radar Systems
Active Phased Array Radar SystemsActive Phased Array Radar Systems
Active Phased Array Radar SystemsReza Taryghat
 
Multi-Funtion Phased Array Radar
Multi-Funtion Phased Array RadarMulti-Funtion Phased Array Radar
Multi-Funtion Phased Array RadarMistral Solutions
 
Broadside array vs end fire array
Broadside array vs end fire arrayBroadside array vs end fire array
Broadside array vs end fire arrayAJAL A J
 
Representation of binary tree in memory
Representation of binary tree in memoryRepresentation of binary tree in memory
Representation of binary tree in memoryRohini Shinde
 
Arrays and addressing modes
Arrays and addressing modesArrays and addressing modes
Arrays and addressing modesBilal Amjad
 
Phased array antenna
Phased array antennaPhased array antenna
Phased array antennaShaveta Banda
 
Coaxial feed microstrip patch antenna using HFSS
 Coaxial feed microstrip patch antenna using  HFSS Coaxial feed microstrip patch antenna using  HFSS
Coaxial feed microstrip patch antenna using HFSSMithilesh Naphade
 
Microstrip patch-antenna
Microstrip patch-antennaMicrostrip patch-antenna
Microstrip patch-antennaaquibjamal123
 
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...widespreadpromotion
 

Destaque (20)

Arrays
ArraysArrays
Arrays
 
02 Arrays And Memory Mapping
02 Arrays And Memory Mapping02 Arrays And Memory Mapping
02 Arrays And Memory Mapping
 
Phased array radar antennas - Anten mảng pha
Phased array radar antennas - Anten mảng phaPhased array radar antennas - Anten mảng pha
Phased array radar antennas - Anten mảng pha
 
Antenna synthesis
Antenna synthesisAntenna synthesis
Antenna synthesis
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithms
 
Unit iv microcontrollers final
Unit iv microcontrollers finalUnit iv microcontrollers final
Unit iv microcontrollers final
 
Fundamentals of Array Antenna
Fundamentals of Array AntennaFundamentals of Array Antenna
Fundamentals of Array Antenna
 
Active Phased Array Radar Systems
Active Phased Array Radar SystemsActive Phased Array Radar Systems
Active Phased Array Radar Systems
 
Multi-Funtion Phased Array Radar
Multi-Funtion Phased Array RadarMulti-Funtion Phased Array Radar
Multi-Funtion Phased Array Radar
 
Broadside array vs end fire array
Broadside array vs end fire arrayBroadside array vs end fire array
Broadside array vs end fire array
 
Representation of binary tree in memory
Representation of binary tree in memoryRepresentation of binary tree in memory
Representation of binary tree in memory
 
Arrays and addressing modes
Arrays and addressing modesArrays and addressing modes
Arrays and addressing modes
 
Phased array antenna
Phased array antennaPhased array antenna
Phased array antenna
 
Arrays Basics
Arrays BasicsArrays Basics
Arrays Basics
 
array and phased array antennna
array and phased array antennnaarray and phased array antennna
array and phased array antennna
 
Coaxial feed microstrip patch antenna using HFSS
 Coaxial feed microstrip patch antenna using  HFSS Coaxial feed microstrip patch antenna using  HFSS
Coaxial feed microstrip patch antenna using HFSS
 
Array in C
Array in CArray in C
Array in C
 
Microstrip patch-antenna
Microstrip patch-antennaMicrostrip patch-antenna
Microstrip patch-antenna
 
Ppt
PptPpt
Ppt
 
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
 

Semelhante a Arrays (20)

Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
 
Lecture 15 - Array
Lecture 15 - ArrayLecture 15 - Array
Lecture 15 - Array
 
Data structure array
Data structure  arrayData structure  array
Data structure array
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
 
cluod.pdf
cluod.pdfcluod.pdf
cluod.pdf
 
Arrays in C.pptx
Arrays in C.pptxArrays in C.pptx
Arrays in C.pptx
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
 
Arrays.pptx
Arrays.pptxArrays.pptx
Arrays.pptx
 
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
 
ARRAYS
ARRAYSARRAYS
ARRAYS
 
Chapter 13.pptx
Chapter 13.pptxChapter 13.pptx
Chapter 13.pptx
 
Arrays
ArraysArrays
Arrays
 
Chapter 10.ppt
Chapter 10.pptChapter 10.ppt
Chapter 10.ppt
 
02 arrays
02 arrays02 arrays
02 arrays
 
unit1Intro_final.pptx
unit1Intro_final.pptxunit1Intro_final.pptx
unit1Intro_final.pptx
 
Arrays
ArraysArrays
Arrays
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
 
Arrays in CPP
Arrays in CPPArrays in CPP
Arrays in CPP
 
Algo>Arrays
Algo>ArraysAlgo>Arrays
Algo>Arrays
 

Mais de SARITHA REDDY

Introduction to microprocessors notes
Introduction to microprocessors notesIntroduction to microprocessors notes
Introduction to microprocessors notesSARITHA REDDY
 
8051 data types and directives
8051 data types and directives8051 data types and directives
8051 data types and directivesSARITHA REDDY
 
Unit ii microcontrollers final
Unit ii microcontrollers finalUnit ii microcontrollers final
Unit ii microcontrollers finalSARITHA REDDY
 
Introduction to microprocessor notes
Introduction to microprocessor notesIntroduction to microprocessor notes
Introduction to microprocessor notesSARITHA REDDY
 
8051 data type and directives
8051 data type and directives8051 data type and directives
8051 data type and directivesSARITHA REDDY
 
Decimation in time and frequency
Decimation in time and frequencyDecimation in time and frequency
Decimation in time and frequencySARITHA REDDY
 
Addressing modes of 8051
Addressing modes of 8051Addressing modes of 8051
Addressing modes of 8051SARITHA REDDY
 
I o ports and timers of 8051
I o ports and timers of 8051I o ports and timers of 8051
I o ports and timers of 8051SARITHA REDDY
 
Mos and cmos technology
Mos and cmos technologyMos and cmos technology
Mos and cmos technologySARITHA REDDY
 
Clampers and clippers
Clampers and clippersClampers and clippers
Clampers and clippersSARITHA REDDY
 
Electro Magnetic Wave Propagation
Electro Magnetic Wave PropagationElectro Magnetic Wave Propagation
Electro Magnetic Wave PropagationSARITHA REDDY
 
Satellite communications
Satellite communicationsSatellite communications
Satellite communicationsSARITHA REDDY
 
Electronics in daily life
Electronics in daily lifeElectronics in daily life
Electronics in daily lifeSARITHA REDDY
 
BSc I year practicals
BSc I year practicalsBSc I year practicals
BSc I year practicalsSARITHA REDDY
 
Combinational circuits
Combinational circuitsCombinational circuits
Combinational circuitsSARITHA REDDY
 
Applications of op amps
Applications of op ampsApplications of op amps
Applications of op ampsSARITHA REDDY
 
Digital logic gates and Boolean algebra
Digital logic gates and Boolean algebraDigital logic gates and Boolean algebra
Digital logic gates and Boolean algebraSARITHA REDDY
 

Mais de SARITHA REDDY (20)

Introduction to microprocessors notes
Introduction to microprocessors notesIntroduction to microprocessors notes
Introduction to microprocessors notes
 
8051 data types and directives
8051 data types and directives8051 data types and directives
8051 data types and directives
 
Unit ii microcontrollers final
Unit ii microcontrollers finalUnit ii microcontrollers final
Unit ii microcontrollers final
 
Introduction to microprocessor notes
Introduction to microprocessor notesIntroduction to microprocessor notes
Introduction to microprocessor notes
 
8051 data type and directives
8051 data type and directives8051 data type and directives
8051 data type and directives
 
Decimation in time and frequency
Decimation in time and frequencyDecimation in time and frequency
Decimation in time and frequency
 
RT linux
RT linuxRT linux
RT linux
 
Addressing modes of 8051
Addressing modes of 8051Addressing modes of 8051
Addressing modes of 8051
 
I o ports and timers of 8051
I o ports and timers of 8051I o ports and timers of 8051
I o ports and timers of 8051
 
Mos and cmos technology
Mos and cmos technologyMos and cmos technology
Mos and cmos technology
 
Logic families
Logic familiesLogic families
Logic families
 
Clampers and clippers
Clampers and clippersClampers and clippers
Clampers and clippers
 
Linked lists
Linked listsLinked lists
Linked lists
 
Electro Magnetic Wave Propagation
Electro Magnetic Wave PropagationElectro Magnetic Wave Propagation
Electro Magnetic Wave Propagation
 
Satellite communications
Satellite communicationsSatellite communications
Satellite communications
 
Electronics in daily life
Electronics in daily lifeElectronics in daily life
Electronics in daily life
 
BSc I year practicals
BSc I year practicalsBSc I year practicals
BSc I year practicals
 
Combinational circuits
Combinational circuitsCombinational circuits
Combinational circuits
 
Applications of op amps
Applications of op ampsApplications of op amps
Applications of op amps
 
Digital logic gates and Boolean algebra
Digital logic gates and Boolean algebraDigital logic gates and Boolean algebra
Digital logic gates and Boolean algebra
 

Arrays

  • 1. ARRAYS IN DATASTRUCTURES USING ‘C’ Dr. C. Saritha Lecturer in Electronics SSBN Degree & PG College ANANTAPUR
  • 2. Overview  What is Array?  Types of Arrays.  Array operations.  Merging of arrays.  Arrays of pointers.  Arrays and Polynomials.
  • 3. ARRAY  An array is a linear data structure. Which is a finite collection of similar data items stored in successive or consecutive memory locations.  For example an array may contains all integer or character elements, but not both.
  • 4.  Each array can be accessed by using array index and it is must be positive integer value enclosed in square braces.  This is starts from the numerical value 0 and ends at 1 less than of the array index value.  For example an array[n] containing n number of elements are denoted by array[0],array[1],…..array[n-1]. where ‘0’ is called lower bound and the ‘n-1’ is called higher bound of the array.
  • 5. Types of Arrays  Array can be categorized into different types. They are  One dimensional array  Two dimensional array  Multi dimensional array
  • 6. One dimensional array:-  One dimensional array is also called as linear array. It is also represents 1-D array.  the one dimensional array stores the data elements in a single row or column.  The syntax to declare a linear array is as fallows Syntax: <data type> <array name> [size];
  • 7.  Syntax for the initialization of the linear array is as fallows  Syntax: <data type><array name>[size]={values};  Example: int arr[6]={2,4,6,7,5,8}; Values array name
  • 8. Memory representation of the one dimensional array:- a[0] a[1] a[2] a[3] a[4] a[5] 2 4 6 7 5 8 100 102 104 106 108 110  The memory blocks a[0],a[1],a[2],a[3 ],a[4] , a[5] with base addresses 1,102,104,106,108, 110 store the values 2,4,6,7,5,8 respectively.
  • 9.  Here need not to keep the track of the address of the data elements of an array to perform any operation on data element.  We can track the memory location of any element of the linear array by using the base address of the array.  To calculate the memory location of an element in an array by using formulae. Loc (a[k])=base address +w(k-lower bound)
  • 10.  Here k specifies the element whose location to find.  W means word length.  Ex: We can find the location of the element 5, present at a[3],base address is 100, then loc(a[3])=100+2(3-0) =100+6 =106.
  • 11. Two dimensional array:-  A two dimensional array is a collection of elements placed in rows and columns.  The syntax used to declare two dimensional array includes two subscripts, of which one specifies the number of rows and the other specifies the number of columns.  These two subscripts are used to reference an element in an array.
  • 12.  Syntax to declare the two dimensional array is as fallows  Syntax: <data type> <array name> [row size] [column size];  Syntax to initialize the two dimensional array is as fallows  Syntax: <data type> <array name> [row size] [column size]={values};
  • 13.  Example: int num[3][2]={4,3,5,6,,8,9}; or int num[3][2]={{4,3},{5,6},{8,9}}; values column size row size array name data type
  • 14. Representation of the 2-D array:- Rows columns 0th column 1st column 0th row a[0][0] a[0][1] a[1][0] a[1][1] 1st row a[2][0] a[2][1] 2nd row
  • 15. Memory representation of 2-D array:-  Memory representation of a 2-D array is different from the linear array.  in 2-D array possible two types of memory arrangements. They are Row major arrangement
  • 16.  Row major arrangement: 0th row 1st row 2nd row 4 3 5 6 8 9 502 504 506 508 510 512  Column major arrangement: 0th column 1st column 4 5 8 3 6 9 502 504 506 508 510 512
  • 17.  We can access any element of the array once we know the base address of the array and number of row and columns present in the array.  In general for an array a[m][n] the address of element a[i][j] would be,  In row major arrangement Base address+2(i*n+j)  In column major arrangement Base adress+2(j*m+i)
  • 18.  Ex: we can find the location of the element 8 then an array a[3][2] , the address of element would be a[2][0] would be  In row major arrangement loc(a[2][0])=502+2(2*2+0) =502+8 =510  In column major arrangement loc(a[2][0])=502+2(0*3+2) =502+4 =506
  • 19. Multi dimensional arrays:-  An array haves 2 or more subscripts, that type of array is called multi dimensional array.  The 3 –D array is called as multidimensional array this can be thought of as an array of two dimensional arrays.  Each element of a 3-D array is accessed using subscripts, one for each dimension.
  • 20.  Syntax for the declaration and initialization as fallows Syntax  <data type><array name>[s1][s2][s3] ={values};  Ex: int a[2][3][2]={ { {2,1},{3,6},{5,3} }, { {0,9},{2,3},{5,8} } };
  • 21. Memory representation of 3-D array:-  In multi dimensional arrays permits only a row major arrangement. 0th 2-D array 1st 2-D array 2 1 3 6 5 3 0 9 2 3 5 8 10 12 14 16 18 20 22 24 26 28 30 32
  • 22.  For any 3-D array a [x][y][z], the element a[i][j][k] can be accessed as Base address+2(i*y*z +j*z+ k)  Array a can be defined as int a [2][3][2] , element 9 is present at a[1][0][1]  Hence address of 9 can be obtained as =10+2(1*3*2+0*2+1) =10+14 =24
  • 23. ARRAY OPERATIONS  There are several operations that can be performed on an array. They are  Insertion  Deletion  Traversal  Reversing  Sorting  Searching
  • 24. Insertion:  Insertion is nothing but adding a new element to an array.  Here through a loop, we have shifted the numbers, from the specified position, one place to the right of their existing position.  Then we have placed the new number at the vacant place.
  • 25.  Ex: for (i=4;i>= 2;i++) { a[i]=a[i-1]; } a[i]=num;
  • 26.  Before insertion : 11 13 14 4 0 0 1 2 3 4  After insertion: 11 12 13 14 4 0 1 2 3 4  Fig: shifting the elements to the right while Insuring an element at 2nd position
  • 27. Deletion:  Deletion is nothing but process of remove an element from the array.  Here we have shifted the numbers of placed after the position from where the number is to be deleted, one place to the left of their existing positions.  The place that is vacant after deletion of an element is filled with ‘0’.
  • 28.  Ex: for (i=3;i<5;i++) { a[i-1]=a[i]; } a[i-1]=0;
  • 29.  Before deletion: 11 12 13 14 4 0 1 2 3 4  After deletion: 11 13 14 4 0 0 1 2 3 4  Fig: shifting the elements to the left while deleting 3rd element in an array.
  • 30. Traversal:  Traversal is nothing but display the elements in the array.  Ex: for (i=0;i<5;i++) { Printf (“%dt”, a[i]); } 11 12 14 4 0
  • 31. Reversing:  This is the process of reversing the elements in the array by swapping the elements.  Here swapping should be done only half times of the array size.  Ex: for (i=0;i<5/2;i++) { int temp=a[i]; a[i]=a[5-1-1]; a[5-1-i]=temp; }
  • 32.  Before swapping: 11 12 13 14 0 0 1 2 3 4  After swapping: 0 14 13 12 11 0 1 2 3 4 Fig: swapping of elements while reversing an array.
  • 33. Sorting:  Sorting means arranging a set of data in some order like ascending or descending order.
  • 34.  Ex: for (i=0;i<5;i++) { for (j=i+1;j<5;j++) { if (a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; }}}
  • 35.  Before sorting: 17 25 13 2 1 0 1 2 3 4  After sorting: 1 2 13 17 25 0 1 2 3 4
  • 36. Searching:  Searching is the process of finding the location of an element with a given element in a list..  Here searching is starts from 0th element and continue the process until the given specified number is found or end of list is reached.
  • 37.  Ex: for (i=0;i<5;i++) { if (a[i]==num) { Printf(“n element %d is present at %dth position”,num,i+1); return; }}if (i==5) Printf (“the element %d is not present in the array ”,num);
  • 38. 11 12 13 14 4 13 11 12 13 14 4 13 11 12 13 14 4 13
  • 39. Merging of arrays  Merging means combining two sorted list into one sorted list.  Merging of arrays involves two steps: They are  sorting the arrays that are to be merged. Adding the sorted elements of both the arrays a to a new array in sorted order.
  • 40.  Ex:  Before merging: 1st array 2nd array 1 3 13 2 8 11  After merging: 1 2 3 8 11 13
  • 41. Arrays of pointers A pointer variable always contains an address.  An array of pointer would be nothing but a collection of addresses.  The address present in an array of pointer can be address of isolated variables or even the address of other variables.
  • 42.  An array of pointers widely used for stoning several strings in the array.  The rules that apply to an ordinary array also apply to an array of pointer as well.  The elements of an array of pointer are stored in the memory just like the elements of any other kind of array.  Memory representation of the array of integers and an array of pointers respectively.
  • 43.  Fig1:Memory representation of an array of integers and integer variables I and j. a[0] a[1] a[2] a[3] i j 3 4 5 6 1 9 100 102 104 106 200 312  Fig2:Memory representation of an array of pointers. b[0] b[1] b[2] b[3] b[4] b[5] 100 102 104 106 200 312 8112 8114 8116 8118 8120 8122
  • 44. Arrays and polynomials  Polynomials like 5x4+2 x3+7x2+10x-8 can be maintained using an array.  To achieve each element of the array should have two values coefficient and exponent.
  • 45.  While maintaining the polynomial it is assumes that the exponent of each successive term is less than that of the previous term.  Once we build an array to represent polynomial we can use such an array to perform common polynomial operations like addition and multiplication.
  • 46. Addition of two polynomials:  Here if the exponents of the 2 terms beinf compared are equal then their coefficients are added and the result is stored in 3rd polynomial.  If the exponents of the 2 terms are not equal then the term with the bigger exponent is added to the 3 rd polynomial.
  • 47.  If the term with an exponent is present in only 1 of the 2 polynomials then that term is added as it is to the 3rd polynomial.  Ex:  1st polynomial is 2x6+3x5+5x2  2nd polynomial is 1x6+5x2+1x+2  Resultant polynomial is 3x6+3x5+10x2+1x+2
  • 48. Multiplication of 2 polynomials:  Here each term of the coefficient of the 2 nd polynomial is multiplied with each term of the coefficient of the 1st polynomial.  Each term exponent of the 2 nd polynomial is added to the each tem of the 1st polynomial.  Adding the all terms and this equations placed to the resultant polynomial.
  • 49.  Ex:  1st polynomial is 1x4+2x3+2x2+2x  2nd polynomial is 2x3+3x2+4x  Resultant polynomial is 2x7+7x6+14x5+18x4+14x3+8x2