CSIS 110 Lecture 35: Arrays and Methods - Prof. Brian F. Hanks, Study notes of Javascript programming

The topic of arrays and methods in csis 110. It explains how to declare formal parameters as arrays in methods, the concept of array size and assignment, and the importance of sorting. The document also includes examples of methods like doit, arrayzero, arrayassign, arrayassign2, and selectionsort. Students are asked to write a program with methods backwardsarray, add5, printarray, and main.

Typology: Study notes

Pre 2010

Uploaded on 08/05/2009

koofers-user-ztv
koofers-user-ztv ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSIS 110 โ€“ Lecture 35
Chapter 8 - Arrays
Reading: 8.1 to 8.2.3 (pages 381 โ€“ 389)
8.4 to 8.6 (pages 396 โ€“ 417)
Arrays and Methods
Last time we looked at passing arrays to methods. Let's say that I have a method named
doIt - how would I declare a formal parameter that is an array of int?
We looked at methods that
- calculated the sum of the elements of an array
- printed the elements of an array
- returned the minimum value contained in an array
- returned the index of the array element that contains the smallest value
- Searched for a value in an array, and returned the index of the first element that
contained that value.
Array size
Before continuing with arrays and methods, let's revisit the array size.
When you allocate the array, its size is fixed - you can't make it smaller or larger.
However, you can reassign the array reference to a different array.
Look at ArrayAssign.java.
Note that I have changed the reference, and that the elements of a1 are not longer
referenced - garbage collected.
Now let's look at ArrayAssign2.java - the assignment statement does not copy the values
of the array - it only changes the reference. So when I modify a2[2], it appears to modify
a1[2] also, because both a1 and a2 are references to the same array.
Assigning values to array elements in methods
So far, these methods have only accessed the elements of the arrays, but they have not
made any assignments to the arrays or the array elements. Let's look at another method.
Look at ArrayZero.java.
What's going on? Draw pictures. Emphasize that arrays are reference types.
Let's look at another example: ArrayCopy.java.
pf3

Partial preview of the text

Download CSIS 110 Lecture 35: Arrays and Methods - Prof. Brian F. Hanks and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 โ€“ Lecture 35

Chapter 8 - Arrays Reading: 8.1 to 8.2.3 (pages 381 โ€“ 389) 8.4 to 8.6 (pages 396 โ€“ 417) Arrays and Methods Last time we looked at passing arrays to methods. Let's say that I have a method named doIt - how would I declare a formal parameter that is an array of int? We looked at methods that

  • calculated the sum of the elements of an array
  • printed the elements of an array
  • returned the minimum value contained in an array
  • returned the index of the array element that contains the smallest value
  • Searched for a value in an array, and returned the index of the first element that contained that value. Array size Before continuing with arrays and methods, let's revisit the array size. When you allocate the array, its size is fixed - you can't make it smaller or larger. However, you can reassign the array reference to a different array. Look at ArrayAssign.java. Note that I have changed the reference, and that the elements of a1 are not longer referenced - garbage collected. Now let's look at ArrayAssign2.java - the assignment statement does not copy the values of the array - it only changes the reference. So when I modify a2[2], it appears to modify a1[2] also, because both a1 and a2 are references to the same array. Assigning values to array elements in methods So far, these methods have only accessed the elements of the arrays, but they have not made any assignments to the arrays or the array elements. Let's look at another method. Look at ArrayZero.java. What's going on? Draw pictures. Emphasize that arrays are reference types. Let's look at another example: ArrayCopy.java.

Sorting Sorting is a very important task in computer programs. Why? Well, think about a dictionary or a phone directory - if the words were in some arbitrary order, it would be much more difficult to find them. You would just have to start at the beginning and search through the listings until you found the one you wanted. Because the data is sorted, you can find what you are looking for much more efficiently. Also, if the item you are searching for is not contained in the dictionary, you can find out that it doesn't exist much more quickly - if the words were not sorted, you would have to examine every word in the dictionary to determine that the word you wanted did not exist! Similar efficiency issues exist with computers - if we keep the data in sorted order, then it is much easier to find values that we are looking for. There are many different sorting algorithms that work well in different situations. We are going to look at one called selection sort. Selection sort is an interative process:

  • On the first iteration, it switches the first value in the list with the smallest value
  • On the second iteration, it switches the second value in the list with the second smallest value,
  • etc. Let's look at an example Initial list of ints: 10, 3, 7, 2, 5 1 st^ iteration - switch 10 with 2 --> 2, 3, 7, 10, 5 2 nd^ iteration - switch 3 with itself --> 2, 3, 7, 10, 5 3 rd^ iteration - switch 7 with 5 --> 2, 3, 5, 10, 7 4 th^ iteration - switch 10 with 7 --> 2, 3, 5, 7, 10 Done - don't need to check last element, it is the largest element in the list So, let's look at a method that does this: See SelectionSort.java Lab (Lab20.doc) Write a program that uses arrays. Your program should have the following methods:
  1. backwardsArray โ€“ this method has one formal parameter: an array of int a. The method should set the element values so that the first element's value is the size of the array, the second element's value is the size-1, ... and the last element's value is 1. For example, if the size of the array passed to the method is 4, then the elements should be set to 4, 3, 2, 1 in that order.
  2. add5 โ€“ this method has one formal parameter: an array of int