Definition of Array: - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Definition of Array:

Description:

Slide 1 ... unit iv – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 29
Provided by: Admin748
Category:

less

Transcript and Presenter's Notes

Title: Definition of Array:


1
  • UNIT IV

2
  • Definition of Array
  • An array is defined to be a group of logically
    related data items of similar type, stored in
    contiguous memory locations, sharing a common
    name but distinguished by subscript(s) values.
  • Depending on the number of subscripts used,
    array are classified into 1.one-dimensional
    arrays(1-d array)
  • 2.two-dimensional arrays(2-d arrays)
  • One-Dimensional Arrays
  • It termed as one-dimensional array or 1-d array.
    It is used to store a list of values, all of
    which share a common name distinguishable by
    subscript values.
  • Declaration of One-dimensional Arrays
  • Syntax
  • data-type variable-namesize
  • Where,
  • 1.Data-type refers to any data type supported by
    C.
  • 2.variable-name refers to array name should be
    valid C identifier.
  • 3.size-indicates number of data items of type
    data-type grouped together.

3
  • Example
  • int a5
  • Here, a is declared to be an array of int type
    and of size five. Five contiguous memory
    locations are shown below,
  • Each data item in the array a is identified by
    the array name a followed by a pair of square
    brackets enclosing a subscript value.The
    subscript value ranges from 0 to 4., a0 denotes
    first data item,a1 denotes second data item and
    a4 denotes the last data item.

2 a0 5 a1 6 a2 8 a3 9 a4
4
  • Initialization of One-Dimensional Arrays
  • We can initialize one-dimensional arrays
    also,i.e., locations of the arrays can be given
    values they are declared.
  • Syntax
  • data-type variable-namesizeinitia
    lizer-list
  • Example
  • int a52,5,6,8,9

5
  • Processing One-dimensional Arrays
  • / Program for declaration, initialization of a
    one-dimensional array and display its elements.
    /
  • includeltstdio.hgt
  • includeltconio.hgt
  • void main()
  • int i,a52,3,5,6,7
  • clrscr()
  • printf(The elements of array a \n)
  • for(i0ilt4i)
  • printf(3d,ai)
  • getch()

6
  • Passing Arrays to a function
  • An array can be passed to a function as a
    parameter i.e., the name of the array is used as
    an argument to a function.
  • When an array is passed to a function, the values
    of the array are not passed to the function,but
    only the address of the first array element is
    passed.
  • Example

7
  • includeltstdio.hgt
  • void main()
  • int i,num5
  • clrscr()
  • for(i0ilt5i)
  • printf(Enter the value for numberd,i1)
  • scanf(d,numi)
  • printf(\n The original list is\n)
  • for(i0ilt5i)
  • printf(d,numi)
  • sort(num)
  • printf(\n The sorted list is\n)
  • for(i0ilt5i)
  • printf(\td,numi)
  • getch()
  • sort(int n)
  • int i,j,temp
  • for(i0ilt4i)
  • for(ji1jlt5j)
  • if(nigtnj)
  • tempni
  • ninj
  • njtemp

8
  • Multidimensional Arrays
  • An array with more than one subscript is
    generally called a multidimensional array i.e,
    arrays of two dimensions(2-d arrays), arrays of
    three dimensions(3-d arrays), arrays of four
    dimension(4-d) and so on.
  • 1.Two-dimensional Arrays(2-d arrays)
  • An array with two subscripts is termed as
    two-dimensional array.A two dimensional aray can
    be group of one or more one-dimensional
    array(s),all of which share a common name(2-d
    array name) distinguishable by subscript
    values.
  • An two-dimensional arrays are very much
    associated with matrices perfect data
    structures for storing matrices.

9
  • Declaration of two-dimensional arrays
  • Syntax
  • data-type variable-namerowsizecol-si
    ze
  • Where
  • data-type refers to any valid C data type.
  • variable-name refers name of array.
  • rows colsize indicates the number of
    elements in each row.
  • Example
  • int b33
  • 0 1
    2
  • 0
  • 1
  • 2

b00 b01 b02
b10 b11 b12
b20 b21 b22
10
  • Initialization of two-dimensional Arrays
  • Syntax
  • data-type variable-namerowsizecolsizeinit
    ializer-list
  • data-type refers to any data type supported by
    C.
  • variable-name refers to array name.
  • rowsize indicates the number of rows
  • colsize indicates the number of columns.
  • Example
  • int a232,3,4,5,6,7

