CmSc 150 Fundamentals of Computing I


Problems on Two-dimensional arrays


  1. Given a 2D array of real numbers (declared as double),
    1. Compute the sum of each of its rows and store the results in a 1D array
    2. Compute the sum of each of its columns and store the results in another 1D array.
    3. Print the two one-dimensional arrays (each on a separate line)

    Application: The 2D array represents the monthly expenses of each department in a college. The financial office would like to know what the yearly expenses of each department, and what are the monthly expenses of the college.

  2. Given a 2D array of integers, compute the average of each of its rows and store the results in a 1D array. Compute the average of each of its columns and store the results in another 1D array. Find the row with the minimum average and print its number. Find the column with the maximum average and print its number.
  3. Application1: The 2D array can represent the monthly temperatures of each state. The weather forecast service would like to know the average temperatures for each state, and each monthly average for the country.

    Application2: the 2D array can represent students' grades in a course: a row corresponds to a student, a column corresponds to an assignment. The professor would like to know the average for each student and the average for each assignment.

  4. Given a 2D array (M x N) of integers, construct another 2D array (M x 2) each row of which will contain the maximum and the minimum of the corresponding row in the given array.
    Print the (M x 2) array row by row. Find the maximum in its first columns and print it, indicating the number of the row that has this maximum. Find the minimum in its second column and print it indicating the row that contains this minimum,
  5. Application: The 2D array can represent the monthly temperatures of each state. The weather forecast service would like to know for each state throughout a year what was the highest temperature and what was the lowest temperature recorded. Also, they would like to know what was the highest temperature for the country and where it was recorded, and the same for the lowest temperature.

  6. Given a 2D array of point coordinates (M x 2) construct a (M x M) array called distance in the following way:
  7. Each row corresponds to a point.
    distance[j][k] contains the distance between the point j and the point k.

    Write a function that takes in its parameters a point, and finds

    1. its closest point among the other points.
    2. its most distant point among the other points.
  8. Given a 2D array of point coordinates, sort the points by their x coordinates in ascending order and print the list of sorted points ( a point is printed by its coordinates). Sort the points by their y coordinates and print the sorted list.
  9. Given a 2D array of point coordinates (M x 2) find the two closest points and the two points that are most far away from each other
  10. Given two numbers M and N construct a table with M rows and N columns filled with integers in the following way:
    the border rows and columns contain 1, the next inner rows and columns contain 2, the next inner rows and columns contain 3, etc, untill the table is filled completely.

    Example:

    1. rows = 5, columns = 6
    2. 
          1 1 1 1 1 1
          1 2 2 2 2 1
          1 2 3 3 2 1
          1 2 2 2 2 1
          1 1 1 1 1 1
    3. rows = 6, columns = 4
    4. 
          1 1 1 1 
          1 2 2 1 
          1 2 2 1 
          1 2 2 1 
          1 2 2 1 
          1 1 1 1 
    5. rows = 2, columns = 4
    6. 
          1 1 1 1 
          1 1 1 1 
    7. rows = 8, columns = 10
    8. 
          1 1 1 1 1 1 1 1 1 1
          1 2 2 2 2 2 2 2 2 1
          1 2 3 3 3 3 3 3 2 1
          1 2 3 4 4 4 4 3 2 1
          1 2 3 4 4 4 4 3 2 1
          1 2 3 3 3 3 3 3 2 1
          1 2 2 2 2 2 2 2 2 1
          1 1 1 1 1 1 1 1 1 1

Back to CmSc 150 ' 06 home page
Back to CmSc 150 ' 05 home page