EKT120_LECTURE07_ARRAYS1

Embed Size (px)

Citation preview

  • 8/12/2019 EKT120_LECTURE07_ARRAYS1

    1/31

    UniMAP Sem II - 09/10 EKT120 : Computer Programming 1

    Lecture 7Arrays (1)

  • 8/12/2019 EKT120_LECTURE07_ARRAYS1

    2/31

  • 8/12/2019 EKT120_LECTURE07_ARRAYS1

    3/31

    UniMAP Sem II - 09/10 EKT120 : Computer Programming 3

    What is an Array? The variables that we have used so far have all

    common characteristics: Each variable could only store a single value at a

    time. Example:

    int count, length, num;double average, total;char selection, choice;

    An array is a collection of a fixed number ofcomponents wherein all of the components are of thesame type

  • 8/12/2019 EKT120_LECTURE07_ARRAYS1

    4/31

    UniMAP Sem II - 09/10 EKT120 : Computer Programming 4

    What is an Array? (Example) Example: Let us work with a list of five

    integers:

    5, 10, 15, 20, and 25. Previously we would declare five

    variables:int num1, num2, num3, num4, num5;

    By using array, since they are all of thesame data type, we could just write:int num[5];

  • 8/12/2019 EKT120_LECTURE07_ARRAYS1

    5/31

    UniMAP Sem II - 09/10 EKT120 : Computer Programming 5

    What is an Array? (Example)

    5

    10

    15

    20

    25

    num

    num[0]

    num[1]

    num[2]

    num[3]

    num[4]

    5 components or elements in

    this array.

    Elements are referred to index.

    Element num[2] has index 2and value 15.

  • 8/12/2019 EKT120_LECTURE07_ARRAYS1

    6/31

    UniMAP Sem II - 09/10 EKT120 : Computer Programming 6

    Arrays of Data Engineering applications usually involve large

    chunk of data (of common type)

    Arrays provide easy and efficient concept fordata storage or management

    Arrays are usually processed through loops(processing is very common)

    Arrays are accessed by indicating an addressor index

  • 8/12/2019 EKT120_LECTURE07_ARRAYS1

    7/31

    UniMAP Sem II - 09/10 EKT120 : Computer Programming 7

    Arrays in CArrays can assume any type (including

    the primitive data types)

    int, char, string, double, float, etc.

    Like any other instances, arrays mustbe declared before use.

  • 8/12/2019 EKT120_LECTURE07_ARRAYS1

    8/31

  • 8/12/2019 EKT120_LECTURE07_ARRAYS1

    9/31

    UniMAP Sem II - 09/10 EKT120 : Computer Programming 9

    Multiple Instances vs. Array// multiple instanceint value1, value2, value3;

    printf (Enter first value: );

    scanf (%d, &value1);

    printf(Enter second value: );scanf(%d, &value2);

    printf (Enter third value: );scanf(%d, &value3);

    // process or display

    // arrayint valueArray[3];

    for(int count=0;count

  • 8/12/2019 EKT120_LECTURE07_ARRAYS1

    10/31

    UniMAP Sem II - 09/10 EKT120 : Computer Programming 10

    Arrays - Memory Allocation Arrays are allocated

    bulk memory

    Single reference usedfor multiple locations

    Items are accessed

    based on index(address) withreference to first item

  • 8/12/2019 EKT120_LECTURE07_ARRAYS1

    11/31

    UniMAP Sem II - 09/10 EKT120 : Computer Programming 11

    Arrays Arithmetic Operations on arrays are similar to that

    on basic variables.

    sum = num[0] + num[1] + num[2] +

    num[3];

    mult = 3 * num[1];

    remainder = num[3] % 3;

    total = num[1] * num[2];

  • 8/12/2019 EKT120_LECTURE07_ARRAYS1

    12/31

    UniMAP Sem II - 09/10 EKT120 : Computer Programming 12

    Array Initialization Arrays can be initialized directly, but assignments are

    done using loops

    Like any other simple variable, arrays can also beinitialized while they are being declared.

    double sales[5] = {12.25, 32.50, 16.90, 23, 45.68};

    sales[0]=12.25, sales[1]= 32.50, sales[2]=16.90,sales[3]= 23.00, sales[4]=45.68;

  • 8/12/2019 EKT120_LECTURE07_ARRAYS1

    13/31

    UniMAP Sem II - 09/10 EKT120 : Computer Programming 13

    Array Initialization (cont) Initializers:

    If not enough initializers, rightmost element becomes 0

    int n[ 7 ] = { 1, 2, 3, 4, 5 }; => n[5] = n[6] = 0

    All elements = 0

    int n[ 5 ] = { 0 };

    If size is omitted, initializers determine the size

    int n[ ] = { 1, 2, 3, 4, 5 };5 initializers, therefore 5 element array

  • 8/12/2019 EKT120_LECTURE07_ARRAYS1

    14/31

    UniMAP Sem II - 09/10 EKT120 : Computer Programming 14

    Sample Program#include int main()

    {int a[3]= {11,22}, b[]={44, 55, 66},i;double x[2],y[10];

    printf("a[0]=%2d, a[1]=%2d, a[2]=%2d \n""b[0]=%2d, b[1]=%2d, b[2]=%2d \n\n",

    a[0],a[1],a[2],b[0],b[1],b[2]);

    printf("Please enter two real numbers\n");scanf("%lf %lf",&x[0], &x[1]);printf("x[0] = %.1lf x[1] = %.1lf\n\n", x[0], x[1]);

    for (i=0;i

  • 8/12/2019 EKT120_LECTURE07_ARRAYS1

    15/31

    UniMAP Sem II - 09/10 EKT120 : Computer Programming 15

    Sample Program Output:

    a[0]=11, a[1]=22, a[2]= 0b[0]=44, b[1]=55, b[2]=66

    Please enter two real numbers77.0 88.0x[0] = 77.0 x[1] = 88.0

    y[0]=0.00y[1]=100.00y[2]=200.00y[3]=300.00

    y[4]=400.00y[5]=500.00y[6]=600.00y[7]=700.00y[8]=800.00y[9]=900.00

  • 8/12/2019 EKT120_LECTURE07_ARRAYS1

    16/31

    UniMAP Sem II - 09/10 EKT120 : Computer Programming 16

    Array Initialization DuringDeclaration

    When declaring and initializing arrays, it isnot necessary to specify the size of the

    array. The size of the array is determined by the

    number of initial values in the braces.

    double sales[] = {12.25, 32.50, 16.90, 23, 45.68};

  • 8/12/2019 EKT120_LECTURE07_ARRAYS1

    17/31

    UniMAP Sem II - 09/10 EKT120 : Computer Programming 17

    A simple example

    The program declares and initializes thearray y. It uses a for loop with index i to

    access the successive elements of y. Foreach loop iteration, the value accessed id isadded to the variable total which is finallydisplayed. Note that the loop index i starts

    from 0 to 4 (not from 1 to 5). Also, note thatthe array size n is declared in the definestatement.

  • 8/12/2019 EKT120_LECTURE07_ARRAYS1

    18/31

    UniMAP Sem II - 09/10 EKT120 : Computer Programming 18

    A simple example (cont..)

    #include#define n 5 // define number of n in the array

    void main(){

    int i, total = 0; // variable declarationint y[n]={9,6,20,5,12}; // array declaration and

    // initializationfor (i=0;i

  • 8/12/2019 EKT120_LECTURE07_ARRAYS1

    19/31

    UniMAP Sem II - 09/10 EKT120 : Computer Programming 19

    Notes

    The defined constants, #define is used to easeany future amendments of the codes, for

    instance, if the array is to be widen to an nof 10instead of 5, it would be adequate by modifyingthe line:

    #define n 5 #define n 10

    there is no need to make any other changes tothe program, thus making the life of programmereasier.

  • 8/12/2019 EKT120_LECTURE07_ARRAYS1

    20/31

    UniMAP Sem II - 09/10 EKT120 : Computer Programming 20

    Operations on Array

    Reading data in an array

    for (index = 0; index < 10; index++)

    scanf (%d, &sale[index]);

    Printing an arrayfor (index = 0; index < 10; index++)

    printf (%d , sale[index]);

  • 8/12/2019 EKT120_LECTURE07_ARRAYS1

    21/31

    UniMAP Sem II - 09/10 EKT120 : Computer Programming 21

    Parallel Arrays

    Two (or more) arrays are calledparallel if their corresponding

    components hold related information.int student_Id[50];

    char student_Grade[50];

  • 8/12/2019 EKT120_LECTURE07_ARRAYS1

    22/31

    UniMAP Sem II - 09/10 EKT120 : Computer Programming 22

    Multi-Dimensional Arrays

    Arrays can have multiple dimensions

    Most used is the 2-dimensional array

    (for matrix implementation)

    Actual implementation is a single array(segmented)

    Nested loopstructure usually used toaccess items

  • 8/12/2019 EKT120_LECTURE07_ARRAYS1

    23/31

  • 8/12/2019 EKT120_LECTURE07_ARRAYS1

    24/31

    UniMAP Sem II - 09/10 EKT120 : Computer Programming 24

    Multi-Dimensional Arrays(cont..)

    A collection of the same type of datastored in contiguous and increasing

    memory locations. Declaration of multi-dimensional array:

    int b[2][3] = {51, 52, 53, 54, 55, 56};

    array_type array_name Array dimension = 2

    two rows

    three columns first rowinitial values

    second rowinitial values

  • 8/12/2019 EKT120_LECTURE07_ARRAYS1

    25/31

  • 8/12/2019 EKT120_LECTURE07_ARRAYS1

    26/31

    UniMAP Sem II - 09/10 EKT120 : Computer Programming 26

    Multi-Dimensional Arrays(cont..)

    can use braces ({ }) to separate rows in 2-dimensional arrays. For example:

    int c [4][3] = {{1, 2, 3},{4, 5, 6},{7, 8, 9},{10,11,12}};

    int c [4][3] = {{1, 2},{4, 5, 6},{7},{10,11,12}};

    initializes c[0][2], c[2][1] and c[2][2] to be zero

    int c [ ][3] = {{1, 2, 3},{4, 5, 6},{7, 8, 9},{10,11,12}};

    implicitly declares the number of rows to be 4

    4 rows

    3 columns

    rows

    columns

  • 8/12/2019 EKT120_LECTURE07_ARRAYS1

    27/31

    UniMAP Sem II - 09/10 EKT120 : Computer Programming 27

    Notes on Arrays

    Arrays enable better and easier datamanagement system

    Closely related to loops Indexing is zero-based

    (0 to n-1 for an array with n locations)

    Multi-dimensional arrays requirenested loopstructure

    (e.g. 2-dimensional array)

  • 8/12/2019 EKT120_LECTURE07_ARRAYS1

    28/31

    UniMAP Sem II - 09/10 EKT120 : Computer Programming 28

    Index out of bounds

    Out of bounds is when (index < 0) or

    (index > arraySize - 1)

    It is a run-time error, happens when anindex is outside the valid boundaries ofthe array. Example:

    int a[10]; int x = 10a[9] = 3 ; //ok

    a[x] = 4 ; //10 is not within the range 0..9

  • 8/12/2019 EKT120_LECTURE07_ARRAYS1

    29/31

    UniMAP Sem II - 09/10 EKT120 : Computer Programming 29

    Index out of bound

    In C, no guard against this problem

    Does not check whether index value is

    within range or not Can result in accessing data of wrong

    memory location

  • 8/12/2019 EKT120_LECTURE07_ARRAYS1

    30/31

  • 8/12/2019 EKT120_LECTURE07_ARRAYS1

    31/31

    UniMAP Sem II - 09/10 EKT120 : Computer Programming 31

    EndArrays (1)

    Q & A!