HOME | Overview | Syllabus | Instructor | Lectures | Laboratories | Readings | Assignments | Resources | Other Links
CS 1711 Recitation Laboratory 8:
Arrays [11/2/99; 11/4/99]

Objectives:

Check-off procedure:

You must attend the recitation and sign in. This laboratory will be checked off using the new procedures.

Laboratory details:

A prime number is a positive integer that is only divisible by itself and by 1. In this laboratory you will implement a Primes class using a sieve. The Primes class has a constructor that takes a single integer parameter, upper, specifying the upper bound of the numbers to be considered. The numbers in [0, upper] are in the range of the sieve. The class has accessor methods to:

There are many ways to calculate primes. In this laboratory is you will implement a sieve to practice with arrays. To analyze the distribution of prime numbers between 1 and upper, you will create a boolean array of length upper + 1 (the sieve) and initialize all of the entries to true. For each integer between 2 and the square root of upper, you will mark entries corresponding to the multiples of that integer to false. When you are finished the sieve will contain true only at entries which were not multiples of other integers (e.g. primes). For example, when upper is 15, the array starts out:

   Entry:  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
   Value:  T  T  T  T  T  T  T  T  T  T  T  T  T  T  T  T

After multiples of 2 are taken out, the array is:

   Entry:  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 
   Value:  T  T  T  T  F  T  F  T  F  T  F  T  F  T  F  T 

After multiples of 3 are taken out, the array is:

 
   Entry:  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15  
   Value:  T  T  T  T  F  T  F  T  F  F  F  T  F  T  F  F  

Handling 1 and 0, we are done:

 
   Entry:  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15   
   Value:  F  F  T  T  F  T  F  T  F  F  F  T  F  T  F  F   

  • Exercise I: The basic sieve.

  • Exercise II: Resizing the sieve

    In this section you will add a modifier method called resize that takes a single integer parameter newsize. If newsize is is equal to the current value of myUpper, don't do anything. If newsize is not equal to the current value of myUpper, adjust the fields of Primes to handle primes in [0, newsize].

  • Exercise III: Automatic resizing

    The accessor methods for Primes only work if the value is less than myUpper. Add code to the method that indicates whether or not n is prime to resize the sieve appropriately if n > myUpper. Hint: use initializeSieve. Is this method still just an accessor? The other accessor methods of Primes could be modified in a similar way, but this is not required for the assignment.


    Last revision: October 31, 1999 at 5:40 am.