|
Problems on Two-dimensional arrays
- Given a 2D array of real numbers (declared as double),
- Compute the sum of each of its rows and store the results in a 1D array
- Compute the sum of each of its columns and store the results in another 1D
array.
- 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.
- 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.
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.
- 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,
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.
- Given a 2D array of point coordinates (M x 2) construct a (M x M) array
called distance in the following way:
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
- its closest point among the other points.
- its most distant point among the other points.
- 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.
- 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
- 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:
- rows = 5, columns = 6
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
- rows = 6, columns = 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
- rows = 2, columns = 4
1 1 1 1
1 1 1 1
- rows = 8, columns = 10
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
|