Write a program that finds the largest element in an array.
Program in C
C
#include <stdio.h>
int findLargestElement(int arr[], int size) {
int largest = arr[0]; // Assume the first element is the largest
for (int i = 1; i < size; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
}
return largest;
}
int main() {
int arr[] = {5, 2, 9, 1, 7};
int size = sizeof(arr) / sizeof(arr[0]);
int largest = findLargestElement(arr, size);
printf("The largest element in the array is: %d\n", largest);
return 0;
}
Explanation:
- In this program, the findLargestElement function takes an array arr and its size size as parameters.
- It assumes the first element of the array is the largest and then iterates through the remaining elements, updating the largest variable whenever a larger element is found. Finally, it returns the largest element.
- In the main function, an array arr is declared with some example values.
- The size of the array is calculated using sizeof operator.
- Then, the findLargestElement function is called with the array and its size, and the largest element returned is printed to the console.
Output:
C
The largest element in the array is: 9
Program in Java
Java
public class LargestElementInArray {
public static int findLargestElement(int[] arr, int size) {
int largest = arr[0]; // Assume the first element is the largest
for (int i = 1; i < size; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
}
return largest;
}
public static void main(String[] args) {
int[] arr = {5, 2, 9, 1, 7};
int size = arr.length;
int largest = findLargestElement(arr, size);
System.out.printf("The largest element in the array is: %d\n", largest);
}
}
Explanation:
- The program starts by defining a public class named LargestElementInArray.
- Inside the class, the program declares a static method called findLargestElement that takes two parameters: an integer array arr and the size of the array size.
- Within the findLargestElement method, a variable largest is initialized with the value of the first element in the array arr[0]. This assumes that the first element is the largest element.
- The method then iterates over the array from the second element (i = 1) to the last element (i < size).
- Inside the loop, each element of the array arr[i] is compared to the current largest value. If arr[i] is greater than largest, the largest value is updated to arr[i].
- After the loop finishes, the largest value is returned as the result of the method.
- The program also defines a main method, which is the entry point of the program.
- Inside the main method, an integer array arr is declared and initialized with values {5, 2, 9, 1, 7}.
- The size of the array is determined using the length property of the array (int size = arr.length).
- The findLargestElement method is called with the arr array and its size as arguments, and the result is stored in the largest variable.
- Finally, the program uses System.out.printf() to print the result to the console, displaying the message “The largest element in the array is: ” followed by the value of largest.
Output
The largest element in the array is: 9