arrays week 9

Upload: tinomutenda

Post on 05-Jul-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/16/2019 Arrays Week 9

    1/34

    ITEC102- SIBT

    Lecture 9

    Week 9

    Arrays

  • 8/16/2019 Arrays Week 9

    2/34

    Last Week

    • Functions

    • Nested Function Calls

    • Global and Local Variables to Functions

    http://www.google.com.au/url?sa=i&rct=j&q=&esrc=s&frm=1&source=images&cd=&cad=rja&uact=8&docid=QB6L6ZLlUr8KrM&tbnid=hTJ6ORDCFTb7_M:&ved=0CAUQjRw&url=http://omundodaprogramacao.com/tag/windows/&ei=t67hU8XBDI3d8AWcw4L4DQ&bvm=bv.72197243,d.dGc&psig=AFQjCNF4xP18VW2_lbRIVXpeQEvqKTS6JQ&ust=1407385641897682

  • 8/16/2019 Arrays Week 9

    3/34

    This Week

    • Arrays

     – Declaration

     – Initialization

     – Indexing

     – Passing

    • Practice and Exercises

    http://www.google.com.au/url?sa=i&rct=j&q=&esrc=s&frm=1&source=images&cd=&cad=rja&uact=8&docid=QB6L6ZLlUr8KrM&tbnid=hTJ6ORDCFTb7_M:&ved=0CAUQjRw&url=http://omundodaprogramacao.com/tag/windows/&ei=t67hU8XBDI3d8AWcw4L4DQ&bvm=bv.72197243,d.dGc&psig=AFQjCNF4xP18VW2_lbRIVXpeQEvqKTS6JQ&ust=1407385641897682

  • 8/16/2019 Arrays Week 9

    4/34

    Recap

    • Consider the following code

    int test(int a, int b){

    return a*b-b+a;

    }

    • Trace out the output of the following function

    call

    print(test(test(test(2,1),test(1,2)),test(test(3,3),3)));

    • 137

    http://www.google.com.au/url?sa=i&rct=j&q=&esrc=s&frm=1&source=images&cd=&cad=rja&uact=8&docid=QB6L6ZLlUr8KrM&tbnid=hTJ6ORDCFTb7_M:&ved=0CAUQjRw&url=http://omundodaprogramacao.com/tag/windows/&ei=t67hU8XBDI3d8AWcw4L4DQ&bvm=bv.72197243,d.dGc&psig=AFQjCNF4xP18VW2_lbRIVXpeQEvqKTS6JQ&ust=1407385641897682

  • 8/16/2019 Arrays Week 9

    5/34

    Need for Arrays

    • To store a set of values of the same data type

    • luckyNumbers = {80,6,12,2001,25,17,2011};

    • favMovies = {”brave heart”, ”gladiator"}; 

    • fascinatingValues = {3.14159, 42, 1.618};

    • lowerCaseVowels ={'a','e','i','o','u’};

    5

    http://www.google.com.au/url?sa=i&rct=j&q=&esrc=s&frm=1&source=images&cd=&cad=rja&uact=8&docid=QB6L6ZLlUr8KrM&tbnid=hTJ6ORDCFTb7_M:&ved=0CAUQjRw&url=http://omundodaprogramacao.com/tag/windows/&ei=t67hU8XBDI3d8AWcw4L4DQ&bvm=bv.72197243,d.dGc&psig=AFQjCNF4xP18VW2_lbRIVXpeQEvqKTS6JQ&ust=1407385641897682

  • 8/16/2019 Arrays Week 9

    6/34

    Need of Arrays

    • Without Arraysint x1 = 5;

    int x2 = 10;

    //AND SO ON !!

    int x500 = 2500;

    • With Arrays

    int[] x = new int[500];for(int i=0; i

  • 8/16/2019 Arrays Week 9

    7/34

    Array Declaration

    • SyntaxdataType[ ] arrayName;

    • Examples  – • int[ ] ages;

    • float[ ] salaryList;

    7

    http://www.google.com.au/url?sa=i&rct=j&q=&esrc=s&frm=1&source=images&cd=&cad=rja&uact=8&docid=QB6L6ZLlUr8KrM&tbnid=hTJ6ORDCFTb7_M:&ved=0CAUQjRw&url=http://omundodaprogramacao.com/tag/windows/&ei=t67hU8XBDI3d8AWcw4L4DQ&bvm=bv.72197243,d.dGc&psig=AFQjCNF4xP18VW2_lbRIVXpeQEvqKTS6JQ&ust=1407385641897682

  • 8/16/2019 Arrays Week 9

    8/34

    Array Instantiation

    • You need to allocate memory for the items in

    the array before you can use it.

    arrayName = new dataType[size];

    • Examples – 

    ages = new int[5];

    wages = new double[24];

    8

    http://www.google.com.au/url?sa=i&rct=j&q=&esrc=s&frm=1&source=images&cd=&cad=rja&uact=8&docid=QB6L6ZLlUr8KrM&tbnid=hTJ6ORDCFTb7_M:&ved=0CAUQjRw&url=http://omundodaprogramacao.com/tag/windows/&ei=t67hU8XBDI3d8AWcw4L4DQ&bvm=bv.72197243,d.dGc&psig=AFQjCNF4xP18VW2_lbRIVXpeQEvqKTS6JQ&ust=1407385641897682

  • 8/16/2019 Arrays Week 9

    9/34

    Instantiation Implications

    • int[ ] ages;

    • ages = new int[5];

    ages

    ages[0] = 0

    ages[1] = 0

    ages[2] = 0

    ages[3] = 0

    ages[4] = 0

    9

    http://www.google.com.au/url?sa=i&rct=j&q=&esrc=s&frm=1&source=images&cd=&cad=rja&uact=8&docid=QB6L6ZLlUr8KrM&tbnid=hTJ6ORDCFTb7_M:&ved=0CAUQjRw&url=http://omundodaprogramacao.com/tag/windows/&ei=t67hU8XBDI3d8AWcw4L4DQ&bvm=bv.72197243,d.dGc&psig=AFQjCNF4xP18VW2_lbRIVXpeQEvqKTS6JQ&ust=1407385641897682

  • 8/16/2019 Arrays Week 9

    10/34

    Declaration and Instantiation 

    • dataType[] arrayName = new dataType[size];

    • Examples – • int[ ] ages= new int[50];

    • double[ ] wages = new double[24];

    10

    http://www.google.com.au/url?sa=i&rct=j&q=&esrc=s&frm=1&source=images&cd=&cad=rja&uact=8&docid=QB6L6ZLlUr8KrM&tbnid=hTJ6ORDCFTb7_M:&ved=0CAUQjRw&url=http://omundodaprogramacao.com/tag/windows/&ei=t67hU8XBDI3d8AWcw4L4DQ&bvm=bv.72197243,d.dGc&psig=AFQjCNF4xP18VW2_lbRIVXpeQEvqKTS6JQ&ust=1407385641897682http://www.google.com.au/url?sa=i&rct=j&q=&esrc=s&frm=1&source=images&cd=&cad=rja&uact=8&docid=QB6L6ZLlUr8KrM&tbnid=hTJ6ORDCFTb7_M:&ved=0CAUQjRw&url=http://omundodaprogramacao.com/tag/windows/&ei=t67hU8XBDI3d8AWcw4L4DQ&bvm=bv.72197243,d.dGc&psig=AFQjCNF4xP18VW2_lbRIVXpeQEvqKTS6JQ&ust=1407385641897682

  • 8/16/2019 Arrays Week 9

    11/34

    Sample Questions

    • Declaration int[] ages;

    • Instantiation ages = new int[5];

    • Declare, instantiate arrays for following requirements.

    • Draw the memory diagram.

    a. 10 integers

    b. 5 floating point values

    c. 8 charactersd. 4 booleans

    11

    ages

    ages[0] = 0

    ages[1] = 0

    ages[2] = 0

    ages[3] = 0

    ages[4] = 0

    http://www.google.com.au/url?sa=i&rct=j&q=&esrc=s&frm=1&source=images&cd=&cad=rja&uact=8&docid=QB6L6ZLlUr8KrM&tbnid=hTJ6ORDCFTb7_M:&ved=0CAUQjRw&url=http://omundodaprogramacao.com/tag/windows/&ei=t67hU8XBDI3d8AWcw4L4DQ&bvm=bv.72197243,d.dGc&psig=AFQjCNF4xP18VW2_lbRIVXpeQEvqKTS6JQ&ust=1407385641897682

  • 8/16/2019 Arrays Week 9

    12/34

    Solutions – Sample Questions

    a. 10 integers:• int[ ] intArr = new int[10];

    b. 5 floats:

    • float[ ] floatArr = new float[5];

    c. 8 chars:• char[ ] charArr = new char[8];

    d. 4 booleans:

    • boolean[ ] boolArr = new boolean[4];

    12

    http://www.google.com.au/url?sa=i&rct=j&q=&esrc=s&frm=1&source=images&cd=&cad=rja&uact=8&docid=QB6L6ZLlUr8KrM&tbnid=hTJ6ORDCFTb7_M:&ved=0CAUQjRw&url=http://omundodaprogramacao.com/tag/windows/&ei=t67hU8XBDI3d8AWcw4L4DQ&bvm=bv.72197243,d.dGc&psig=AFQjCNF4xP18VW2_lbRIVXpeQEvqKTS6JQ&ust=1407385641897682

  • 8/16/2019 Arrays Week 9

    13/34

    Getting number of items

    • arrayName.length

    • Example, – int [ ] ageList = new int [6];

     – println(ageList.length); //6

    13

    http://www.google.com.au/url?sa=i&rct=j&q=&esrc=s&frm=1&source=images&cd=&cad=rja&uact=8&docid=QB6L6ZLlUr8KrM&tbnid=hTJ6ORDCFTb7_M:&ved=0CAUQjRw&url=http://omundodaprogramacao.com/tag/windows/&ei=t67hU8XBDI3d8AWcw4L4DQ&bvm=bv.72197243,d.dGc&psig=AFQjCNF4xP18VW2_lbRIVXpeQEvqKTS6JQ&ust=1407385641897682

  • 8/16/2019 Arrays Week 9

    14/34

    Accessing Array Items

    • float[ ] arr  = new float[5];

    •  Access item at index 0 arr[0]

    •  Access item at index 1 arr[1]

    •  Access item at index 2 arr[2]•  Access item at index 3 arr[3]

    •  Access item at index 4 arr[4]

    • ---------------------------•  Access item at index i arr[i]• for i from 0 to arr.length - 1

    14

    http://www.google.com.au/url?sa=i&rct=j&q=&esrc=s&frm=1&source=images&cd=&cad=rja&uact=8&docid=QB6L6ZLlUr8KrM&tbnid=hTJ6ORDCFTb7_M:&ved=0CAUQjRw&url=http://omundodaprogramacao.com/tag/windows/&ei=t67hU8XBDI3d8AWcw4L4DQ&bvm=bv.72197243,d.dGc&psig=AFQjCNF4xP18VW2_lbRIVXpeQEvqKTS6JQ&ust=1407385641897682

  • 8/16/2019 Arrays Week 9

    15/34

    Accessing Array Items

    • //for each item of the array

    • for(int i=0; i

  • 8/16/2019 Arrays Week 9

    16/34

    Initialization by Iteration

    • float[] arr = new float[30];

    • //for each item of the array

    • for(int i=0; i

  • 8/16/2019 Arrays Week 9

    17/34

    Explicit Initialization

    • When you already know the values you want

    inside the array, and the number of items is

    not too large – • dataType[] arrayName = {val1, val2, … }; 

    • Example – 

    • int[] gradeBounds = {50, 65, 75, 85};• char [ ] lowerCaseVowels = {'a’,'e’,'i’,'o’,'u'};

    • float[ ] marathonDistances = {21.0975, 42.195};

    17

    http://www.google.com.au/url?sa=i&rct=j&q=&esrc=s&frm=1&source=images&cd=&cad=rja&uact=8&docid=QB6L6ZLlUr8KrM&tbnid=hTJ6ORDCFTb7_M:&ved=0CAUQjRw&url=http://omundodaprogramacao.com/tag/windows/&ei=t67hU8XBDI3d8AWcw4L4DQ&bvm=bv.72197243,d.dGc&psig=AFQjCNF4xP18VW2_lbRIVXpeQEvqKTS6JQ&ust=1407385641897682

  • 8/16/2019 Arrays Week 9

    18/34

    Display by Iteration

    • //for each item of the array

    • for(int i=0; i

  • 8/16/2019 Arrays Week 9

    19/34

    Operating on Array Items

    float[] GPAs = new float[10];

    float total= 0, avgGPA; 

    for(int i=0; i

  • 8/16/2019 Arrays Week 9

    20/34

    Class Exercise

    • int[] arr = new int[10];

    for(int i=0; i

  • 8/16/2019 Arrays Week 9

    21/34

    Class Exercise

    • int[] arr = new int[10];

    • for(int i=0; i

  • 8/16/2019 Arrays Week 9

    22/34

    Problem Solving

    • Create an array that contains 100 integers

    such that the

    •   first item is 0

    •   second item is 5

    •   third item is 0

    •   fourth item is 5• And so on… 

    22

    http://www.google.com.au/url?sa=i&rct=j&q=&esrc=s&frm=1&source=images&cd=&cad=rja&uact=8&docid=QB6L6ZLlUr8KrM&tbnid=hTJ6ORDCFTb7_M:&ved=0CAUQjRw&url=http://omundodaprogramacao.com/tag/windows/&ei=t67hU8XBDI3d8AWcw4L4DQ&bvm=bv.72197243,d.dGc&psig=AFQjCNF4xP18VW2_lbRIVXpeQEvqKTS6JQ&ust=1407385641897682

  • 8/16/2019 Arrays Week 9

    23/34

    Problem solving

    • First, creating an array to hold 100 integers

    int[] arr = new int[100];

    • arr [0] should be 0•   arr [1] should be 5

    • arr [2] should be 0

    •   arr [3] should be 5• arr [4] should be 0

    •   arr [5] should be 5

    23

    http://www.google.com.au/url?sa=i&rct=j&q=&esrc=s&frm=1&source=images&cd=&cad=rja&uact=8&docid=QB6L6ZLlUr8KrM&tbnid=hTJ6ORDCFTb7_M:&ved=0CAUQjRw&url=http://omundodaprogramacao.com/tag/windows/&ei=t67hU8XBDI3d8AWcw4L4DQ&bvm=bv.72197243,d.dGc&psig=AFQjCNF4xP18VW2_lbRIVXpeQEvqKTS6JQ&ust=1407385641897682

  • 8/16/2019 Arrays Week 9

    24/34

    Problem solving

    • even indexed values are 0, odd indexed values are 5

    • declare and allocate memory for array arr to store 100ints

    • for each item indexed by i (i = 0 to arr.length-1)•   if i is even

    •   arr[i] = 0

    •   else

    •   arr[i] = 5

    •   end of if

    • end of for

    24

    http://www.google.com.au/url?sa=i&rct=j&q=&esrc=s&frm=1&source=images&cd=&cad=rja&uact=8&docid=QB6L6ZLlUr8KrM&tbnid=hTJ6ORDCFTb7_M:&ved=0CAUQjRw&url=http://omundodaprogramacao.com/tag/windows/&ei=t67hU8XBDI3d8AWcw4L4DQ&bvm=bv.72197243,d.dGc&psig=AFQjCNF4xP18VW2_lbRIVXpeQEvqKTS6JQ&ust=1407385641897682

  • 8/16/2019 Arrays Week 9

    25/34

    Problem solving

    • even indexed values are 0, odd indexed values are 5

    • int[] arr = new int[100];

    • for(int i = 0; i < arr.length; i++) {

    •   if ( i % 2 == 0 ) {•   arr[i] = 0;

    •   }

    •   else {

    •   arr[i] = 5;•   }

    • } 

    25

    http://www.google.com.au/url?sa=i&rct=j&q=&esrc=s&frm=1&source=images&cd=&cad=rja&uact=8&docid=QB6L6ZLlUr8KrM&tbnid=hTJ6ORDCFTb7_M:&ved=0CAUQjRw&url=http://omundodaprogramacao.com/tag/windows/&ei=t67hU8XBDI3d8AWcw4L4DQ&bvm=bv.72197243,d.dGc&psig=AFQjCNF4xP18VW2_lbRIVXpeQEvqKTS6JQ&ust=1407385641897682

  • 8/16/2019 Arrays Week 9

    26/34

    Class Exercise

    • Assuming the following array

    int[ ] nums = {5,4,2,7,6,8,5,2,8,14};

    Write a code to square each number in array

    ((i.e., multiply each by itself)

    • for (int i = 0; i < nums.length; i++) {

    nums[i] = nums[i]*nums[i];} 

    http://www.google.com.au/url?sa=i&rct=j&q=&esrc=s&frm=1&source=images&cd=&cad=rja&uact=8&docid=QB6L6ZLlUr8KrM&tbnid=hTJ6ORDCFTb7_M:&ved=0CAUQjRw&url=http://omundodaprogramacao.com/tag/windows/&ei=t67hU8XBDI3d8AWcw4L4DQ&bvm=bv.72197243,d.dGc&psig=AFQjCNF4xP18VW2_lbRIVXpeQEvqKTS6JQ&ust=1407385641897682

  • 8/16/2019 Arrays Week 9

    27/34

    Class Exercise

    • Assuming the following array

    int[ ] nums = {5,4,2,7,6,8,5,2,8,14};

    Write a code to calculate the sum of all the

    numbers.

    • int sum = 0;

    for (int i = 0; i < nums.length; i++) {

    sum += nums[i];}

    http://www.google.com.au/url?sa=i&rct=j&q=&esrc=s&frm=1&source=images&cd=&cad=rja&uact=8&docid=QB6L6ZLlUr8KrM&tbnid=hTJ6ORDCFTb7_M:&ved=0CAUQjRw&url=http://omundodaprogramacao.com/tag/windows/&ei=t67hU8XBDI3d8AWcw4L4DQ&bvm=bv.72197243,d.dGc&psig=AFQjCNF4xP18VW2_lbRIVXpeQEvqKTS6JQ&ust=1407385641897682

  • 8/16/2019 Arrays Week 9

    28/34

    Class Exercise

    • Assuming the following array

    int[ ] nums = {5,4,2,7,6,8,5,2,8,14};

    Write a code add a random number between

    zero and 10 to each number.

    • for (int i = 0; i < nums.length; i++) {

    nums[i] += int(random(10));

    }

    http://www.google.com.au/url?sa=i&rct=j&q=&esrc=s&frm=1&source=images&cd=&cad=rja&uact=8&docid=QB6L6ZLlUr8KrM&tbnid=hTJ6ORDCFTb7_M:&ved=0CAUQjRw&url=http://omundodaprogramacao.com/tag/windows/&ei=t67hU8XBDI3d8AWcw4L4DQ&bvm=bv.72197243,d.dGc&psig=AFQjCNF4xP18VW2_lbRIVXpeQEvqKTS6JQ&ust=1407385641897682

  • 8/16/2019 Arrays Week 9

    29/34

    Class Exercise

    • Assuming the following array

    int[ ] nums = {5,4,2,7,6,8,5,2,8,14};

    Write a code to add to each number the

    number that follows in the array. Skip the lastvalue in the array.

    • for (int i = 0; i < nums.length-1; i++) {

    nums[i] += nums[i+1];}

    http://www.google.com.au/url?sa=i&rct=j&q=&esrc=s&frm=1&source=images&cd=&cad=rja&uact=8&docid=QB6L6ZLlUr8KrM&tbnid=hTJ6ORDCFTb7_M:&ved=0CAUQjRw&url=http://omundodaprogramacao.com/tag/windows/&ei=t67hU8XBDI3d8AWcw4L4DQ&bvm=bv.72197243,d.dGc&psig=AFQjCNF4xP18VW2_lbRIVXpeQEvqKTS6JQ&ust=1407385641897682http://www.google.com.au/url?sa=i&rct=j&q=&esrc=s&frm=1&source=images&cd=&cad=rja&uact=8&docid=QB6L6ZLlUr8KrM&tbnid=hTJ6ORDCFTb7_M:&ved=0CAUQjRw&url=http://omundodaprogramacao.com/tag/windows/&ei=t67hU8XBDI3d8AWcw4L4DQ&bvm=bv.72197243,d.dGc&psig=AFQjCNF4xP18VW2_lbRIVXpeQEvqKTS6JQ&ust=1407385641897682

  • 8/16/2019 Arrays Week 9

    30/34

    Passing array to functions

    void initialize(int[] a, int low, int high) {

    for(int i=0; i

  • 8/16/2019 Arrays Week 9

    31/34

    Passing Array to Functions

    • So, when you pass an array (say A) to a

    function as array (B), and modify B, A is

    modified too.

    • This is, because, unlike individual

    variables, a duplicate of an array is NOT

    made and the formal parameter (B) is

    physically the same as A

    31

    http://www.google.com.au/url?sa=i&rct=j&q=&esrc=s&frm=1&source=images&cd=&cad=rja&uact=8&docid=QB6L6ZLlUr8KrM&tbnid=hTJ6ORDCFTb7_M:&ved=0CAUQjRw&url=http://omundodaprogramacao.com/tag/windows/&ei=t67hU8XBDI3d8AWcw4L4DQ&bvm=bv.72197243,d.dGc&psig=AFQjCNF4xP18VW2_lbRIVXpeQEvqKTS6JQ&ust=1407385641897682http://www.google.com.au/url?sa=i&rct=j&q=&esrc=s&frm=1&source=images&cd=&cad=rja&uact=8&docid=QB6L6ZLlUr8KrM&tbnid=hTJ6ORDCFTb7_M:&ved=0CAUQjRw&url=http://omundodaprogramacao.com/tag/windows/&ei=t67hU8XBDI3d8AWcw4L4DQ&bvm=bv.72197243,d.dGc&psig=AFQjCNF4xP18VW2_lbRIVXpeQEvqKTS6JQ&ust=1407385641897682

  • 8/16/2019 Arrays Week 9

    32/34

    Passing Array to Functions

    A = 10

    B = 10

    10

    20

    30

    A and B being

    individual variables

    A and B being

    arrays

    A,B

    32

    http://www.google.com.au/url?sa=i&rct=j&q=&esrc=s&frm=1&source=images&cd=&cad=rja&uact=8&docid=QB6L6ZLlUr8KrM&tbnid=hTJ6ORDCFTb7_M:&ved=0CAUQjRw&url=http://omundodaprogramacao.com/tag/windows/&ei=t67hU8XBDI3d8AWcw4L4DQ&bvm=bv.72197243,d.dGc&psig=AFQjCNF4xP18VW2_lbRIVXpeQEvqKTS6JQ&ust=1407385641897682http://www.google.com.au/url?sa=i&rct=j&q=&esrc=s&frm=1&source=images&cd=&cad=rja&uact=8&docid=QB6L6ZLlUr8KrM&tbnid=hTJ6ORDCFTb7_M:&ved=0CAUQjRw&url=http://omundodaprogramacao.com/tag/windows/&ei=t67hU8XBDI3d8AWcw4L4DQ&bvm=bv.72197243,d.dGc&psig=AFQjCNF4xP18VW2_lbRIVXpeQEvqKTS6JQ&ust=1407385641897682

  • 8/16/2019 Arrays Week 9

    33/34

    Declaring Multiple Arrays

    •   float[] x, y;

    •   x = new float[4];

    •   y = new float[4]; //Error

    • Because the declaration code is parsed as:

    •   float[] x;

    •   float y;

    • You need to declare each array individually as:

    •   float[] x;

    •   float[] y;

    33

    http://www.google.com.au/url?sa=i&rct=j&q=&esrc=s&frm=1&source=images&cd=&cad=rja&uact=8&docid=QB6L6ZLlUr8KrM&tbnid=hTJ6ORDCFTb7_M:&ved=0CAUQjRw&url=http://omundodaprogramacao.com/tag/windows/&ei=t67hU8XBDI3d8AWcw4L4DQ&bvm=bv.72197243,d.dGc&psig=AFQjCNF4xP18VW2_lbRIVXpeQEvqKTS6JQ&ust=1407385641897682

  • 8/16/2019 Arrays Week 9

    34/34