Lab 2 - Arrays + Looping
Lab 2 - Arrays + Looping
EXERCISE 1
Write a program that produces the multiplication table of 1 to 9 as shown
| 1 2 3 4 5 6 7 8 9
-------------------------------
1 | 1 2 3 4 5 6 7 8 9
2 | 2 4 6 8 10 12 14 16 18
3 | 3 6 9 12 15 18 21 24 27
4 | 4 8 12 16 20 24 28 32 36
5 | 5 10 15 20 25 30 35 40 45
6 | 6 12 18 24 30 36 42 48 54
7 | 7 14 21 28 35 42 49 56 63
8 | 8 16 24 32 40 48 56 64 72
9 | 9 18 27 36 45 54 63 72 81
Exercise 2
Write a program ReverseArray.java that reverses the order of a one-dimensional array
a[] of int values. Do not create another array to hold the result.
Exercise 3
Write a program named minGap.java that finds the minimum 'gap' between adjacent values
in an integer array. The gap between two adjacent values in a arrayis defined as the second
value minus the first value. For example, suppose a variable called array is an array of
integers that stores the following sequence of values:
int[] array = {1, 3, 6, 7, 12};
1
The first gap is 2 (3 - 1), the second gap is 3 (6 - 3), the third gap is 1 (7 - 6) and the fourth
gap is 5 (12 - 7). Thus, the program should display 1 because that is the smallest gap in the
array. If the size of the array is less than 2 elements, you should display 0.
Exercise 4
A. Create a new java project called matrix. The main purpose of the program is to offer to the user a
set of operations on matrices
B. Define a variable T that corresponds to a matrix having the size 5*5.
C. Show the menu that allows the user to choose an operation. The user selects an operation by
typing its number. We define in what follows the content of each option. After finishing an
operation, the menu is re-displayed unless the user typed 5 for Exit.
1. The user enters the values to fill the matrix, only positive values are accepted
2. The user enters the index i of the row to delete. Here is an example with i = 2
Before 1 2 4 7 8 After
1 2 4 7 8 2 5 98 5 4
2 5 98 5 4 8 9 3 6 1
7 4 7 9 3 42 2 95 37 6
8 9 3 6 1
42 2 95 37 6
16 9 2 7
6 3 12 13
11 14 5 4
1 8 15 10