erstellt 31.05.03; übersetzt 01.07.03; © Copyright 2003 Deutsche Übersetzung Heinrich Gailer


Kapitel 49C Programmieraufgaben


Aufgabe 1 --- Summe aller Arrayelemente

Vervollständigen Sie das folgende Programm so, dass es die Summe aller Elemente des Arrays berechnet. Entwerfen Sie das Programm in der Weise, dass es auch dann funktioniert, wenn die Dimensionen der Zeilen und Spalten geändert werden. (Mit anderen Worten, verwenden Sie die length-Eigenschaft des Feldes.)


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} };

    // Summe deklarieren


    // Summe berechnen
    for ( int row=0; row < data.length; row++)
    {
      for ( int col=0; col < ???; col++)
      {

      }
    }

    // Summe ausgeben
    System.out.println(  );

  }
}

Zurück zur Übersicht.


Aufgabe 2 --- Summe einer jeden Zeile

Vervollständigen Sie das folgende Programm, so dass es die Summe aller Elemente für jede Zeile berechnet.


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} };

    // Summe deklarieren


    // Summe für jede Zeile berechnen
    for ( int row=0; row < data.length; row++)
    {
      // Summe initialisieren

      // Summe für diese Zeile berechnen
      for ( int col=0; col < ???; col++)
      {

      }

      // Summe für diese Zeile ausgeben
      System.out.println(  );
    }


  }
}

Zurück zur Übersicht.


Aufgabe 3 --- Maximale und minimale Elemente

Vervollständigen Sie das folgende Programm, so dass es das größte und das kleinste Element des Arrays findet. Schreiben Sie das Programm, so dass es selbst dann funktioniert, wenn die Dimensionen der Zeilen und Spalten geändert werden. (Mit anderen Worten, verwenden Sie eher length als fest kodierte Zahlen).

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} };

    // max und min deklarieren


    // Summe berechnen
    for ( int row=0; row < data.length; row++)
    {
      for ( int col=0; col < ???; col++)
      {

      }
    }

    // Ergebnisse ausgeben
    System.out.println(  );

  }
}

Zurück zur Übersicht.


Aufgabe 4 --- Größte Elemente

Verändern Sie das Programm, so dass es das größte Element jeder Zeile ausgibt.

Zurück zur Übersicht.


Aufgabe 5 --- Elemente in jeder Zeile vertauschen

Schreiben Sie ein Programm, das die Anordnung der Elemente in jeder Zeile der Matrix umkehrt, dann die Ergebnismatrix ausgibt.

Zurück zur Übersicht.


Aufgabe 6 --- Bild glätten (lang)

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 Aufgabe.

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 Aufgabe.)

Zurück zur Übersicht.


Aufgabe 7 --- 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 Aufgabe), or for the image display program (next Aufgabe).

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

Zurück zur Übersicht.


Aufgabe 8 --- 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.

Zurück zur Übersicht.