2 3 4
5 6 7
11
  • Processing of two-dimensional arrays
  • 1.See Matrix Addition
  • 2.See Matrix Subtraction
  • 3.See Matrix Multiplication
  • Three-dimensional Arrays
  • An array with three subscript is termed as a
    three-dimensional array.A three-dimensional array
    is a collection of one or more two-dimensional
    array is a collection of one or more
    two-dimensional arrays,all of which share a
    common name and are distinguishable by values of
    the first subscript of the three-dimensional
    array.
  • Declaration of three-dimensional arrays
  • Syntax
  • data-type variable-anmesize1size-2size-3
  • Where,
  • data-type refers to any data type supported by
    C.
  • variable-name refers to the array name

12
  • size1 indicates the number of tables being
    grouped together.
  • size2 indicates the number of rows of each
    table.
  • size3 indicates the number of columns of
    each table.
  • Example
  • int a245
  • whare a is declared to be a 3-d array.
  • a000 indicates data item in first
    table,first row,first column.
  • a100 indicates data item in second
    table,first row,first column.
  • a112 indicates data item in second
    table,second row,thrid column.

13
  • / To accept the elements of a 3-d array and
    display item /
  • includeltstdio.hgt
  • includeltconio.hgt
  • void main()
  • int a222,I,j,k
  • clrscr()
  • printf(Enter the elements of a of order
  • 222(Two tables of size 22) \n)
  • for(i0ilt2i)
  • for(j0jlt2j)
  • for(k0klt2k)
  • scanf(d,aijk)
  • printf(\n The 3-d arrary a \n\n)
  • for(i0ilt2i)
  • printf(a-Table d \n\n,i1)
  • for(j0jlt2j)
  • for(k0klt2k)
  • printf(4d,aijk)
  • printf(\n)
  • printf(\n\n)
  • getch()
  • Input-Output
  • Enter the Elements
  • 1 2 3 4 5 6 7 8
  • The 3-d array a
  • a-Table 1
  • 1 2
  • 3 4
  • A-Table 2
  • 5 6

14
  • Arrays and String
  • In C Strings are defined as arrays of
    characters.
  • Example
  • char names710
  • names0Raj
  • names1Jayaram
  • .
  • .
  • .
  • names6Aishu

15
  • Example
  • includeltstdio.hgt
  • includeltconio.hgt
  • void main()
  • char names520
  • printf(Enter five names \n)
  • for(i0ilt5i)
  • scanf(s,namesi)
  • printf(List of Names \n)
  • for(i0ilt5i)
  • printf(s \n,namesi)
  • getch()
  • Input-Output List of Names
  • Devaraj
    Devaraj
  • Shobana
    Shobana
  • Jayanthi
    Jayanthi
  • Shanthi
    Shanthi

16
  • Structure
  • A structure can be defined to be a group of
    logically data items, which may be of different
    types,stored in contiguous memory locations,
    sharing a common name, but distinguished by its
    members.
  • Syntax
  • struct tag-name
  • data-type member1
  • data-type member2
  • data-type member3
  • variable 1,variable 2,.variable n
  • Declaration of Structure Variables
  • struct tag-name variable-name
  • Example
  • struct emp
  • int empno
  • char name20
  • float salary
  • e1,e2

17
  • Initialization of Structure Variables
  • Syntax
  • struct tag-name variable-namemember1-value,mem
    ber2-value,

  • membern-value
  • Example
  • struct emp e121,Anju,20000
  • Operation of Structure
  • Accessing the individual members of a structure
    variable with help of member operator.
  • Example
  • struct emp e
  • e.empno10
  • 10 is assigned to empno member of e.
  • strcpy(e.name,Ram)
  • The string Ram is copied to name member of e.

18
  • includeltstdio.hgt
  • includeltconio.hgt
  • struct emp
  • int empno
  • char name20
  • float salary
  • void main()
  • struct e1
  • clrscr()
  • printf(Enter empno,name and salary \n)
  • scanf(dsf,e1.empno,e1.name,
  • e1.salary)
  • printf(e1.empnod\n,e1.empno)
  • printf(e1.names\n,e1.name)
  • printf(e1.salary8.2f\n\n,e1.salary)
  • getch()

19
  • Passing Structure to function
  • Similar to,we pass arrays of basic
    type(char,int,float,etc.,)to functions,passint
    arrays of structures to functions as arguments.
  • Example
  • includeltstdio.hgt
  • includeltconio.hgt
  • struct emp
  • int empno
  • char name20
  • float salary
  • void display(struct emp,int)
  • void main()
  • struct emp e10
  • int i,n
  • clrscr()
  • printf(Enter no. of employees \n)
  • scanf(d,n)
  • printf(Enter d employee details \n,n)
  • for(i0iltni)
  • scanf(dsf,ei.empno,
  • ei.name,ei.salary)
  • printf(The list of employees \n)
  • getch()

20
  • void display(struct emp e,int n)
  • int i
  • for(i0iltni)
  • printf(6d 15s 8.2f \n,ei.empno,ei.n
    me,ei.salary)

21
  • Self Referential Structures
  • When a member of a structure is declared as a
    pointer that points at the same structure, then
    it is called self referential structure.
  • Syntax
  • struct tag
  • datatype1 member1
  • datatype2 member2
  • datatype3 member3
  • struct tag name
  • When tag refers to the structure name and
    name refers to the name of the pointer variable.
  • Example
  • struct node
  • char name20
  • struct node ptr

22
  • Where,
  • name-gtcharacter array
  • ptr-gtpointer to another structure of same
    type called ptr.Hence it is called
    self-referential structure.
  • Self-referential structures are very useful in
    applications that involve linked data structures,
    such as linked lists and trees.

23
  • Union
  • Like a structure, a union is also a derived data
    type.
  • The members of a union share a single storage
    space.
  • Only ONE member of each union can be referenced
    at a time.
  • Amount of space allocated for storage is the
    amount needed for the largest member of the
    union.
  • Syntax
  • union tag
  • datatype1 member1
  • datatype2 member2
  • ..
  • datatypen membern
  • variable1,variable2,.variablen

24
  • Example 1
  • union id
  • int i
  • float f
  • char c
  • Example 2
  • includeltstdio.hgt
  • void main()
  • union data
  • int i10
  • float f20.5
  • union data d
  • printf(d,d.i)
  • printf(f,d.f)

25
  • Difference between Structure and Union

STRUCTURE UNION
Every memory has its own memory sapce. All the members use the same memory space to store the values.
It can handle all the members(or) a few as required at a time. It can handle only one member at a time,as all the members use the same space.
Keyword struct is used. Keyword union is used.
It may be initialized with all its members. Only its first member may be initialized.
Any member can be accessed at any time without the loss of data. Only one member can be accessed at any time with the loss of previous data.
Different interpretation for the same memory location are not possible. Different interpretations for the same
More storage space is required. Minimum storage space is required.
Syntax structure strut-name var1..varn Syntax union union-name var1..varn
26
  • User-defined data types
  • 1.Per-defined are int float char void user
    defined are structures and union.2.A
    user-defined data type is typedef and Enumerated
    Data type.
  • 3.User defined datatype are nothing but those
    which are constructed by using structure and
    union.
  • 1.Enumerated Data TypegtEnumerated data type
    offers us a way of inventing our own data type.
  • Syntax
  • enum tag-name
  • enumerator1,
  • enumerator2,
  • enumerator3,
  • .
  • enumeratorn

27
  • Example
  • enum boolean f
  • ffalse
  • 2.Typedef
  • Typedef a facility provided by C,enables us to
    rename existing built-in data types and
    user-defined data types and thereby help in
    increasing the degree of readability of source
    code.
  • Syntax
  • typedef old-name new-name
  • Where old-name is the name of the existing data
    type and new-name is the new name given to the
    data type identified by the old-name.
  • Example
  • struct emp
  • int empno
  • char name20
  • float salary
  • typdedef struct emp EMP
  • Variables of struct emp can now be declared using
    EMP as the type specifier as
  • EMP e
  • e has been declared to be a variable of struct
    emp type.

28
  • Bitwise Operation
  • In computer programming, a bitwise operation
    operates on one or two bit patterns or binary
    numbers at the level of their individual bits.
  • Bitwise operations are slightly faster than
    addition and subtraction operations and
    significantly faster than multiplication and
    division operations.
  • The bitwise operators utilize something called
    Binary Math. If you already know binary Math, it
    is BASE 2.
Write a Comment
User Comments (0)
About PowerShow.com