created 05/31/03


Chapter 49C Programming Exercises


The algorithms in Chapter 47, "Common Array Algorithms", can be extended to work with two-dimensional arrays. The first several exercises ask you to do this.


Exercise 1 --- Sum of All Array Elements

Complete the following program so that it computes the sum of all the elements of the array. Write the program so that it works even if the dimensions of the rows and columns are changed. In other words, use length rather than hard-coded numbers.)

import java.io.* ;

class ArraySum
{

  public static void main ( String[] args ) throws IOException
  {
    int[] data = { {3, 2, 5},
                   {1, 4, 4, 8, 13},
                   {9, 1, 0, 2},
                   {0, 2, 6, 3, -1, -8} };
    
    // declare the sum
    
    
    // compute the sum
    for ( int row=0; row < data.length; row++)
    {
      for ( int col=0; col < ???; col++) 
      {
        
      }
    }
      
    // write out the sum
    System.out.println(  );

  }
}      

Click here to go back to the main menu.


Exercise 2 --- Sum of Each Row

Complete the following program so that it computes the sum of the elements in each row.

import java.io.* ;

class RowSums
{

  public static void main ( String[] args ) throws IOException
  {
    int[] data = { {3, 2, 5},
                   {1, 4, 4, 8, 13},
                   {9, 1, 0, 2},
                   {0, 2, 6, 3, -1, -8} };
    
    // declare the sum
    
    
    // compute the sums for each row
    for ( int row=0; row < data.length; row++)
    {
      // initialize the sum
      
      // compute the sum for this row
      for ( int col=0; col < ???; col++) 
      {
        
      }
      
      // write the sum for this row
      System.out.println(  );
    }
      

  }
}      

Click here to go back to the main menu.


Exercise 3 --- Sum of Each Column

Modify the program so that it computes and prints the sum of each column of the array.

Click here to go back to the main menu.


Exercise 4 --- Maximum and Minimum Elements

Complete the following program so that it computes the maximum and minimun of the elements in the array. Write the program so that it works even if the dimensions of the rows and columns are changed. In other words, use length rather than hard-coded numbers.)

import java.io.* ;

class ArrayMaxMin
{

  public static void main ( String[] args ) throws IOException
  {
    int[] data = { {3, 2, 5},
                   {1, 4, 4, 8, 13},
                   {9, 1, 0, 2},
                   {0, 2, 6, 3, -1, -8} };
    
    // declare the max and the min
    
    
    // compute the sum
    for ( int row=0; row < data.length; row++)
    {
      for ( int col=0; col < ???; col++) 
      {
        
      }
    }
      
    // write out the results
    System.out.println(  );

  }
}      

Click here to go back to the main menu.


Exercise 5 --- Largest Elements

Modify the program so that it computes and prints the largest element in each row.

Click here to go back to the main menu.


Exercise 6 --- Reversal of Elements in Each Row

Write a program that reverses the order of the elements in each row of the matrix, then prints out the resulting matrix.

Click here to go back to the main menu.


Exercise 7 --- Image Smoother (long)

A gray-level image is sometimes stored as a list of int values. The values represent the intensity of light at discrete positions in the image.

An image may be smoothed by replacing each element with the average of the element's neighboring elements.

Say that the original values are in the 2D array "image". Compute the smoothed array by doing this: Each value smooth[r][c] is the average of nine values:

  
image[r-1][c-1], image[r-1][c  ], image[r-1][c+1],
image[r  ][c-1], image[r  ][c  ], image[r  ][c+1],
image[r+1][c-1], image[r+1][c  ], image[r+1][c+1].

Assume that the image is rectangular, that is, all rows have the same number of locations. Use integer arithmetic for this so that the values in smooth are integers.

import java.io.* ;

class Smooth
{

  public static void main ( String[] args ) throws IOException
  {
    int[][] image  = {{0,0,0,0,0,0,0,0,0,0,0,0},
                      {0,0,0,0,0,0,0,0,0,0,0,0},
                      {0,0,5,5,5,5,5,5,5,5,0,0},
                      {0,0,5,5,5,5,5,5,5,5,0,0},
                      {0,0,5,5,5,5,5,5,5,5,0,0},
                      {0,0,5,5,5,5,5,5,5,5,0,0},
                      {0,0,5,5,5,5,5,5,5,5,0,0},
                      {0,0,5,5,5,5,5,5,5,5,0,0},
                      {0,0,5,5,5,5,5,5,5,5,0,0},
                      {0,0,5,5,5,5,5,5,5,5,0,0},
                      {0,0,0,0,0,0,0,0,0,0,0,0},
                      {0,0,0,0,0,0,0,0,0,0,0,0}};

    // assume a rectangular image
    int[][] smooth = new int[ image.length ][ image[0].length ];
    
    // Compute the smoothed value for 
    // non-edge locations in the image.

    for ( int row=1; row<image.length-1; row++ )
    {
      for ( int col=1; col<image[row].length-1; col++ )
      {
        
      }
      smooth[row][col] = sum/9;
    }
      
    // write out the input
    
    // write out the result

  }
}      

