LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-21-2012, 07:15 PM   #1
ChozN
LQ Newbie
 
Registered: Dec 2011
Posts: 8

Rep: Reputation: Disabled
Exclamation largest numbers from an array - Shell/Bash Script


I need to calculate the biggest number in array size n.
Example: Users enter: 1 7 4 9
The biggest number is : 9
Simple but I'm really new on this on Shell/Bash! Anything will be great! Thanks!

Code:
#!/bin/bash 

printf "\tEnter a list of numbers, with spaces: "
read -a ARRAY

BIG=$1
for i in ${!ARRAY[@]} ; do
    if [ $i -gt $BIG ] ; then
        BIG=$i
    fi
done

echo "Largest number is $BIG"
 
Old 03-21-2012, 08:36 PM   #2
ChozN
LQ Newbie
 
Registered: Dec 2011
Posts: 8

Original Poster
Rep: Reputation: Disabled
solved please closed this thread! Thanks anyways!
 
Old 03-22-2012, 12:54 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Please show solution for others and then you can mark as SOLVED yourself
 
Old 03-22-2012, 02:13 AM   #4
ChozN
LQ Newbie
 
Registered: Dec 2011
Posts: 8

Original Poster
Rep: Reputation: Disabled
Code:
#!/opt/local/bin/bash
# get the biggest number in an array of integers

declare -a list # array of integers
declare -i biggest # biggest number of the array
declare -i current # current element of the array

read -a list -p "Enter each integer element separated by white spaces: " 
size=${#list[@]}

biggest=${list[0]} # first element of the array is supposed to be the biggest one
for current in ${list[@]}; do # loop through the array
    if [ $current -gt $biggest ]; then
        biggest=$current
    fi
done

echo -e "\nThe biggest integer number in the array is: $biggest";
This is one way to solution from [chapeupreto] on other forum!
 
Old 03-22-2012, 03:18 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Or maybe a little simpler:
Code:
#!/bin/bash

read -a list -p "Enter each integer element separated by white spaces: "

echo "The largest number is: $(printf "%d\n" ${list[@]} | sort -n | tail -n1)"
 
1 members found this post helpful.
Old 03-22-2012, 11:18 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I'd personally prefer the in-shell solution, as long as the number list isn't huge.

The if statement could be shortened to a single line though, and it should ideally be using an arithmetic expression test instead.

If you loop through the array index numbers you can get the position as well as the value.

Code:
biggest=${list[0]} position=0

for i in "${!list[@]}"; do
	(( ${list[i]} > biggest )) && biggest=${list[i]} position=$i
done

echo "Largest value is $biggest (Array entry no: $position)"
Finally, it might be a good idea to include a test for non-integer input first.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script to find the sum of numbers associated with each character in a string Aagam Linux - Newbie 22 02-07-2012 08:59 AM
[SOLVED] shell script help: copying directory list into an array and then accessing the array richman1234 Linux - Newbie 6 07-25-2010 11:19 PM
shell script to batch process-modify ip numbers in files. SupermanInNY Programming 5 08-13-2009 03:03 AM
smallest and largest of three numbers emation06 Programming 10 10-21-2006 11:30 PM
Shell scripting - Adding many numbers in an array Stingreen Linux - General 2 10-25-2004 05:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration