CSCI 235 Lab 3 Arrays

After you have a partner, one of you should log in on his or her own account. Launch a web browser (e.g., Firefox) and view the online version of this lab so that you will have access to the hints.

Do not look at the hints until and unless you need them. The hints get more specific along the way, so if you don’t know where to start, hint 1 will give you a few questions to think about that will hopefully push you in the right direction; hint 2 will suggest a direction to try but won’t give any details; hint 3 will clarify hint 2; you get the idea.

1. Introduction

The goal of this lab is to practice creating and using basic arrays.

While you write this program, practice the documentation procedures you have seen in class. In the future, properly documenting your code will be part of your grade. In brief, begin each file with a header like this:

/**  
 * (filename)  
 *  
 * (One or two-sentence description of the program)  
 *  
 * @author (your name(s) )  
 * Wheaton College, CSCI 235, Spring 2016  
 * Lab 3  
 * 29 Jan 2016  
 */

…and also write a line comment by each variable declaration describing what it is for.

Start out by opening a terminal. Then copy the directory with the starting file, and then move into it.

$ cp -r /cslab/class/csci235/labs/lab3 lab3
$ cd lab3

2. The Sieve of Eratosthenes

The method

You should already be familiar with the problem from the prep assignment, but that is repeated here for your convenience.

The Sieve of Eratosthenes is a method for finding prime numbers by finding all of the primes up to a limit. Begin by listing all the integers from 2 up to the limit (using 20 as an example).

2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20

Then cross off all the multiples of 2.

2  3 \\4  5 \\6  7 \\8  9 \\10 11 \\12 13 \\14 15 \\16 17 \\18 19 \\20

The next uncrossed number (in this case 3) is also the next prime number. Cross off every 3rd number that’s not already crossed off.

2  3 \\4  5 \\6  7 \\8 \\9 \\10 11 \\12 13 \\14 \\15 \\16 17 \\18 19 \\20

Repeat this process. Find the next uncrossed number (say, n) and cross off every nth number after that. Continue until you reach the end of the list.

Setting up

To save you some time, you’ve been provided with a start in the file Sieve.java. Open that in emacs and fill in the opening documentation.

The template provided includes a declaration of a named constant SIEVE_LIMIT. This looks very much like a variable declaration and initialization, but you should notice a few features of this declaration:

Write your program to print all primes up through SIEVE_LIMIT. Then you have only one place to change if you want to run for a different size (which you might want to do when you are testing…)

Hints

Hint 1 is very general. Hint 1.

Hints 2–4 talk about how to store the data. Hint 2. Hint 3. Hint 4.

Hints 5 and 6 talk about how you process the data to find the answer. Hint 5. Hint 6.

3. Turn in

Prepare a typescript that shows your program running.

Then hand in your source file as lab3. You do not need to hand in a printed copy.

Copy your work to the account of the partner who is not logged in.