Lab 3-2 Arrays

1. Reversing a List of Numbers

Let us write a program that reads an integer size specifying the length of a list of numbers. The program then asks the user to enter that many number, reads them in, and stores them in an array. Finally, the program prints the list of numbers out in reverse order.

Here is a sample interaction:

How many numbers are in your list? 5
Enter the list of numbers separated by blanks and 
then press ENTER
12 8 32 3 -12
You entered the list 12 8 32 3 -12 
The reversed list is -12 3 32 8 12 

How can we write such a program? We begin by making a plan in pseudocode:

        // variables needed 
        Scanner sc = new Scanner(System.in);   // scanner 
        int [] numbers;                        // array of numbers to be read in
        int size;                              // size of list to be reversed
        int number;                            // used to read in a single number

        Create an array to hold the list of numbers       

        Read the list of numbers       

        Confirm list of numbers entered       

        Print list of numbers in reverse      

Once we have a plan, we can fill in the details. Notice that we are using nextInt() to read in integers from the keyboard. Here is the entire program.


/**
 * This program reads in a list of integers and
 * prints the list out in reverse.
 *
 */
package arrayexample;
import java.util.*;
public class Main
{   
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int [] numbers;  // array of numbers to be read in
        int size;        // size of list to be reversed
        int number;      // used to read in a single number

        // Create an array to hold the list of numbers
        System.out.print("How many numbers are in your list? ");
        size = sc.nextInt();
        numbers = new int[size];

        // Read the list of numbers
        System.out.println("Enter the list of numbers separated by blanks and ");
        System.out.println("then press ENTER");
        for (int k = 0; k < numbers.length; k++)
            numbers[k] = sc.nextInt();

        // Confirm list of numbers entered
        System.out.print("You entered the list ");
        for (int k = 0; k < numbers.length; k++)
            System.out.print(numbers[k] + " ");

        System.out.println();

        // Print list of numbers in reverse
        System.out.print("The reversed list is ");
        for (int k = numbers.length-1; k >= 0; k--)
            System.out.print(numbers[k] + " ");

        System.out.println();
    }
}

Run this program. Study it and make sure you understand it.

2. Try this: Doubling up

Modify the program so that instead of printing the array in reverse, it replaces each component of the array with twice its value. For example, if numbers[0] was 17, it will be set to 34. The program prints out this new array with a suitable caption

The list with twice each number is ...

Try this: Constant arrays

Modify the program so that after the list of numbers is read in, the program determines whether all the numbers in the list are the same. For example, if the list is

13 23 13 13

The program prints

These numbers are not all the same.

But if the list is

13 13 13 13

The program prints

All the numbers are the same.

Does your program work if the list has length 1?

3. Try this: Increasing sequence

Modify the program so it recognises increasing sequences, where each number in the list is strictly greater than its predecessor. For example, if the list is

12 45 78 90

The program prints

This is an increasing sequence.

But if the sequence is

12  12 45 6

The program prints

This is not an increasing sequence.

Does your program work if the list has length 1?

4. Missile Launch Code Verification

The country of Sluddobiva has recently acquired nuclear missile technology. The Sluddobivan people are anxious that their missiles should only be used when absolutely necessary to defend their country against aggression. The missile system is controlled by a computer. The Sluddobivan Department of Defense has hired you to program the computer to ensure that the missiles will be cleared for launch only when the Sluddobivan president and vice president independently enter a secret sequence of numbers. The computer system is to clear the missiles for lauch only if the sequence of numbers entered in by the president matches exactly the sequence entered in by the vice president. Here is a sample interaction

Numbers in launch code sequence should be entered on 
single line, separated by blanks.

Enter length of launch code sequence: 4
Mr. President, Enter the launch code sequence: 
34 56 2 89

Mr. Vice President, Enter the launch code sequence: 
34 56 21 89

Codes do not check out. Abort missile launch.

Here is a second example of how the program should work

Numbers in launch code sequence should be entered on 
single line, separated by blanks.

Enter length of launch code sequence: 5
Mr. President, Enter the launch code sequence: 
13 67 3 2 67

Mr. Vice President, Enter the launch code sequence: 
13 67 3 2 67

All equal: Missile system cleared for launch.

Write a program that will meet the needs of the Sluddobivan Department of Defense. Make sure your program works correctly: you do not want your program to to mistakenly cause a nuclear Armageddon.

Submit

Submit the program for Missile Launch Code Verification. This is due Friday of Week 3 at midnight.