LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-16-2009, 05:00 AM   #1
abhi1988srivastava
LQ Newbie
 
Registered: Dec 2009
Posts: 9

Rep: Reputation: 0
smallest and greatest without arrays


Hi

I am a begineer and want to know a simple question which i got stuck while stdying

Q> enter 5 numbers from user and find the greatest and smallest of them without putting them in array? describe two methods for the same.


please help me with this

Thanking in advance
 
Old 12-16-2009, 05:04 AM   #2
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,596

Rep: Reputation: 80
Is that homework?
Try to think how you would find the greatest and smallest number yourself if you were the computer and I presented the numbers one by one. Would you write them down or would you use another trick?
 
Old 12-16-2009, 05:16 AM   #3
abhi1988srivastava
LQ Newbie
 
Registered: Dec 2009
Posts: 9

Original Poster
Rep: Reputation: 0
No it wasnt an assignment. i just did the commands of linux and was doing questions with my friends , then we thought how to do find for this question without using array. i thought of doing it with two for loops but wasnt able to chalk out. even with sort command but not getting properly. If possible , please help me
 
Old 12-16-2009, 05:20 AM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,720

Rep: Reputation: 1703Reputation: 1703Reputation: 1703Reputation: 1703Reputation: 1703Reputation: 1703Reputation: 1703Reputation: 1703Reputation: 1703Reputation: 1703Reputation: 1703
Just check the numbers as they are entered.

Evo2.

PS. Oops, just realized that, that is what Agrouf was hinting at. Cat no longer in bag.

Last edited by evo2; 12-16-2009 at 05:23 AM.
 
Old 12-16-2009, 05:28 AM   #5
abhi1988srivastava
LQ Newbie
 
Registered: Dec 2009
Posts: 9

Original Poster
Rep: Reputation: 0
x=0
echo"enter 5 nos"
read a b c d e

for(i in $a $b $c $d $e)
do
for(j in $a $b $c $d $e)
do

if test $i -lt $j
then
x=$i
$i=$j
$j=x

fi
done

done


will it do it for me?

please help in this
 
Old 12-16-2009, 05:31 AM   #6
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,720

Rep: Reputation: 1703Reputation: 1703Reputation: 1703Reputation: 1703Reputation: 1703Reputation: 1703Reputation: 1703Reputation: 1703Reputation: 1703Reputation: 1703Reputation: 1703
Quote:
Originally Posted by abhi1988srivastava View Post
please help in this
You got a hint, and I basically gave you the answer. Reread what has been posted already.

Evo2.
 
Old 12-16-2009, 05:43 AM   #7
abhi1988srivastava
LQ Newbie
 
Registered: Dec 2009
Posts: 9

Original Poster
Rep: Reputation: 0
i think i can get my answer manipulating this.

may i know anyother method that doesnot have for loops and compact???
 
Old 12-16-2009, 05:58 AM   #8
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,720

Rep: Reputation: 1703Reputation: 1703Reputation: 1703Reputation: 1703Reputation: 1703Reputation: 1703Reputation: 1703Reputation: 1703Reputation: 1703Reputation: 1703Reputation: 1703
Ok, the way I would do it is have one loop that reads in the numbers one at a time: after reading each number check to see if it is larger than the largest read so far, or smaller than the the smallest so far: if it is store it. The main advantage to this approach is that you only ever have to store two numbers: regardless of how many numbers you are supposed to compare: be it 5 or 5 million.

Cheers,

Evo2.

PS. Notice that the point of the exercise is about algorithms not implementation.

Last edited by evo2; 12-16-2009 at 06:19 AM. Reason: PS. + typo
 
Old 12-16-2009, 06:05 AM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
no arrays?
Code:
echo "enter 5 nos"
read a b c d e
echo "biggest: " $(echo "$a $b $c $d $e" | tr " " "\n" | sort -rn | head -1)
echo "smallest: "$(echo "$a $b $c $d $e" | tr " " "\n" | sort -n | head -1)
 
Old 12-16-2009, 07:56 AM   #10
abhi1988srivastava
LQ Newbie
 
Registered: Dec 2009
Posts: 9

Original Poster
Rep: Reputation: 0
thanks a lot
but may i know whats the last "head -1" in piping.
sorry i am a novice
and code i have submitted above , can i modify it to get the answer i want. i again tried not getting.

But it was a great help thanks
 
Old 12-16-2009, 08:33 AM   #11
sumeet inani
Member
 
Registered: Oct 2008
Posts: 908
Blog Entries: 26

Rep: Reputation: 49
My logic is
Take first number & equate it to variables big & small.Now take next 4 numbers one by on & if currently entered number is > big then overwrite current on big.Similiarly if current number < small then overwrite on small.This is simple & obvious.After 5 numbers have been entered you have greatest in big & minimum in small.

If you find this post useful then thank me by pressing thumbs up button.Thank you.

Last edited by sumeet inani; 12-16-2009 at 09:10 AM.
 
Old 12-16-2009, 09:42 AM   #12
abhi1988srivastava
LQ Newbie
 
Registered: Dec 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ghostdog74 View Post
no arrays?
Code:
echo "enter 5 nos"
read a b c d e
echo "biggest: " $(echo "$a $b $c $d $e" | tr " " "\n" | sort -rn | head -1)
echo "smallest: "$(echo "$a $b $c $d $e" | tr " " "\n" | sort -n | head -1)
computer says incomplete command , may i know whats the flaw?
 
Old 12-16-2009, 11:10 AM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Works for me
Code:
c:~$ read a b c d e
1 2 3 4 5
c:~$ echo "biggest: " $(echo "$a $b $c $d $e" | tr " " "\n" | sort -rn | head -1)
biggest:  5
c:~$ echo "smallest: "$(echo "$a $b $c $d $e" | tr " " "\n" | sort -n | head -1)
smallest: 1
 
Old 12-16-2009, 05:36 PM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,307

Rep: Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743
'Incomplete cmd' sounds like you're typing that in at the cmd line. You need to put it in a script and execute that. If it doesn't work, post the script (in code tags)
 
  


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
Arrays of Structures to Arrays of Classes knobby67 Programming 1 01-01-2008 01:39 PM
What is the greatest software ever ? marozsas Linux - News 1 08-20-2006 08:37 AM
Question about outputing arrays with pointers, then just arrays... RHLinuxGUY Programming 1 04-12-2006 05:40 AM
the greatest site thanko LQ Suggestions & Feedback 3 12-28-2004 09:16 AM
Isn't Linux the greatest! perry General 8 06-12-2004 03:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:56 PM.

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