You are on page 1of 3

Fundamentos de programación

Worksheet – Arrays

1. Given the number n, create a matrix of integers of size n×n and fill it with random
numbers. Then iterate over the array using nested loops and output every element of the
array.

2. Implement the printTheThirdRow method, that prints the third row of a two-dimensional
array called twoDimArray. The elements should be separated by a comma.

3. Find the indexes of the initial appearance of the highest element in a matrix.

4. Implement the printCorners method. It will be able to print all corner elements of the
twoDimArray in the following order: left to right and top to bottom.

5. Given a two-dimensional array (matrix) and the two numbers col1 and col2, create a
method that swaps the columns with indexes col1 and col2 within the matrix.

6. A cinema has n rows, each row consisting of m seats. A two-dimensional matrix stores the
information on the sold tickets: the number 1 meaning that the ticket for this place is
already sold, and the number 0 that the place is available. You want to buy k tickets to
neighboring seats in the same row. Write a program to find out whether it can be done.

On the input, the program gets the number of n rows and m seats, as well as the number
of tickets we need (k).
The program will output the number of the first row with k consecutive available seats. If
there is no such a row, it will output the number 0.

7. Create a program that accepts an integer n as input, representing the number of rows and
columns in a square matrix, and prints a diagonally striped pattern over the array. This
should be done by displaying cells with a '#' if their row index and column index are the
same or add together to n-1, and with a space ' ' otherwise. A newline should follow each
row of the array.
Fundamentos de programación

Optional exercises

8. Your task is to draw a star on the n×n field using the symbols ‘.’ and ‘*’. The number n
must be odd and not greater than 15.
To do this, you can follow the instructions:
• Firstly, you need to create a two-dimensional array (matrix) from n×n elements by
filling it with the ‘.’ symbols. Each element of the matrix will be a char containing a
single symbol.
• Secondly, fill the middle row of the matrix, the middle column, and the diagonals with
the ‘*’ symbol. As a result, all *'s in the array must form the star figure.
• Thirdly, output this matrix; the elements of the array should be separated by a space.

Sample Input:
9

Sample Output:

* . . . * . . . *
. * . . * . . * .
. . * . * . * . .
. . . * * * . . .
* * * * * * * * *
. . . * * * . . .
. . * . * . * . .
. * . . * . . * .
* . . . * . . . *

9. Given a rectangle array n×m in size. Rotate it by 90 degrees clockwise, by storing the result
into the new array m×n in size.
The input consists of the two numbers n and m, not exceeding 100.
Output the resulting array, separating the numbers by a single space.

Sample Input:
11 12 13 14
21 22 23 24
31 32 33 34

Sample Output:
31 21 11
32 22 12
33 23 13
34 24 14
Fundamentos de programación

10. Given the number n, create a matrix of size n×n and fill it using the following rule.
Numbers 0 should be stored on the primary (main) diagonal. The two diagonals, adjacent
to the primary one, should contain numbers 1. The next two diagonals should contain
numbers 2, and so on.
Note: the primary diagonal runs from the top left corner to the bottom right corner.

11. Create a program that works as a Spanish-English dictionary; that is, it will ask for a word in
Spanish and write the corresponding word in English.
The program will display a menu with two options:

• Add words to the dictionary.


• Look up words.

You might also like