15Lect Array 1st Continue

Embed Size (px)

Citation preview

  • 7/28/2019 15Lect Array 1st Continue

    1/14

  • 7/28/2019 15Lect Array 1st Continue

    2/14

    What Are Arrays?

    It is a data structure

    Unlike a variable used to store a single value,

    e.g int debt = 2;

    It is used to store series of values of the same type, sequentially

    The whole array bears a single name, and the individual items, or

    elements, are accessed by using an integer index e.g int debts[10];//int is the datatype

    //debts is name of array//and 20 is the number of elements //contained in an

    array

    1

    debts

    2

    debt

  • 7/28/2019 15Lect Array 1st Continue

    3/14

    How the Compiler recognizes an Array?

    Just like variables and functions array have to be declared beforetheir usage.

    Declaration of array;/* some array declarations */

    int main(void)

    {

    float a[365]; /* array of 365 floats */

    char b[12]; /* array of 12 chars */

    int c[50]; /* array of 50 ints */

    int debts[10]; /* array of 10 ints */

    ... }

    Above [] brackets identifies a as an array

    We can access individual elements by writing its index number orsubscript in square brackets along with the array name. cont.

  • 7/28/2019 15Lect Array 1st Continue

    4/14

    How to Access the Elements of Array

    In C the index of an Arraystarts at index 0 and continueuntil one less then the total i.e.(10-1) in case ofdebts[10]

    To access data, just writeindex of the element, we want,with the name of the array, e.g

    X = debts[2];

    /*this will transfer thevalue at index 2 ofdebts array, i.e. 1 tothe variable X*/

    1

    2

    1

    .

    .

    .

    5

    8

    debts[0]

    debts[1]

    debts[2]

    debts[8]

    debts[9]

    .

    .

    .

    Array Index Array Data

  • 7/28/2019 15Lect Array 1st Continue

    5/14

  • 7/28/2019 15Lect Array 1st Continue

    6/14

    Initialization of An Array

    A single variable can be initialized as;

    int debt = 2;

    Array Initialization;#define NUM = 8; /*constant, a good prog. practice*/

    int main(void)

    {

    int powers[NUM] = {1,2,4,6,8,16,32,64}; /* ANSI only */

    ... }comma-separated list of values enclosed in braces form

    the elements of an array

  • 7/28/2019 15Lect Array 1st Continue

    7/14

  • 7/28/2019 15Lect Array 1st Continue

    8/14

    Initialization cont.

    /* some_data.c -- partially initialized array */

    #include

    #define SIZE 4int main(void)

    {

    int some_data[SIZE] = {1492, 1066};

    int i;

    printf("%2s%14s\n", "i", "some_data[i]");

    for (i = 0; i < SIZE; i++)printf("%2d%14d\n", i, some_data[i]);

    return 0;

    }

    This time the output looks like this:

    i some_data[i]0 1492

    1 1066

    2 0

    3 0

    Which means partial initialization, also set the other memory locations to 0;

  • 7/28/2019 15Lect Array 1st Continue

    9/14

    Initialization cont

    Another way;

    const int days[] = {31,28,31,30,31,30,31,31,30,31};

    //compiler sets the number of items to 10 automatically

    Designated Initialization;

    int arr[6] = {0,0,0,0,0,212}; // traditional syntax

    int arr[6] = {[5] = 212}; // initialize arr[5] to 212

    if we want to initialize just one element of array,former is the traditional way, and later is the newC99 method.

  • 7/28/2019 15Lect Array 1st Continue

    10/14

    Assigning Values to Arrays

    We can use loops for assigning values to array

    in addition to assignment at initialization

    Some wrong methods of assignment;/* nonvalid array assignment */

    #define SIZE 5

    int main(void){

    int o[SIZE] = {5,3,2,8}; /* ok here */

    int y[SIZE];

    y = o; /* not allowed */

    y[SIZE] = o[SIZE]; /* invalid */

    y[SIZE] = {5,3,2,8}; /* doesn't work */

    }

  • 7/28/2019 15Lect Array 1st Continue

    11/14

    Array Bounds

    C compiler wont check the boundary of your array, so its theprogrammer responsibility.

    Reason! To make C faster.

    How to avoid!

    Use a symbolic constant for length of Array index,

    Remember that indexing starts at 0 in C.int n = 5;

    int m = 8;

    float a1[5]; // yes

    float a2[5*2 + 1]; // yes

    float a3[sizeof(int) + 1]; // yes

    float a4[-4]; // no, size must be > 0

    float a5[0]; // no, size must be > 0

    float a6[2.5]; // no, size must be an integer

    float a7[(int)2.5]; // yes, typecast float to int constant

    float a8[n]; // not allowed before C99

    float a9[m]; // not allowed before C99 VLA

  • 7/28/2019 15Lect Array 1st Continue

    12/14

    Multidimensional Arrays

    Declaration;int box[2][2];int box[2][2][2]; //uptil 26 in Bloodshed DevC

    Initialization;Int box[2][2] = { {1, 2}, {3, 4} }; //two col and rows

    Int box[2][2][2] = {

    { {1,2}, {3, 4} },

    { {5,6}, {7, 8} }

    };

  • 7/28/2019 15Lect Array 1st Continue

    13/14

    Passing Arrays to Function

    #include

    int sum(int arr[]);

    int main(void){

    int i ;

    int arr[4];

    long answer;

    for (i = 0; i < 4; i++)

    arr[i] = 2*i;

    answer = sum(arr);

    for (i = 0; i < 4; i++)

    printf("index = %d, value = %d\n", i, arr[i]);

    printf("result = %d\n", answer);

    }

    int sum(int arr[])

    { int i;

    int result = 0;

    for (i = 0; i < 4; i++)

    result = arr[i] + result;return (result);}

  • 7/28/2019 15Lect Array 1st Continue

    14/14