The edges of the image are a problem because only some of the nine values that go into the average exist. There are various ways to deal with this problem:

  1. Easy (shown above): Leave all the edge locations in the smoothed image to zero. Only inside locations get an averaged value from the image.
  2. Harder: Copy values at edge locations directly to the smoothed image without change.
  3. Hard: For each location in the image, average together only those of the nine values that exist. This calls for some fairly tricky if statements, or a tricky set of for statements inside the outer two.
  4. Alternate: Copy the original image into the center of an enlarged array that has two more columns and two more rows than the original. Create another enlarged array to hold a temporary smoothed version of the enlarged array. Now, apply the Easy solution to the enlarged array with the other enlarged array as the result. Next, copy the center of the result to the final smooth image.

Here is a sample run of the hard solution:

C:\>java ImageSmooth

Input:
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 5 5 5 5 5 5 5 5 0 0
0 0 5 5 5 5 5 5 5 5 0 0
0 0 5 5 5 5 5 5 5 5 0 0
0 0 5 5 5 5 5 5 5 5 0 0
0 0 5 5 5 5 5 5 5 5 0 0
0 0 5 5 5 5 5 5 5 5 0 0
0 0 5 5 5 5 5 5 5 5 0 0
0 0 5 5 5 5 5 5 5 5 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0

Output:
0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 1 1 1 0 0
0 1 2 3 3 3 3 3 3 2 1 0
0 1 3 5 5 5 5 5 5 3 1 0
0 1 3 5 5 5 5 5 5 3 1 0
0 1 3 5 5 5 5 5 5 3 1 0
0 1 3 5 5 5 5 5 5 3 1 0
0 1 3 5 5 5 5 5 5 3 1 0
0 1 3 5 5 5 5 5 5 3 1 0
0 1 2 3 3 3 3 3 3 2 1 0
0 0 1 1 1 1 1 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0
C:\>

Once you have the program working with hard-coded data, modify it so that it reads its data using input redirection (Chapter 22). Use your programming editor to create some interesting input files, or create input files by doing the next exercise.

Further modify the program so that it writes out only the smoothed image, as text, one integer per line. This output can be redirected to a text file and then used as input for the image display program (see the second following exercise.)

Click here to go back to the main menu.


Exercise 8 --- Image Creator (short)

This program uses no arrays. It is put here becase it is useful for use with some of the other programs of this section.

Write a program that creates a 64 by 64 image as a text file. The image will consist of eight bands of increasingly higher values. Think of the image as 64 rows of 64 integers each, but actually write out one integer per line. This is for the convenience of the programs the input the image.

The image starts out with 8 rows of zero, so the program writes 8 times 64 zeros (as character '0'). Next, the image has 8 rows of eight, so the program writes 8 times 64 eights (as character '8'). Next, the image has 8 rows of sixteen, and so on. Write one value per line, without spaces or commas.

Use output redirection to send the output to a file. Use this file as input for you image smoother (previous exercise), or for the image display program (next exercise).

This is a very short program. The body consists of three lines: a double for loop with a one line loop body.

Click here to go back to the main menu.


Exercise 9 --- Image Display (medium length)

It would be nice to display your images by some means other than printing out integers. Ideally, your programs should read and write images using standard image formats. But actual image formats, like giff, tiff, and jpeg are very complicated. Instead, write a program that displays an image by using rows of characters to represent brightness levels.

Write a program that reads in a file that contains one integer per line. Each integer represents one location in the image. Assume that there are 64 rows and 64 columns in the image. Assume that the integers are in the range 0 to 63.

For each integer, write out a single character depending on its value:

Don't put extra spaces between characters. Start a new line after each row of 128 characters. This is one place where the switch statement is convenient. To use it, divide the input value by 8 to get the integer for the switch. Or you can use eight if-else statements if you prefer.

Click here to go back to the main menu.