5: 2D Arrays

Use 2D arrays to solve all these tasks.

5.1 Sum of Table Elements

Coding Exercise: Sum of Table Elements
In the first line there is given one number n. This number is the number of rows and the number of columns in the table. In the following n rows there are given n numbers in each row.
Write a program to calculate and output the sum of all numbers given in the table.
You may enter input for the program in the box below.

5.2 Snake

Coding Exercise: Snake
Given the size of the table n.
Write a program to create a snake table of size n.
For example, if the size of the table is 4, the output should be the following:

1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13

It is possible to solve this problem with and without 2D array. Try both ways.

You may enter input for the program in the box below.

5.3 Rotate the table

Coding Exercise: Rotate the table
In the first line given the number of rows in the table n and the number of columns in the table m. The following n lines contain m numbers each.
The table is filled with numbers from 1 to n*m. Write a program to rotate the table by 90 degrees to the right.
For example, if n = 3 and m = 7, the original table is:

1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21

The rotated table should be the following:

15 8 1
16 9 2
17 10 3
18 11 4
19 12 5
20 13 6
21 14 7

Write a program to print the original and the rotated table. Leave an empty line between the two tables.

5.4 An error in the table

Coding Exercise: An error in the table
In the first line there is given one number n. This number is the number of rows and the number of columns in the table. In the following n rows there are given n numbers in each row.
The last column and the last row are the control column, row. I.e. they contain the sums of the corresponding columns, rows.
An error (i.e. exactly one error) might have been made in one of table cells. Write a program to test this. If the table contains no errors, output "CORRECT". If an error was made, output the number of the row and the column of the cell with incorrect data. The rows are numbered from top, the columns are numbered from left to right. For example, if input is the following:

4
1 2 5 8
8 5 4 12
3 0 4 7
7 7 13 27

the output should be

2 1

You may enter input for the program in the box below.