introduction to array 20082013 - student

Upload: nazri-jumardi

Post on 14-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 Introduction to Array 20082013 - Student

    1/35

    Introduction to Array

  • 7/29/2019 Introduction to Array 20082013 - Student

    2/35

    Properties of array Index

    Data type

    Contiguous One dimensional array

    Operations using arrayMinimum

    Maximum Count

    Average

    Summation

    Array

  • 7/29/2019 Introduction to Array 20082013 - Student

    3/35

    What is Array?

    A block of memory locations in the internalmemory that is assigned to one variable name.

    Each array memory location is called element.

    Sometimes called a subscripted variable.

    Static arrays An array in which the maximum number of

    elements cannot change during processing

    Dynamic arrays

    An array in which the maximum number ofelements can change during processing

    More flexible and use less memory space thanstatic arrays, but.. more time consuming during

    processing

  • 7/29/2019 Introduction to Array 20082013 - Student

    4/35

    Array

    A series of elements of the same type placed in contiguous memory

    locations that can be individually referenced by adding an index to a

    unique identifier.

    Example:

    To store 5 values of type in tin an array without having to declare 5different variables, each one with a different identifier.

    To store 5 different values of the same type, in tfor example, with a

    unique identifier.

    For example, an array to contain 5 integer values of type int called

    billy could be represented like this:

  • 7/29/2019 Introduction to Array 20082013 - Student

    5/35

    Array

    Each blank panel represents an element of the array, in this case

    integer values of type int.

    These elements are numbered from 0 to 4 since in arrays the first

    index is always 0, independently of its length.

    Like a regular variable, an array must be declared before it is used.A typical declaration for an array in C++ is:

    type name [elements];

    where type is a valid type (like int, float...), name is a valid identifier

    and the elements field (which is always enclosed in square brackets

    []), specifies how many of these elements the array has to contain.

  • 7/29/2019 Introduction to Array 20082013 - Student

    6/35

    Base-Zero vs Base-One Arrays

    Base-zero system:

    A computer system in which array element numbers begin with zero

    rather than one

    Base-one system:

    A computer system in which array element numbers begin with one

    rather than zero

  • 7/29/2019 Introduction to Array 20082013 - Student

    7/35

    Base-Zero vs Base-One Arrays

    0

    1

    2

    3

    4

    5

    6

    7

    N

    A[0]

    A[1]

    A[2]

    A[3]

    A[4]

    A[5]

    A[6]

    A[7]

    ...

    A[N]

    Base 0

    A

    1

    2

    3

    4

    5

    6

    7

    8

    N

    A[1]

    A[2]

    A[3]

    A[4]

    A[5]

    A[6]

    A[7]

    A[8]

    ...

    A[N]

    Base 1

    A

  • 7/29/2019 Introduction to Array 20082013 - Student

    8/35

    Base-Zero Array

    Example:

    100 list of marks of integer type, will be declared as:

    int mark1, mark2, mark3, , mark100;

    Notes:

    If we have 100 marks to be stored, you can imagine how long we

    have to write the declaration part by using normal variable

    declaration?

    By using an array, it can be declared as: int mark[100];

    This will reserve 100 contiguous/sequential memory locations for

    storing the integer data type.

    Graphically can be depicted as follows:

  • 7/29/2019 Introduction to Array 20082013 - Student

    9/35

    Base-Zero Array (Cont)

  • 7/29/2019 Introduction to Array 20082013 - Student

    10/35

    Types of array

    Three types

    One-dimensional array

    Two-dimensional array

    Multidimensional array

  • 7/29/2019 Introduction to Array 20082013 - Student

    11/35

    One-dimensional array

    The simplest array structure

    Consists of one column of memory locations.

    Dimension refers to the array size that is how big the

    array is. A single dimensional array declaration has the

    following form:

    array_element_data_typearray_name[array_size];

    array_element_data_type: declares the base type ofthe array, which is the type of each element in the

    array.

    array_name: is any valid C++ identifier name.

    array_size: defines how many elements the array will

  • 7/29/2019 Introduction to Array 20082013 - Student

    12/35

    One-dimensional array

    Example:

    To declare an array of 20

    characters, named

    character: char character[20]

    Can be depicted as

    follows:

    The array character can store up to 20 characters with the

    first character occupying location character[0] and the last

    character occupying character[19].

    Note that the index runs from 0 to 19.

    An index always starts from 0 and ends with (array size-1).

  • 7/29/2019 Introduction to Array 20082013 - Student

    13/35

    Eg: One-dimensional array named

    Age

    Age[0]

    Age[1]

    Age[2]

    Age[3]

    Age[4]

    Age[5]

    Age[6]

    Age[7]

    Age[8]

    Age[9]

    0

    1

    2

    3

    4

    5

    6

    7

    8

    9

    32

    54

    25

    36

    45

    20

    28

    50

    42

    20

    Array

    Age

    VariableReference Name

    The number inthe parentheses

    refers to the box

    number in the

    array, the element

    number

    Fifth element of

    theAge array

    Age[4] = 45

    The index of the first

    position in an array is 0.

    Pronounced:

    Age-sub-4

  • 7/29/2019 Introduction to Array 20082013 - Student

    14/35

    Array Initialization

    An array may be initialized at the time of itsdeclaration, which means to give initial values to anarray.

    Initialization of an array may takes the following form:

    type array_name[size] = {value_list};

    For examples:

    int id[7] = {1, 2, 3, 4, 5, 6, 7};

    Declares an integer array id and it immediately assigns the

    values 1, 2, 3, , 7 to id[0], id[1], id[2],, id[6].

    float x[5] = {5.6, 5.7, 5.8, 5.9, 6.1};

    Assigns the values 5.6 to x[0], 5.7 to x[1], and so on.

  • 7/29/2019 Introduction to Array 20082013 - Student

    15/35

    Array Initialization

    charvowel[6] = {a, e, i, o, u, \0};

    Assigns the characters a to vowel[0], e tovowel[1], and so on.

    The last character in the array vowel is the NULLcharacter (\0).

    charvowel[6] = "aeiou";

    When the value assigned to a character array is astring, the compiler automatically supplies theNULL character but we still have to reserve oneextra place for the NULL.

  • 7/29/2019 Introduction to Array 20082013 - Student

    16/35

    Exercise 1

    1. How many slots/indexes does this array have?

    2. How many characters, counting the null character at the end, does

    this array hold?

    3. The number of an index is also called an index. What is the lowest

    index?

    Answers:

    1. 11

    2. 7 characters including the null character3. 0

    0 1 2 3 4 5 6 7 8 9 10

    L o v i n g \0

  • 7/29/2019 Introduction to Array 20082013 - Student

    17/35

    Exercise 1

    4. What is the highest index for this array?

    5. Is the number for the highest index the same as that for the number ofindexes?

    6. What is stored in the last index as a character, that is, in an indexnumber 5?

    7. What is stored in the last index numerically? All strings should have thisnull character stored in its last index to signal the end of the string.

    Answers:

    4. 10

    5. No. The number of indexes are 11 but the highest index is 10 because

    the index counting starts from 0 instead of 1.

    6. A 'g' character.

    7. A null character.0 1 2 3 4 5 6 7 8 9 10

    L o v i n g \0

  • 7/29/2019 Introduction to Array 20082013 - Student

    18/35

    Reminder!

    null characteris used to terminate a

    string and this is specific to string only

  • 7/29/2019 Introduction to Array 20082013 - Student

    19/35

    Exercise 2

    1. Draw and name a one-dimensional array that would hold 10

    temperatures. Number the elements.

  • 7/29/2019 Introduction to Array 20082013 - Student

    20/35

    Answer 2

    0 1 2 3 4 5 6 7 8 9

    temperatur

    es

  • 7/29/2019 Introduction to Array 20082013 - Student

    21/35

    Exercise 3

    Given an array called myArraydeclared with 10 items, or boxes from [0] to

    [9],

    which of the following designations would be legal calls? Assume X = 5.

    1. myArray[X]

    2. myArray[2*X]3. myArray[0]

    4. myArray[X-6]

    5. myArray[4*X]

  • 7/29/2019 Introduction to Array 20082013 - Student

    22/35

    Answer 3

    1. myArray[5]

    2. myArray[10]

    3. myArray[0]

    4. myArray[-1]

    5. myArray[20]

    1. Legal because there is a box

    5

    2. Illegal because there is nobox 10

    3. Legal because there is a box

    0

    4. Illegal because there is no

    box -1

  • 7/29/2019 Introduction to Array 20082013 - Student

    23/35

    Operation using array

    Summation

    Average

    MinimumMaximum

    Count

  • 7/29/2019 Introduction to Array 20082013 - Student

    24/35

    Summation

    Example: Create a pseudocode that read 3

    floats into an array and print their sum.

    Problem: Read and add 3 float numbers in an

    array Input: Float numbers

    Output: Sum of numbers in array

  • 7/29/2019 Introduction to Array 20082013 - Student

    25/35

    Summation 1

    Start

    float data[3]; int i;

    Set sum = 0;

    Promt for data.

    for (i=0; i

  • 7/29/2019 Introduction to Array 20082013 - Student

    26/35

    Summation 2

    Start

    int i, total = 0, y[n] = {6,9,2,4,5,23,12};

    for (i=0; i

  • 7/29/2019 Introduction to Array 20082013 - Student

    27/35

    Average

    Example: Input 10 integers. Determine and

    display the mean (average) integers.

    Problem: Read and add 10 integer numbers in an

    array Input: Integer numbers

    Output: Average of numbers in array

  • 7/29/2019 Introduction to Array 20082013 - Student

    28/35

    Average

    Start

    int data[10], count; float average;

    Set sum = 0;

    Promt for data.

    for (count=0; count

  • 7/29/2019 Introduction to Array 20082013 - Student

    29/35

    Minimum

    Start

    int i; float small, a[n]={100.00,40.00,-

    30.00,400.00,60.00,-25.00,-24.00,0.00,3.24,0.50};

    Set small = a[0];

    for (i=0; i

  • 7/29/2019 Introduction to Array 20082013 - Student

    30/35

    Maximum

    Start

    int a[5] = {3, 7, 4, 9, 6}, i;Set largest = a[0];

    for (i=0; i largest)

    largest = a[i]

    Endfor

    Print largest

    End

    Example: Printing the largest number in the

    array

  • 7/29/2019 Introduction to Array 20082013 - Student

    31/35

    Maximum (Cont)

    Start

    int a[5] = {3, 7, 4, 9, 6}, i;

    Set largest = a[0];

    Set index = 0;

    for (i=0; i largest)

    largest = a[i]

    index = i

    Endfor

    Print index

    End

    Example: Print only the index of the largest

    number in the array

  • 7/29/2019 Introduction to Array 20082013 - Student

    32/35

    Count

    Example:

    Initialize an array with 6 integers. Read another integer and

    print the value of each array element that is less than this

    read-in integer and print the number of elements that were

    not printed. For example, if the array were 40, 80, 20, 50,90, 30 and an 80 were read in, then the output would be

    something like:

    -------Output-------

    40 20 50 30

    count = 2

  • 7/29/2019 Introduction to Array 20082013 - Student

    33/35

    Count

    Start

    int num[6] = {40, 80, 20, 50, 90, 30}, num2, i,

    count=0;

    Prompt for an integer num2Read integer num2

    for(i=0;i num2)

    count = count + 1

    Endfor

    Print count

    End

  • 7/29/2019 Introduction to Array 20082013 - Student

    34/35

    Exercise 1

    1. Initialize an array with 6 integers for thefollowing array arr. Read another integer andadd that number to each element in the array.Print the original and the new array element.

    arr [6] = {3, 5, 7, 2, 12, 10}

  • 7/29/2019 Introduction to Array 20082013 - Student

    35/35

    Exercise 2

    1. Print the sum of the numbers greater than 5and the count of the elements less than orequal to 5 in the following array:

    a[5] = {3, 7, 4, 9, 6},

    sum = 22, count = 2