Loading…
OS · Arrays · Program 27 · In the record
Write a shell script to print the largest element from an array
1#!/bin/bash2 3echo "Enter the number of elements:"4read n5 6echo "Enter $n elements:"7for (( i = 0; i < n; i++ ))8do9 read arr[i]10done11 12max=${arr[0]}13 14for (( i = 1; i < n; i++ ))15do16 if (( arr[i] > max ))17 then18 max=${arr[i]}19 fi20done21 22echo "Array: ${arr[*]}"23echo "Largest element = $max"5 ⏎ 12 ⏎ 45 ⏎ 7 ⏎ 89 ⏎ 23Enter the number of elements: 5 Enter 5 elements: 12 45 7 89 23 Array: 12 45 7 89 23 Largest element = 89
Run program 27 in your browser
bash · just-bash. Change the code or the input, run it, break it, and see what happens.