Tuesday, 16 December 2014

// The "Arrays2" class.
// Sample array program first written in Turing,
// then converted to Java.

// This program creates an initialized list of prime numbers (the first 10).
// It then reads them off, in order, and then reads them off again, this time
// in reverse order, from the 'primes' array.

public class Arrays2
{
    public static void main (String[] args)
    {
        // MAIN PROGRAM IN TURING:
        // create an initialized array called 'primes' of the first 10 integers
        // that are prime numbers:
        // % var primes:  array 1..10 of int := init(2,3,5,7,11,13,17,18,23,29)
        // now, create the same 'primes' array in Java:
        int[] primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
        // read the prime numbers off, in order, from 'primes':
        System.out.println ("Here are the primes, in order: ");
        for (int i = 0 ; i <= 9 ; i++)
        {
            System.out.println (primes [i]);
        } // end of for loop # 1

        System.out.println ();
        System.out.println ("Now, here are the primes, in reverse order: ");
        // now, read the same numbers off, in reverse order, from 'primes':
        for (int j = 9 ; j >= 0 ; j--)
        {
            System.out.println (primes [j]);
        } // end of for loop # 2

    } // main method
} // Arrays2 class

No comments:

Post a Comment