COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

download COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

of 43

Transcript of COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    1/43

    9. Array

    Knowledge:

    Understand the execute technique of array

    Skill:Can write application program using one and two dimensionalarray

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    2/43

    TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/20072

    Why Array?

    Assume that you were asked to write aprogram that accept 10 student marks,

    calculating average mark, and then print out

    number of students that get more thanaverage mark.

    How to store all these marks?

    Consider the following..(recall)

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    3/43

    TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/20073

    public class Average {

    public static void main(String[] args) {

    //variables declaration

    int mark1,mark2,mark3,mark4,mark5;

    int mark6,mark7,mark8,mark9,mark10;

    int studentNumbers, totalMark;

    float markAverage;

    Scanner input = new Scanner(System.in);

    Variables Declaration

    Solution Without Array (1/6)

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    4/43

    TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/20074

    //get student mark

    System.out.print(Mark 1:);mark1 = input.nextInt();

    System.out.print(Mark 2:);

    mark2 = input.nextInt();

    System.out.print(Mark 3:);mark3 = input.nextInt();

    System.out.print(Mark 4:);

    mark4 = input.nextInt();

    System.out.print(Mark 5:);mark5 = input.nextInt();

    Get Inputs

    Solution Without Array (2/6)

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    5/43

    TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/20075

    //get student mark

    System.out.print(Mark 6:);mark6 = input.nextInt();

    System.out.print(Mark 7:);

    mark7 = input.nextInt();

    System.out.print(Mark 8:);mark8 = input.nextInt();

    System.out.print(Mark 9:);

    mark9 = input.nextInt();

    System.out.print(Mark 10:);mark10 = input.nextInt();

    What would happen if there were 1000 students?

    Solution Without Array (3/6)

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    6/43

    TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/20076

    //calculate total marks

    totalMarks = mark1 + mark2 + mark3 + mark4+ mark5 + mark6 + mark7 + mark8 + mark9+ mark10;

    //calculate average mark

    markAverage = totalMark/10;

    Solution Without Array (4/6)

    What would happen if there were 1000 students?

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    7/43TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/20077

    //calculate number of student above average

    if (mark1 > markAverage) studentNumber++;if (mark2 > markAverage) studentNumber++;

    if (mark3 > markAverage) studentNumber++;

    if (mark4 > markAverage) studentNumber++;

    if (mark5 > markAverage) studentNumber++;if (mark6 > markAverage) studentNumber++;

    if (mark7 > markAverage) studentNumber++;

    if (mark8 > markAverage) studentNumber++;

    if (mark9 > markAverage) studentNumber++;if (mark10 > markAverage) studentNumber++;

    Solution Without Array (5/6)

    What would happen if there were 1000 students?

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    8/43TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/20078

    //print the result

    System.out.println(number of student thatget more than average = +

    studentNumber);}

    }

    Solution Without Array (6/6)

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    9/43TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/20079

    Instead of using 10 variables to represent 10 student

    marks,another technique is using one-dimensional

    array for storing a list of marks.

    Array is a collection of memory location that is used

    for storing data with the same type. Each memorylocation in array is referred by arrays name and

    location memory index.

    Array (Definitions)

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    10/43TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200710

    Normal Variable

    int mark;

    Array Variable

    int[] mark = new int[10];

    mark

    mark[1]mark[2]

    mark[3]

    mark[4]

    mark[5]mark[6]

    mark[7]

    mark[8]

    mark[9]

    mark[0]

    Array VS NormalVariables

    mark

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    11/43TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200711

    67

    mark

    34

    76

    47

    82

    54

    38

    72

    63

    24

    [0]

    [1]

    [2]

    [3]

    [4]

    [5]

    [6]

    [7]

    [8]

    [9]

    Single element in array

    is retrieved using index

    Indices start from 0;the 1st element of an

    array mark is referred toas mark[0] and the n-thelement as mark[n-1].

    An index can have anyint value from 0 toarrays length - 1.

    Indices (Subscript)(1/2)

    A

    r

    ra

    y

    In

    d

    e

    x

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    12/43TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200712

    67

    34

    76

    47

    82

    54

    38

    72

    63

    24

    [0]

    [1]

    [2]

    [3]

    [4]

    [5]

    [6]

    [7]

    [8]

    [9]

    An index is written within squarebrackets after arrays name

    To retrieve 1st element in array

    To retrieve 6th element in array.

    To retrieve nthelement in array

    element1=mark[0];

    Array Index

    Indices (Subscript)(2/2)

    element6=mark[5];

    Array Index

    elementn=mark[n-1];

    mark

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    13/43TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200713

    Syntax 1

    DataType [ ] variableName = new DataType[arraysize

    of type integer]

    Example :define an array variable called monthName oftype String with length 12

    String[ ] monthName = new String[12];

    Declare One Dimensional Array (1/3)

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    14/43TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200714

    Syntax 2

    DataType [ ] variableName;variableName = new DataType[arraySize of type integer]

    Example :

    define an array variable called studentNum of type integerwith length indicate by user input;

    int length;

    int[ ] studentNum;length = input.nextInt();

    studentNum = new int[length];

    Declare One Dimensional Array (2/3)

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    15/43TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200715

    Syntax 3

    DataType [ ] variableName = {value1, value2. value3.};

    where n items automatically defined as an array length

    Example :

    define an array variable called keyCode of type integer thatcontains 1,6,7,3,4,2,1 as it values;

    int[ ] keyCode ={1,6,7,3,4,2,1}

    Declare One Dimensional Array (3/3)

    n items

    What is the length/size of array keyCode?

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    16/43TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200716

    For each array that has been instantiated, thereis a variable called length

    The variable length contains the size of the array.

    The variable length can be directly accessed in aprogram using the array name and the dotoperator.

    .length

    Instance Variable length

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    17/43TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200717

    EXAMPLE 1:

    int[ ] mark = new int[4];System.out.println(the size of marks array is+ mark.length);

    OUTPUT:the size of marks array is

    EXAMPLE 2:int[ ] snakeLocation = {10,23,34,49,66,83,86,90,95,99};System.out.println(the size of array snakeLocation is+ snakeLocation.length);

    OUTPUT:the size of snakeLocations array is

    length Example

    10

    4

    1 2 3 4 5 6 7 8 9 10

    4mark.length

    10snakeLocation.length

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    18/43TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200718

    Using loops to step through elements in array andperform the operations.

    for (i = 0; i < list.length; i++)

    System.out.print(list[i] + " ");

    Processing One DimensionalArray

    int [] list = new int [100];

    int i;

    declarations

    for (i=0;i< list.length;i++)

    list[i] = input.nextInt();

    Using for loop to assign an input into the array

    for (i=0;i< list.length;i++)

    System.out.println(list[i] + );

    Using for loop to display all elements in array

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    19/43TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200719

    for (index = 0; index < sales.length;

    index++)

    sales[index] = 10.0;

    sales[2]=16.0;

    double[] sales = new double[4];

    Common Operation onArray (1)

    Initialize an array to a specific value

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    20/43TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200720

    for (index = 0; index < sales.length;index++)

    sales[index] = 10.0;

    sales[2]=16.0;

    double[] sales = new double[4];

    Common Operation onArray (1)

    Initialize an array to a specific value

    sales

    ?

    ?

    ?

    ?

    index sales.length

    4 true

    sales[0]=10.0

    [0]

    0

    [1]

    [2]

    [3]

    10.0

    1 false

    sales[1]=10.0

    10.0

    2

    sales[2]=10.0

    10.0

    3

    sales[3]=10.0

    10.0

    4

    16.0

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    21/43TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200721

    for (index = 0; index < sales.length; index++){

    System.out.print(Total Sales for Month +(index+1)+ :);

    sales[index] = input.nextDouble();

    }

    Common Operation onArray (2)

    Read data into an array

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    22/43TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200722

    for (index = 0; index < sales.length; index++){

    System.out.print(Total Sales for Month +(index+1)+ :);

    sales[index] = input.nextDouble();

    }

    0 10.0

    10.0

    16.0

    10.0

    1

    2

    3

    sales

    Common Operation onArray (2)

    Read data into an array

    Total Sales for Month 1 :

    0 4< true

    15.7

    13.015.7

    1

    Total Sales for Month 4 :

    Total Sales for Month 2 :19.7

    19.7

    2

    Total Sales for Month 3 : 0.57

    0.57

    3

    13.0

    4 false

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    23/43

    TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200723

    System.out.println(Month + + Total Sales);

    for (index = 0; index < sales.length; index++)

    System.out.println((index+1) + +sales[index]");

    Common Operation onArray (3)

    Print the contains of the array

    0

    1

    2

    3

    sales

    15.7

    19.7

    0.57

    13.0

    Month Total Sales

    1 15.7

    2 19.7

    3 0.574 13.0

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    24/43

    TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200724

    System.out.println(Month + + Total Sales);

    for (index = 0; index < sales.length; index++)

    System.out.println((index+1) + +sales[index]");

    Common Operation onArray (3)

    Print the contains of the array

    0

    1

    2

    3

    sales

    15.7

    19.7

    0.57

    13.0

    Month Total Sales

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    25/43

    TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200725

    sum = 0;

    for (index = 0; index < sales.length; index++)

    sum = sum + sales[index];

    if (sales.length != 0)average = sum / sales.length;

    else

    average = 0.0;

    Common Operation onArray (4)

    Find sum and average of array

    0

    1

    2

    3

    sales

    15.7

    19.7

    0.57

    13.0

    sum

    015.735.435.9748.97

    average

    12.2425

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    26/43

    TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200726

    sum = 0;

    for (index = 0; index < sales.length; index++)

    sum = sum + sales[index];

    if (sales.length != 0)average = sum / sales.length;

    else

    average = 0.0;

    Common Operation onArray (4)

    Find sum and average of array

    0

    1

    2

    3

    sales

    15.7

    19.7

    0.57

    13.0

    false

    sum

    0

    0 4< true

    15.7

    1

    35.4

    2

    35.97

    3

    48.97

    4

    average

    true

    12.2425

    48.97/4

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    27/43

    TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200727

    maxIndex = 0;

    for (index = 1; index < sales.length; index++)

    if (sales[maxIndex] < sales[index])maxIndex = index;

    largestSale = sales[maxIndex];

    Common Operation onArray (5)

    Determining largest element in array

    0

    1

    2

    3

    sales

    15.7

    19.7

    0.57

    13.0

    largeSale 19.7

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    28/43

    TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200728

    maxIndex = 0;

    for (index = 1; index < sales.length; index++)

    if (sales[maxIndex] < sales[index])maxIndex = index;

    largestSale = sales[maxIndex];

    Common Operation onArray (5)

    Determining largest element in array

    0

    1

    2

    3

    sales

    15.7

    19.7

    0.57

    13.0

    largeSale

    1

    maxIndex 0

    < 4 true

    19.715.7 true< false

    1

    23

    0.5719.7 13.0

    4 false

    19.7

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    29/43

    TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200729

    The System.arraycopy() method

    //original array

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

    //destination array

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

    //copy all of the ori array to the dest array,

    //starting with index 0System.arraycopy(ori,0,dest,0,ori.length);

    //result

    dest has the following contents 1,2,3,4,5,6,4,3,2,1

    Common Operation onArray (6)

    Copying Arrays

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    30/43

    TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200730

    An array is in bounds if:

    0

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    31/43

    TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200731

    Array Index Out of Bounds(2/2)

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    32/43

    TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200732

    Relational Operators Arrays

    int[] listA = new int[3];

    int[] listB = new int[3];

    if (listA == listB)

    ...

    The expression listA == listB determines if the values

    oflistA and listB are the same, thus determining

    whether listA and listB refer to the same array.

    To determine whether listA and listB contain the same

    elements, you need to compare them element by element.

    Relational Operator andArray (1/2)

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    33/43

    TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200733

    Relational Operators Arrays

    int[] listA = new int[4];

    int[] listB = new int[3];

    if (listA == listB)

    listB = listA;

    if(listA == listB)

    Relational Operator andArray (2/2)

    0

    1

    2

    3

    listA

    15.7

    19.7

    0.57

    13.0

    0

    1

    2

    listB

    15.7

    19.7

    0.57

    false

    true

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    34/43

    TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200734

    Arrray of arrays

    int twoDim[] = new int [4][];

    twoDim[0] = new int[2];

    twoDim[1] = new int[2];

    twoDim[2] = new int[3];

    Multi DimensionalArray

    twoDim[0]

    twoDim

    01

    34

    1901

    2

    19

    01

    2

    23

    19

    0

    twoDim[1]twoDim[2]

    twoDim[3]twoDim[1][1]

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    35/43

    TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200735

    Relational Operators Arrays

    write a program that accept 10 student

    marks, calculating mark average, and then

    print out number of students that get

    more than mark average.

    Example of Array Usage (1)

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    36/43

    TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200736

    1. start2. Get student mark

    1. Repeat 3 times1. Get student mark and store in array

    3. Calculate total mark1. While not end of array

    1. Get element in the array2. Add the element to the sum

    4.

    Calculate average mark1. Average = sum / size of array5. Calculate number of student over average

    1. While not end of array1. Get element in array2. Compare element with the average

    1. If greater or equal than1. Increase numberOf StudentoverAverage by 1

    6. Display Result7. End

    Example of Array Usage (1/7)

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    37/43

    TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200737

    //1.start

    import java.util.Scanner;public class StudentOverAverage {

    public static void main(String[] args) {

    //declaration

    int[] mark = new int[3];int sum = 0;

    int index ;

    int average = 0;

    int numberOfStudentOverAverage=0;

    Scanner input = new Scanner (System.in);

    Example of Array Usage (2/7)

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    38/43

    TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200738

    //2. Get student mark

    // 1. Repeat 3 times// 1.Get student mark and store in array

    for (index=0;index < mark.length;index++){

    System.out.println(Enter Marks + (index+1));mark[index] = input.getInt();

    }

    Example of Array Usage (3/7)

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    39/43

    TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200739

    //3. Calculate total mark

    // 1.While not end of array// 1.Get element in the array

    // 1.Add the element to the sum

    for (index=0;index < mark.length;index++){sum = sum + mark[index]

    }

    Example of Array Usage (4/7)

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    40/43

    TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200740

    //4.Calculate average mark

    // 1.Average = sum / size of array

    average = sum / mark.length;

    Example of Array Usage (5/7)

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    41/43

    TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200741

    //5. Calculate number of student over average

    // 1. While not end of array// 1. Get element in array

    // 1. Compare element with the average

    // 2. If greater or equal than

    // 1.Increase

    // numberOfStudentoverAverage by 1

    for (index=0;index < mark.length;index++){

    if (mark[index] >= average)

    numberOfStudentOverAverage++;

    }

    Example of Array Usage (6/7)

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    42/43

    TMK3102 PENGATURCARAAN KOMPUTER JUL 2006/200742

    //6. Display Result

    System.out.println(Number of student over

    avegare are: + numberOfStudentOverAverage );

    //7. end

    }

    }

    Example of Array Usage (7/7)

  • 8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 9

    43/43

    Case Study

    100 students were asked to rate the

    quality of the food in the student

    cafeteria on a scale of 1 to 10 (1 means

    awful and 10 means excellent). Based on

    the response,1. summarize the result of the poll.

    eq:

    Rank Frequency Histogram

    1 12 ************

    2 5 *****

    2. Calculate the Min, mode and median.