Page 86 - CTS - CSA TP - Volume 2
P. 86
COMPUTER SOFTWARE APPLICATION - CITS
Explanation:
• This program finds the maximum element in an array of integers.
• It initializes an array numbers with values {10, 5, 20, 8, 15}.
• It assumes the first element of the array as the maximum.
• Then, it iterates through the array starting from the second element, comparing each element with the
current maximum and updating max if a larger element is found.
• Finally, it prints the maximum element.
These examples demonstrate some basic operations with arrays in Java, such as summing up elements and
finding the maximum element. They showcase the use of loops for iteration and conditional statements for
making comparisons.
TASK 3: Sorting Array elements
Method 1: Java program that allows the user to input array elements through the keyboard and displays them in
sorted order using Quick Sort - Arrays.sort() method:
// using Quick Sort - Arrays.sort() method
import java.util.Arrays;
import java.util.Scanner;
public class ArraySort {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter the size of the array
System.out.print(“Enter the size of the array: “);
int size = scanner.nextInt();
// Create an array of the specified size
int[] numbers = new int[size];
// Prompt the user to enter array elements
System.out.println(“Enter the elements of the array:”);
for (int i = 0; i < size; i++) {
System.out.print(“Element “ + (i + 1) + “: “);
numbers[i] = scanner.nextInt();
}
// Sort the array
Arrays.sort(numbers);
// Display the sorted array
System.out.println(“Array elements in sorted order:”);
for (int i = 0; i < size; i++) {
71
CITS : IT & ITES - Computer Software Application - Exercise 93