In-Class Assignment 5
Using the skeleton program below, first write the code to create a 10 x 5 two-dimensional array, initially setting each element of the array to the string 'x'.
Once you have the array created, write the two subroutines called in the
skeleton. print_2d will not take any arguments, and will
simply print out the contents of the array (see sample output).
set_2d will take three arguments: The x and y coordinates
of a cell in the array, and a string to which to set that position
in the array.
For a bonus half-point, add code to set_2d to error-check:
make sure that the x and y coordinates are within the bounds of the array,
and print out a warning message (without updating the array) if they are
not
NOTE: print_2d and set_2d
should not "know" that the array they are working with is 10x5. The values
10 and 5 should not appear hardcoded anywhere in either subroutine.
Skeleton Program
#!/usr/bin/env perl use strict; use warnings; #Your code to create a two-d array here print "After initial creation:\n"; print_2d(); set_2d(5, 0, 'o'); set_2d(9, 3, 'o'); set_2d(2, 4, '*'); print "After setting three cels:\n"; print_2d(); #if you choose to implement the bonus, un-comment the following four lines: #set_2d(11, 4, '-'); #set_2d(2, 8, '-'); #print "After (NOT) setting out-of-range cells:\n"; #print_2d(); ###Subroutine Definitions Here###
Sample Output
After defining your array and the two subroutines, your code should produce the following results (with the bonus implemented as well...)
After initial creation: x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x After setting three cels: x x x x x o x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x o x x * x x x x x x x 11 out of range for X-coordinate 8 out of range for Y-coordinate After (NOT) setting out-of-range cells: x x x x x o x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x o x x * x x x x x x x
Submission
Your program must run onrcs-sun4.rpi.edu. To submit, execute
the program ~lallip/public/ic_submit.pl. This assignment is due
at 6pm today, October 5, 2005
