Wednesday, 17 December 2014

// The "TablesAndArraysExample4B" class.  Page 311.
// (from the Carter book)

// Example 4 (B):

// Suppose that we wanted to store the sequence with terms, as follows:
//
//  t1 = 1/2, t2 = 1/4,....t5 = 1/32
//
// in an array of double values.  We could create and initialize such an
// array by writing the following:
//
public class TablesAndArraysExample4B
{
    public static void main (String[] args)
    {
        // declare and initialize the "terms" array as follows:

        double[] terms = {0.0, 0.5, 0.25, 0.125, 0.0625, 0.03125};

        // Note: the location at "ti" zero (i = 0) does not have be used.
        // Now, do the same thing, using the "terms2" array instead:

        double[] terms2 = { (double) 0.0, (double) 1 / 2, (double) 1 / 4,
            (double) 1 / 8, (double) 1 / 16, (double) 1 / 32};

        // This would store the value "t2i" at the location with index "i".
        // Again, the location at "t2i" zero (i=0) does not have to be used.
        //
        // now, view the contents of both of the arrays "terms" and "terms2":

        for (int m = 1 ; m < 6 ; m++)
        {
            System.out.print ((double) (terms [m]) + " ");
        } // end of for loop # 1

        System.out.println ();

        for (int n = 1 ; n < 6 ; n++)
        {
            System.out.print ((double) (terms2 [n]) + " ");
        } // end of for loop # 2

        System.out.println ();

    } // main method
} // TablesAndArraysExample4 class

No comments:

Post a Comment