p8 arrays

Upload: abhishek-ek

Post on 06-Apr-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 P8 ARRAYS

    1/19

  • 8/3/2019 P8 ARRAYS

    2/19

    ARRAYS

    Sequence of memory elements of same

    data type.

    Sequence of 10 integers stored one afteranother in memory represents an array.

    int a[10];

  • 8/3/2019 P8 ARRAYS

    3/19

    ARRAYS

    Fixed number of data items of same type.

    Data items-Elements

    Type int ,float and long etc.. Array of datatype character - strings

  • 8/3/2019 P8 ARRAYS

    4/19

    ARRAY Declaration

    a array of 10 integers

    Square braces represents number of

    elements

    Array index starts from a[0] as shownbelow:

    int a[10]Data type

    Name of array

    Size of array

    Note: In C, array indices always begin with zero.

  • 8/3/2019 P8 ARRAYS

    5/19

    Accessing ARRAY Elements

    Array index points to particular elements in

    an array.

    Example: a[3]=54

    a[7]=7

    a[5]=21

    a[10]=?

    28 67917342178548615

  • 8/3/2019 P8 ARRAYS

    6/19

    Usage of for loop

    Commonly used to access array elements.

    for(i=0;i

  • 8/3/2019 P8 ARRAYS

    7/19

    Initialization of ARRAY

    Can be initialized once they are declared.

    int a[10]={28,15,86,54,78,21,34,7,91,67};

    Values should be given in curly brackets. Less than 10 elements-0 will be filled.

    Greater than 10 elements-excessive

    element declared warning will be reported. List of values-separated by commas.

  • 8/3/2019 P8 ARRAYS

    8/19

    Name of the array

    Take the case where a is an array of 10

    integers.

    a-Name of the array

    If a is used without any subscript in the printf,

    what will be the output?

    What will be the output of the following

    statement?

    printf(%d, sizeof(a));

    Note : a =&a[0]

  • 8/3/2019 P8 ARRAYS

    9/19

    ARRAYS

    To handle matrix and tables, we need

    array with multiple subscripts.

    Two-dimensional array-two subscripts Row and column-starts with zero

    2-D array can also be called as matrix.

  • 8/3/2019 P8 ARRAYS

    10/19

    2-D ARRAY Declaration

    35.5 40.5 45.5

    50.5 55.5 60.5

    65.0 70.0 75.0

    80.0 85.0 90.0

    float marks[4][3]Data type

    Name of the array

    Size of an array

    4 rows

    3 columns

    0

    1

    2

    3

    0 21

    Elements are accessed as follows:

    Marks[0][0]=35.5

    Marks[1][1]=55.5

    Marks[2][1]=70.0

    Marks[3][1]=85.0

  • 8/3/2019 P8 ARRAYS

    11/19

    Storage of 2-D ARRAY

    200

    0

    200

    4

    200

    8

    201

    2

    201

    6

    202

    0

    202

    4

    202

    8

    203

    2

    203

    6

    204

    0

    204

    4

    Marks[0][0]

    Marks[0][1]

    Marks[1][0] Marks[2][0] Marks[3][0]

    Initialization:

    Float marks[4][3]={ {35.5,40.5,45.5},

    {50.5,55.5,60.5},

    {65.0,70.0,75.0},

    {80.0,85.0,90.0} };

  • 8/3/2019 P8 ARRAYS

    12/19

    Passing ARRAY to a Function

    /*Pass by Value*/

    main( )

    {

    int i ;

    int marks[ ] = { 55, 65, 75, 56, 78, 78, 90 } ;

    for ( i = 0 ; i

  • 8/3/2019 P8 ARRAYS

    13/19

    Passing ARRAY to a Function

    /* Demonstration of call by reference */

    main( )

    {

    int i ;

    int marks[ ] = { 55, 65, 75, 56, 78, 78, 90 } ;

    for ( i = 0 ; i

  • 8/3/2019 P8 ARRAYS

    14/19

    Write a program to add two matrices using

    function

    Eg: function addmat()

  • 8/3/2019 P8 ARRAYS

    15/19

    POINTERS AND ARRAYS

    Facts to be considered:

    Array elements are always stored in

    contiguous memory locations.Pointer when incremented always points

    to immediately next location of its type.

  • 8/3/2019 P8 ARRAYS

    16/19

    POINTER TO ARRAY

    main()

    {

    int num[ ]={24,34,12,44,56,17};

    int i=0,*j;

    j=&num[0];while(i

  • 8/3/2019 P8 ARRAYS

    17/19

    POINTER TO ARRAY

    Accessing array elements using pointer is

    faster than accessing them by subscripts.

    Should be accessed using pointers, if theelements are to be accessed in a fixed

    order.

    If there is no fixed logic, then subscript can

    be used but pointer will be faster always.

    Pointer notations:

    num[i] = *(num+i) = *(i+num) = i[num]

  • 8/3/2019 P8 ARRAYS

    18/19

    I request Electronics and communication

    ENGINEERING students to visit my blogfor

    more

    abhishek1ek.blogspot.com

    awhengineering.blogspot.com

  • 8/3/2019 P8 ARRAYS

    19/19