Back to Week 2
Lab Session

Week 2: Lab — Arrays

Hands-on Programming Exercises — Ch. 7

Progress0 / 9 completed

1Array Sum

Read 5 integers into an array, then compute and print the sum of all elements using a for loop and the += operator.
Expected OutputEnter 5 numbers: 10 20 30 40 50 Sum = 150
Your Code
Declare the array with int[] numbers = new int[5];. Use a for loop to read values: numbers[i] = input.nextInt();. Then loop again to add each element to sum using sum += numbers[i];.

2Find Maximum

Read N integers into an array, then find and print the maximum value. Initialize max with the first element and compare with the rest using an if statement.
Expected OutputEnter size: 4 Enter 4 numbers: 8 23 5 17 Max = 23
Your Code
Set int max = numbers[0]; then loop from index 1: if (numbers[i] > max) max = numbers[i];.

3Reverse Array

Read N integers into an array, then print them in reverse order by looping from the last index (length - 1) down to 0.
Expected OutputEnter size: 5 Enter 5 numbers: 1 2 3 4 5 Reversed: 5 4 3 2 1
Your Code
Use for (int i = numbers.length - 1; i >= 0; i--) to traverse the array backwards. Print each element with a space separator.

4Copy Array

Create an array with values {1, 2, 3, 4, 5}, copy it using System.arraycopy, modify the copy, and show the original is unchanged.
Expected OutputOriginal: 1 2 3 4 5 Copy (modified): 99 2 3 4 5 Original (unchanged): 1 2 3 4 5
Your Code
Create the copy: int[] copy = new int[original.length];. Then System.arraycopy(original, 0, copy, 0, original.length);. Modify copy[0] = 99; and print both arrays using a for loop.

5Array Average

Read doubles into an array, then compute the average using an enhanced for loop (for-each). Divide the sum by array.length.
Expected OutputEnter 4 grades: 85.5 90.0 78.5 92.0 Average = 86.5
Your Code
Use for (double grade : grades) { sum += grade; } then double average = sum / grades.length;. Print with System.out.println("Average = " + average);.

6Pass Array to Method

Write a method called doubleElements that takes an int[] array and doubles every element in it. Print the array in main before and after calling the method to show that arrays are passed by reference.
Expected OutputBefore: 1 2 3 4 5 After: 2 4 6 8 10
Your Code
In the method, loop through the array: for (int i = 0; i < arr.length; i++) arr[i] *= 2;. Since arrays are passed by reference, changes inside the method affect the original array in main.

7Return Array from Method

Write a method called createSquares that takes an int n and returns a new int[] array of size n where each element at index i contains (i+1)2. Print the returned array in main.
Expected OutputEnter n: 5 Squares: 1 4 9 16 25
Your Code
Create a new array inside the method: int[] result = new int[n];. Fill it with a loop: result[i] = (i + 1) * (i + 1);. Then return result;. In main, receive it: int[] squares = createSquares(n);.

82D Array Sum

Read a 3×3 matrix using a 2D array (int[][]), then compute the sum of all elements using nested for loops.
Expected OutputEnter 3x3 matrix: 1 2 3 4 5 6 7 8 9 Sum = 45
Your Code
Declare: int[][] matrix = new int[3][3];. Use nested loops to read: matrix[i][j] = input.nextInt();. Then nested loops again to sum: sum += matrix[i][j];.

9Matrix Row Sum

Read a 3×3 matrix and print the sum of each row. Use nested for loops — the outer loop iterates rows, the inner loop sums columns within each row.
Expected OutputEnter 3x3 matrix: 1 2 3 4 5 6 7 8 9 Row 1 sum = 6 Row 2 sum = 15 Row 3 sum = 24
Your Code
For each row i, initialize int sum = 0;, then inner loop: sum += matrix[i][j];. Print "Row " + (i+1) + " sum = " + sum after each inner loop.