Friday July 21, 2000


Quiz 27


1. What is the syntax used to declare a new array?

2. What is the syntax used to initialize an array when it is declared?

3. How do you reference the first element in an array of size n?

4. How do you reference the last element in an array of size n?




























There is a correction that needs to be made to yesterday's lecture. There was a mistake in terminology about the sorting technique described.

The technique that we discussed in class yesterday is called the Selection Sort.

Both of these algorithms are very inefficient because it takes about n2 comparisons to sort the array.

There are other sorting techniques that are much more efficient. One is known as the QuickSort. We will not discuss the details of this method this method. However, you should remember that the QuickSort takes about n*log n comparisons to sort an array in the best case.

The terminology used is that the Bubble Sort is an O(N2) sorting algorithm, and in the best case a QuickSort is O(N log N).

Once an array is sorted searching for items in the array is then made easier.

A very simple technique for finding an element in an array is to start with the first element in the array and search until you find the element you're looking for. Even though this is a simple technique it is very inefficient because in an array of size n you are going to have to look at n/2 elements on average.

There is a much more effective technique that can be used to search for an element in an array once its sorted.

Let's consider the following problem.

Suppose you have an array of 1000 names that are sorted and someone asks you to find a particular name in the list. How would you do it?

We may also use arrays with more than one dimension.

In order to declare an array with two dimensions we would use the following syntax.

data-type[][] arrayname = new data-type[rows][columns];

Your programming assignment

Your final program will involve two parts. You will prompt the user for which part they want to execute.

The first option that you will give the user is to have you sort an array of randomly generated ints between 0 and a certain number. They will input how many numbers you should generate and what the maximum number to be generated is. For example, they might ask you to produce 20 random ints between 0 and 500. You will sort the array and determine the following information.

The minimum number generated.

The maximum number generated.

The number of prime numbers generated.

The number of even numbers generated.

The number of odd numbers generated.

The minimum prime number generated.

The maximum prime number generated.

The sum of all prime numbers generated.

You may produce random numbers using the Math.random() method. It returns a double between 0.0 and 1.0. So to produce a random number between 0 and a certain number, you would multiply the result of Math.random() by that number and cast it as an int.

The second option that you will present to the user will be for you to produce a table of logarithms for them. The table will be stored in a two-dimensional array.

The rows in the array will represent tenths of numbers, that is, the first row will represent 1.0, the second row will represent 1.1, the third row will represent 1.2, etc. The last row in the array will represent 10.0.

The columns in the array will represent hundreths of numbers up to 0.09. That is the columns will represent the numbers 0.00, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, and 0.09.

The user will input what base to use, and each entry in the array will be the logarithm to that base of the sum of the numbers represented by the row and column.

For example, the first entry in the table will the logarithm to the base of the input number of 1.0 + 0.00.

The logarithms can be computed using the Math.log() method and the change of base formula



When the logarithms are produced, truncate them to 3 decimal places.

Then prompt the user for a filename to use and write the table to that file.