LinuxQuestions.org
Visit Jeremy's Blog.
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 01-31-2003, 04:38 AM   #1
zibertron
LQ Newbie
 
Registered: Jan 2003
Location: Southern Cali- L A
Distribution: Red Hat 7.1
Posts: 7

Rep: Reputation: 0
Red face is homework,but been racking brain since 8pm


I need to make a script that accepts ten numbers and displays the highest number entered, so far i have the following:
#!/bin/bash
echo " please enter ten numbers"
echo "enter num 1"
read num1
echo "enter num 2"
read num2
echo "enter num 3" #im only doing 3 numbers now to test it first
read num3
if [ $num1 -gt $num2 -a gt $num3 ;; $num2 -gt $num1 -a $num3 ;; $num3 -gt $num1 -a -gt $num2 ]
then
echo " $num1 is the highest number"
elif
echo " $num2 is the highest number"
else
echo " $num3 is the highest number"
fi
# or what else could i do?

Last edited by zibertron; 01-31-2003 at 05:20 AM.
 
Old 01-31-2003, 04:48 AM   #2
FredrikN
Member
 
Registered: Nov 2001
Location: Sweden
Distribution: GNU/Linux since -97
Posts: 149

Rep: Reputation: 15
To sort 10 number with if selections is a bad idé

A better solution is to store all values in some array and then use
the selection sort algorithm.

Scan all valuses to find the smallest value, exchange that value with the value in the first position. Then scan the list again(but do not include the first value) to find the smallest value then exchange it with the value in the second position and the continue until the end of the array.
 
Old 01-31-2003, 04:54 AM   #3
FredrikN
Member
 
Registered: Nov 2001
Location: Sweden
Distribution: GNU/Linux since -97
Posts: 149

Rep: Reputation: 15
With Java it's pice a cake :-)

There is a sort metod in the Arrayclass that will do the job easy for you.

Just use the sort metod like Arrays.sort(the_Array) and the it will be sorted
but sure, it's good if you also can do it by yourself
 
Old 01-31-2003, 05:01 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,414

Rep: Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966
this sounds like homework... if it is please do not ask similar questions here. cheats never prosper!

anyway, it's even easier with perl.... sort()
 
Old 01-31-2003, 05:08 AM   #5
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
You need to use a loop and do a comparison.
 
Old 01-31-2003, 05:13 AM   #6
0x0001
Member
 
Registered: Sep 2002
Distribution: Slackware 8.1
Posts: 84

Rep: Reputation: 15
It sounds like homework for sure, but I guess it is ok to give pointers, as long as it serves that he learns somehting.

In this spirit:
Unix/Linux has a tools approach to problems, which means that there are lot of small programms which can be added together (with pipes for example) to solve more complex problems, in this case I would advise that you look at sort(1).

Another good read about bash scripting is the "Advanced Bash-Scripting Guide" (http://www.tldp.org/guides.html#abs) at the
linux documentation project (http://www.tldp.org )
 
Old 01-31-2003, 05:24 AM   #7
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
I don't know shell scripting but this is the general idea:

Code:
#!/bin/bash

biggest = 0

while [ read input from command line ]

do
        if [ input > biggest ]
            then
            biggest = input;
        fi

done

echo biggest
 
Old 01-31-2003, 11:52 AM   #8
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Code:
#!/bin/bash
echo "Please enter ANY amount of numbers, each on their own line."
echo "Press CTRL-D when done."
echo

HIGHEST=$(cat | sort -n -r | head -1)

echo "Highest number you entered = $HIGHEST"
echo
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Sorry folks need help setting up Peleus Linux - Wireless Networking 1 11-04-2005 07:15 AM
hello folks Ha1f LinuxQuestions.org Member Intro 2 07-02-2005 02:46 PM
Hello folks :-) MikeAtVillage LinuxQuestions.org Member Intro 1 05-05-2005 11:08 AM
Just thanking folks here Dcrusoe LinuxQuestions.org Member Success Stories 3 01-18-2005 04:56 PM
G'day Folks chris Linux - Networking 0 05-22-2003 09:43 AM


All times are GMT -5. The time now is 06:35 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration