GIGO: words unreadable aloud
Mishrogo Weedapeval
 

 

  Monday 31 May 2004
Chapter 4, Exercise 19 variant

I thought the c4x19 solution might look better this way (with print and create folded into the 2D array class), but it gets me a null pointer crash in the compiler. I guess I oughta download a more recent nightly build and try it again. Makes me leery about trying the 3D version, though.

(PS, I really did post the c4x19 one before heading out for Tucson for the long weekend. The Radio Userland server(s?) seem to suck pretty bad a lot of the time, though.)

class arDbl {
    var ar : Array[Double] = _;
    def length = ar.length;
    def apply( i: Int ) = ar(i);
    def update( i:Int, nv: Double) = ar.update(i, nv );
    def this( sz: Int ) = {
        this();
        ar= new Array[Double]( sz );
    }
};
class ar2D {
    var ard : Array[ arDbl ] = _;
    def length = ard.length; 
    def apply( i: Int ) = ard(i);
    def update( i:Int, j:Int, nv:Double ) = {
        ard(i).update(j, nv );
    }
    def this( rows:Int, cols: Int ) = {
        this();
        ard= new Array[ arDbl ]( rows );
        for( val j <- Iterator.range(0, rows) ) {
            ard(j) = new arDbl(cols);
        }
    }
    def this( rows:Int, cols: Int, start:Double, end:Double ) = {
        this(rows,cols);
        var current = start;
        var step = (end - start) / (rows * cols -1);
        for( val j <- Iterator.range( 0, rows ) ) {
            var curr_row = ard(j);
            for( val i <- Iterator.range( 0, cols ) ) {
                curr_row.update( i, current);
                current = current + step;
            }
        } 
    }
    def print = {
        var rows = ard.length;
        var cols = ard(0).length;
        for( val i <- Iterator.range( 0, rows ) ) {
            for( val j <- Iterator.range( 0, cols ) ) {
                Console.print( ard(i)(j) + " " );
            }
            Console.println("");
        }
    }
}
object c4x19 {
    def main (args: Array[String] ) = {
        Console.println("--> new ar2D( 3, 3, 1.0, 10.0 )" );
        val arm = new ar2D( 3, 3, 1.0, 10.0 );
        arm.print; 
        Console.println("");
        Console.println("--> new ar2D( 3, 4, 1.0, 20.0 )" );
        (new ar2D( 3, 4, 1.0, 20.0 )).print;
        Console.println("");
        Console.println("--> new ar2D( 5, 5, -10.0, 10.0 )" );
        (new ar2D( 5, 5, -10.0, 10.0 )) print
    }
}

11:59:23 PM   comment/     


Click here to visit the Radio UserLand website. Click to see the XML version of this web page. © Copyright 2007 Doug Landauer .
Last update: 07/2/6; 12:40:05 .
Click here to send an email to the editor of this weblog.

May 2004
Sun Mon Tue Wed Thu Fri Sat
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Apr   Jun

Previous/Next