LinuxQuestions.org
Review your favorite Linux distribution.
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, 06: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, 06:04 AM   #2
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79
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, 06: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, 06:20 AM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
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 06:23 AM.
 
Old 12-16-2009, 06: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, 06:31 AM   #6
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
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, 06: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, 06:58 AM   #8
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
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 07:19 AM. Reason: PS. + typo
 
Old 12-16-2009, 07: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, 08: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, 09:33 AM   #11
sumeet inani
Member
 
Registered: Oct 2008
Posts: 895
Blog Entries: 26

Rep: Reputation: 48
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 10:10 AM.
 
Old 12-16-2009, 10: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, 12:10 PM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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, 06:36 PM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

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


All times are GMT -5. The time now is 10:13 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration