Assignment: Arrays in Java

Assignment: Arrays in Java

Objective:

The objective of this assignment is to thoroughly test your understanding of arrays in Java, including single-dimensional arrays, multidimensional arrays, jagged arrays, and anonymous arrays. You will be required to write code snippets and solve problems using these array structures.


Part 1: Single-Dimensional Arrays

Question 1: Array Initialization and Access

Write a Java program that creates an array of 10 integers, initializes it with values from 1 to 10, and prints all the elements.

Example Output:

Array elements: 1 2 3 4 5 6 7 8 9 10

Question 2: Sum and Average of Array Elements

Write a Java program that calculates the sum and average of all elements in a single-dimensional array of 5 integers entered by the user.

Example Output:

Enter 5 numbers: 10 20 30 40 50
Sum: 150
Average: 30.0

Question 3: Find Maximum and Minimum Values in an Array

Write a Java program that finds and prints the maximum and minimum values in an array of integers.

Example Output:

Array elements: 10 20 30 40 50
Maximum value: 50
Minimum value: 10

Question 4: Reverse an Array

Write a Java program that reverses the elements of a given array and prints the reversed array.

Example Output:

Original array: 1 2 3 4 5
Reversed array: 5 4 3 2 1

Question 5: Array Element Frequency

Write a Java program that counts the frequency of each element in an array of integers and prints the result.

Example Output:

Array elements: 1 2 2 3 3 3
Frequency:
1 occurs 1 time(s)
2 occurs 2 time(s)
3 occurs 3 time(s)

Question 6: Find Second Largest Element in an Array

Write a Java program that finds the second largest element in a single-dimensional array.

Example Output:

Array elements: 10 20 30 40 50
Second largest element: 40

Question 7: Remove Duplicates from Array

Write a Java program that removes duplicate elements from an array and prints the unique elements.

Example Output:

Original array: 1 2 2 3 4 4 5
Array without duplicates: 1 2 3 4 5

Question 8: Find Common Elements Between Two Arrays

Write a Java program that finds and prints the common elements between two arrays.

Example Output:

Array 1: 1 2 3 4 5
Array 2: 4 5 6 7 8
Common elements: 4 5

Question 9: Rotate an Array

Write a Java program that rotates an array by a given number of positions to the right.

Example Output:

Original array: 1 2 3 4 5
Rotate by 2 positions
Rotated array: 4 5 1 2 3

Question 10: Find Missing Number in an Array

Write a Java program that finds the missing number in an array containing numbers from 1 to n with one missing number.

Example Output:

Array elements: 1 2 4 5
Missing number: 3

Part 2: Multidimensional Arrays

Question 11: Matrix Addition

Write a Java program that adds two 2x2 matrices and prints the resulting matrix.

Example Output:

Matrix 1:
1 2
3 4

Matrix 2:
5 6
7 8

Sum of matrices:
6 8
10 12

Question 12: Matrix Multiplication

Write a Java program that multiplies two 2x2 matrices and prints the resulting matrix.

Example Output:

Matrix 1:
1 2
3 4

Matrix 2:
2 0
1 2

Product of matrices:
4 4
10 8

Question 13: Transpose of a Matrix

Write a Java program that computes the transpose of a 3x3 matrix and prints the transposed matrix.

Example Output:

Original matrix:
1 2 3
4 5 6
7 8 9

Transposed matrix:
1 4 7
2 5 8
3 6 9

Question 14: Sum of Diagonals in a Matrix

Write a Java program that calculates and prints the sum of the primary and secondary diagonals of a 3x3 matrix.

Example Output:

Matrix:
1 2 3
4 5 6
7 8 9

Sum of primary diagonal: 15
Sum of secondary diagonal: 15

Question 15: Matrix Subtraction

Write a Java program that subtracts one 3x3 matrix from another and prints the resulting matrix.

Example Output:

Matrix 1:
9 8 7
6 5 4
3 2 1

Matrix 2:
1 2 3
4 5 6
7 8 9

Difference of matrices:
8 6 4
2 0 -2
-4 -6 -8

Question 16: Check if a Matrix is Symmetric

Write a Java program that checks if a given matrix is symmetric (i.e., equal to its transpose).

Example Output:

Matrix:
1 2 3
2 4 5
3 5 6

The matrix is symmetric.

Question 17: Spiral Traversal of a Matrix

Write a Java program that prints the elements of a 3x3 matrix in spiral order.

Example Output:

Matrix:
1 2 3
4 5 6
7 8 9

Spiral order:
1 2 3 6 9 8 7 4 5

Question 18: Find Maximum Element in Each Row of a Matrix

Write a Java program that finds and prints the maximum element in each row of a 3x3 matrix.

Example Output:

Matrix:
1 2 3
4 5 6
7 8 9

Maximum in row 1: 3
Maximum in row 2: 6
Maximum in row 3: 9

Question 19: Sum of Each Row and Column in a Matrix

Write a Java program that calculates and prints the sum of each row and each column in a 3x3 matrix.

Example Output:

Matrix:
1 2 3
4 5 6
7 8 9

Sum of row 1: 6
Sum of row 2: 15
Sum of row 3: 24

Sum of column 1: 12
Sum of column 2: 15
Sum of column 3: 18

Question 20: Identity Matrix Check

Write a Java program that checks if a given 3x3 matrix is an identity matrix (i.e., 1s on the diagonal and 0s elsewhere).

Example Output:

Matrix:
1 0 0
0 1 0
0 0 1

The matrix is an identity matrix.

Part 3: Jagged Arrays

Question 21: Jagged Array Creation and Access

Write a Java program that creates a jagged array with 3 rows, where the first row has 2 columns, the second row has 3 columns, and the third row has 4 columns. Initialize the array with values and print the elements.

Example Output:

Jagged array:
Row 1: 1 2
Row 2: 3 4 5
Row 3: 6 7 8 9

Question 22: Triangle Matrix using Jagged Array

Write a Java program that uses a jagged array to represent a triangular matrix and fills it with values. Print the triangular matrix.

Example Output:

Triangular matrix:
1 
2 3 
4 5 6 
7 8 9 10

Question 23: Jagged Array for Student Marks

Write a Java program that stores the marks of students in different subjects using a jagged array and prints them.

Example Output:

Student 1 marks: 85 90
Student 2 marks: 78 88 92
Student 3 marks: 80 75 85 90

Question 24: Jagged Array for Employee Salaries

Write a Java program that stores the salaries of employees in different departments using a jagged array and prints them.

Example Output:

Department 1 salaries: 50000 60000
Department 2 salaries: 45000 55000 65000
Department 3 salaries: 70000 80000

Question 25: Jagged Array with User Input

Write a Java program that allows the user to input values for a jagged array with 3 rows, where the first row has 2 columns, the second row has 3 columns, and the

third row has 4 columns. Print the array after input.

Example Output:

Enter values for row 1 (2 columns): 1 2
Enter values for row 2 (3 columns): 3 4 5
Enter values for row 3 (4 columns): 6 7 8 9

Jagged array:
Row 1: 1 2
Row 2: 3 4 5
Row 3: 6 7 8 9

Part 4: Anonymous Arrays

Question 26: Sum Using Anonymous Array

Write a Java program that calculates the sum of an anonymous array passed to a method.

Example Output:

Sum of array elements: 100

Question 27: Average Using Anonymous Array

Write a Java program that calculates the average of an anonymous array of double values passed to a method.

Example Output:

Average of array elements: 86.8

Question 28: Maximum Element Using Anonymous Array

Write a Java program that finds the maximum element in an anonymous array passed to a method.

Example Output:

Maximum element: 50

Question 29: Minimum Element Using Anonymous Array

Write a Java program that finds the minimum element in an anonymous array passed to a method.

Example Output:

Minimum element: 10

Question 30: Sort Using Anonymous Array

Write a Java program that sorts an anonymous array passed to a method using bubble sort and prints the sorted array.

Example Output:

Original array: 5 3 8 1 2
Sorted array: 1 2 3 5 8

Part 5: Array Manipulation

Question 31: Insert Element in Array

Write a Java program that inserts an element at a specific position in an array and shifts the other elements to the right.

Example Output:

Original array: 1 2 3 4 5
Insert 10 at position 3
Updated array: 1 2 10 3 4 5

Question 32: Remove Element from Array

Write a Java program that removes an element from a specific position in an array and shifts the remaining elements to the left.

Example Output:

Original array: 1 2 3 4 5
Remove element at position 2
Updated array: 1 2 4 5

Question 33: Split an Array

Write a Java program that splits an array into two halves and prints both halves.

Example Output:

Original array: 1 2 3 4 5 6
First half: 1 2 3
Second half: 4 5 6

Question 34: Merge Two Arrays

Write a Java program that merges two arrays into a single array and prints the merged array.

Example Output:

Array 1: 1 2 3
Array 2: 4 5 6
Merged array: 1 2 3 4 5 6

Question 35: Array Rotation by k Positions

Write a Java program that rotates an array by k positions to the right and prints the rotated array.

Example Output:

Original array: 1 2 3 4 5
Rotate by 3 positions
Rotated array: 3 4 5 1 2

Question 36: Replace All Negative Numbers in Array

Write a Java program that replaces all negative numbers in an array with 0 and prints the updated array.

Example Output:

Original array: 1 -2 3 -4 5
Updated array: 1 0 3 0 5

Question 37: Find All Prime Numbers in an Array

Write a Java program that finds and prints all the prime numbers in an array.

Example Output:

Array elements: 1 2 3 4 5 6 7 8 9 10
Prime numbers: 2 3 5 7

Question 38: Count Even and Odd Numbers in an Array

Write a Java program that counts the number of even and odd numbers in an array and prints the result.

Example Output:

Array elements: 1 2 3 4 5 6 7 8 9 10
Even numbers: 5
Odd numbers: 5

Question 39: Find the Longest Increasing Subsequence in an Array

Write a Java program that finds and prints the longest increasing subsequence in an array.

Example Output:

Array elements: 1 2 3 1 2 3 4 5
Longest increasing subsequence: 1 2 3 4 5

Question 40: Move All Zeros to End of Array

Write a Java program that moves all zeros in an array to the end while maintaining the order of other elements.

Example Output:

Original array: 1 0 2 0 3 0 4 5
Updated array: 1 2 3 4 5 0 0 0

Part 6: Array Sorting and Searching

Question 41: Bubble Sort

Write a Java program that sorts an array of integers using the bubble sort algorithm.

Example Output:

Original array: 5 1 4 2 8
Sorted array: 1 2 4 5 8

Question 42: Selection Sort

Write a Java program that sorts an array of integers using the selection sort algorithm.

Example Output:

Original array: 29 10 14 37 13
Sorted array: 10 13 14 29 37

Question 43: Insertion Sort

Write a Java program that sorts an array of integers using the insertion sort algorithm.

Example Output:

Original array: 12 11 13 5 6
Sorted array: 5 6 11 12 13

Question 44: Merge Sort

Write a Java program that sorts an array of integers using the merge sort algorithm.

Example Output:

Original array: 12 11 13 5 6 7
Sorted array: 5 6 7 11 12 13

Question 45: Quick Sort

Write a Java program that sorts an array of integers using the quick sort algorithm.

Example Output:

Original array: 10 7 8 9 1 5
Sorted array: 1 5 7 8 9 10

Question 46: Linear Search

Write a Java program that searches for an element in an array using linear search and returns its index.

Example Output:

Array: 10 20 30 40 50
Search for: 30
Element found at index: 2

Question 47: Binary Search

Write a Java program that searches for an element in a sorted array using binary search and returns its index.

Example Output:

Sorted array: 10 20 30 40 50
Search for: 40
Element found at index: 3

Question 48: Find the kth Smallest Element in an Array

Write a Java program that finds the kth smallest element in an array.

Example Output:

Array: 7 10 4 3 20 15
k = 3
kth smallest element: 7

Question 49: Find the kth Largest Element in an Array

Write a Java program that finds the kth largest element in an array.

Example Output:

Array: 7 10 4 3 20 15
k = 3
kth largest element: 10

Question 50: Find the Majority Element in an Array

Write a Java program that finds the majority element in an array (an element that appears more than n/2 times).

Example Output:

Array: 3 3 4 2 4 4 2 4 4
Majority element: 4

Submission Guidelines:

  1. Submit your answers in a well-documented Java code file.

  2. Provide comments in your code explaining the logic you used.

  3. Ensure your code is clean, properly formatted, and includes error handling where necessary.


Evaluation Criteria:

  • Correctness of logic and implementation.

  • Efficient use of array structures.

  • Code readability and documentation.

  • Handling of edge cases and errors.

  • Efficiency of the solution.

Good luck!

Last updated