Lab 6
Lab 6
Sahiwal Campus
Example 3: This program demonstrates how to find Maximum and minimum value in arrays.
Exercises:
Exercise 1: Write a program using arrays to solve the problem. The problem is to read 10 numbers, get
the average of these numbers, and find the number of the items greater than the average.
Exercise 2: Count the occurrences of each letter in the array. To do so, create an array, say counts,
of 26 int values, each of which counts the occurrences of a letter. That is counts[0] counts the
number of a‘s, counts[1] counts the number of b‘s, and so on.
Exercise 3: Write a program that reads 10 numbers in an array, write a method which will return the
sort array.
Multidimensional Array
Up to this point, we were storing elements in linear manner, that is, we provide one index to
locate the element in one-dimensional structure. Java also provides arrays of arrays, this means
we create an array, and each element in that array is an array itself. These arrays are also called
multidimensional arrays.
Example 4: Two dimensional arrays are common in representing matrices, for example, we
can write a program that adds two matrices (remember that matrices must have same number
of rows and same number of columns when it comes to addition).
Example 5: This program demonstrates how to pass Two Dimensional arrays to methods.
Exercises:
Exercise 4: Write a program that reads 16 integers from the user, stores them in 5 by 4 matrix, the last
row of the matrix should contain the sums of the columns (see figure 9.2) calculated by your program.
Then the whole matrix is printed.
Exercise 5: Write a Java program to multiply two matrices of 5x5. Suggest the data.
Exercise 6: (Largest row and column) Write a program that randomly fills in 0s and 1s into a 4-by-4
matrix, prints the matrix, and finds the first row and column with the most 1‘s. Here is a sample
input/output of the pro-gram:
0011
0011
1101
1010
The largest row index: 2 The largest column index: 2
Post Lab
1. (Eliminate duplicates) Write a method that returns a new array by eliminating the duplicate
values in the array. Write a JAVA program that reads in at least ten integers, invokes the
method, and displays the results.
2. (Locate the largest element) Write the following method that returns the location of the
largest element in a two-dimensional array.
The return value is a one-dimensional array that contains two elements. These two
elements indicate the row and column indices of the largest element in the two-
dimensional array. Write a test program that prompts the user to enter a two dimensional
array and displays the location of the largest element in the array.
Enter the number of rows and columns of the array:3 4
Enter the array:
23.5 35 2 10
4.5 3 45 3.5
35 44 5.5 9.6
The location of the largest element is at (1, 2)
Note: Submit your Lab report consisting of all in-lab exercise tasks results and post lab tasks
within two working days. After the deadline your lab report won’t be considered.