three numbers sorting

three numbers sorting

Post by Stefan Da » Wed, 22 Oct 2003 08:34:12



Hi all,

Write a shell script which reads in three numbers as arguments from the keyboard,
   compare them, and list them in an ascending order.

     [$] ./script2
     Enter three numbers: 20 10 85

     Your numbers in ascending order are: 20 30 85

 
 
 

three numbers sorting

Post by Ben » Wed, 22 Oct 2003 08:35:54



> Hi all,

> Write a shell script which reads in three numbers as arguments from the keyboard,
>    compare them, and list them in an ascending order.

>      [$] ./script2
>      Enter three numbers: 20 10 85

>      Your numbers in ascending order are: 20 30 85

If you do it first, then I will.  You still haven't said which shell.

regards,
Ben

--
BTW. I can be contacted at Username:newsgroup4.replies.benaltw
Domain:xoxy.net

 
 
 

three numbers sorting

Post by Chris F.A. Johnso » Wed, 22 Oct 2003 13:55:03



> Hi all,

> Write a shell script which reads in three numbers as arguments from the keyboard,
>    compare them, and list them in an ascending order.

>      [$] ./script2
>      Enter three numbers: 20 10 85

>      Your numbers in ascending order are: 20 30 85

    If that's your professor's sample run, get a new professor. (10
    was entered, but 30 shows up in the output.)

    What do you need help with?

         Reading the numbers from the keyboard?

         Sorting the numbers?

         Comparing two numbers?

         Displaying the result?

    And:

         What shell are you using?

         Are you allowed to use external commands?

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2003, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

three numbers sorting

Post by Stefan Da » Wed, 22 Oct 2003 15:49:05


hi,
It is both in C shell or Bourne shell.. ? tried to do many times
but I could not manage the problem. As I mentioned, I am new in shell
script programming and need to learn basic scripts so as to enhance my
coding ability..

please, kindly help..

Regards
Stephan

 
 
 

three numbers sorting

Post by Chris F.A. Johnso » Wed, 22 Oct 2003 16:07:58



> hi,
> It is both in C shell or Bourne shell.. ı tried to do many times
> but I could not manage the problem. As I mentioned, I am new in shell
> script programming and need to learn basic scripts so as to enhance my
> coding ability..

     Please read my answers to your other posts.

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2003, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

three numbers sorting

Post by Stephane CHAZELA » Wed, 22 Oct 2003 16:22:57



> Write a shell script which reads in three numbers as arguments from the keyboard,
>    compare them, and list them in an ascending order.

>      [$] ./script2
>      Enter three numbers: 20 10 85

>      Your numbers in ascending order are: 20 30 85

#! /usr/bin/env zsh
read -A 'l?Enter three numbers: '
(($#l!=3||${#l:#*[^0-9]*}!=3))&&{
  print >&2 "I expected three numbers"
  exit 1
Quote:}

print Your numbers in ascending order are: ${(on)l}

--
Stphane         [real-mailto: "Stephane.Chazelas" at "free.fr"]

 
 
 

three numbers sorting

Post by Stephane CHAZELA » Wed, 22 Oct 2003 18:15:47


[...]

Quote:> #! /usr/bin/env zsh
> read -A 'l?Enter three numbers: '
> (($#l!=3||${#l:#*[^0-9]*}!=3))&&{
>   print >&2 "I expected three numbers"
>   exit 1
> }
> print Your numbers in ascending order are: ${(on)l}

Well, this only works with positive integer numbers.

#! /usr/bin/awk -f
function is_number(n) {
  return n ~ /^[-+]?[0-9]*(\.?[0-9]|[0-9]\.)[0-9]*([eE][-+]?[0-9]+)?$/

Quote:}

BEGIN {
  printf("Enter three numbers: ")
  getline
  if (NF != 3 || ! is_number($1) || ! is_number($2) || ! is_number($3)) {
    print "I expected three numbers"
    exit 1
  }
  printf("Your numbers in ascending order are: ")
  if ($2 <= $1)
    $0 = $2 " " $1 " " $3
  if ($3 <= $1)
    $0 = $3 " " $1 " " $2
  else if ($3 <= $2)
    $0 = $1 " " $2 " " $3
  print

Quote:}

~$ ./script
Enter three numbers: 1e-34 -3E12 3.23
Your numbers in ascending order are: -3e12 1e-34 3.23

--
Stphane                      ["Stephane.Chazelas" at "free.fr"]

 
 
 

three numbers sorting

Post by Stephane CHAZELA » Wed, 22 Oct 2003 18:29:15


[...]

Quote:>   else if ($3 <= $2)
>     $0 = $1 " " $2 " " $3
>   print

$0 = $1 " " $3 " " $2

of course

--
Stphane                      ["Stephane.Chazelas" at "free.fr"]

 
 
 

three numbers sorting

Post by Ed Morto » Wed, 22 Oct 2003 23:04:55



> Hi all,

> Write a shell script which reads in three numbers as arguments from the keyboard,
>    compare them, and list them in an ascending order.

>      [$] ./script2
>      Enter three numbers: 20 10 85

>      Your numbers in ascending order are: 20 30 85

Uhhh, folks - should we really be doing this persons homework for them?
I'm all for giving people a helping hand, but in this persons postings
there's been no attempt to do the homework themselves and they've been
getting pretty complete solutions. In fact, the solutions are so precise
that if they hand this in, it'll be a red flag to their professor that
they didn't do it themselves, especially when they can't explain in
detail what the scripts do.

        Ed.

 
 
 

three numbers sorting

Post by Ben » Wed, 22 Oct 2003 23:18:51



> Uhhh, folks - should we really be doing this persons homework for them?
> I'm all for giving people a helping hand, but in this persons postings
> there's been no attempt to do the homework themselves and they've been
> getting pretty complete solutions. In fact, the solutions are so precise
> that if they hand this in, it'll be a red flag to their professor that
> they didn't do it themselves, especially when they can't explain in
> detail what the scripts do.

I think Stephen intentionally made his scripts that way so it would be a
red flag to the professor if the person used them. On the other hand... :-)

Ben

--
BTW. I can be contacted at Username:newsgroup4.replies.benaltw
Domain:xoxy.net

 
 
 

three numbers sorting

Post by Stephane CHAZELA » Wed, 22 Oct 2003 23:56:02


[...]

Quote:> I think Stephen intentionally made his scripts that way so it would be a
          Stephane
> red flag to the professor if the person used them. On the other hand... :-)

Yes, when it's too obvious that OP didn't make any effort, I
like to post unusual answers. If not to the OP, they can be
useful to someone as some neat tricks. And the OP needs to
understand it if he wants to use it.

--
Stphane                      ["Stephane.Chazelas" at "free.fr"]

 
 
 

three numbers sorting

Post by William Par » Thu, 23 Oct 2003 06:06:29



> Hi all,

> Write a shell script which reads in three numbers as arguments from the keyboard,
>   compare them, and list them in an ascending order.

>     [$] ./script2
>     Enter three numbers: 20 10 85

>     Your numbers in ascending order are: 20 30 85

echo -n "Enter three numbers: "
echo "Your numbers in ascending order are: "
tr ' ' '\n' | sort -n | paste -s -d ' '

--

Linux solution for data management and processing.

 
 
 

three numbers sorting

Post by Harr » Thu, 23 Oct 2003 07:17:48


Quote:Stefan Dask wrote...
>Write a shell script which reads in three numbers as arguments from
>the keyboard, compare them, and list them in an ascending order.

Assume input are really _arguments_ :

#!/bin/sh
echo $* | awk -F" " '{ for(i=1;i<=NF;i++) { printf "\n" $i }}' | sort -n

$ ./xx 10 9 8 12 13

8
9
10
12
13

 
 
 

1. SORT command > sorting decimal numbers in columns on a file not working !

Hi. I try to sort a column on a file containing decimal numbers. I use
Sse Linux 9.1. Then I try to do this:


doasidpoas   opipo     7.8
kjkljkljklji   kjklj   35.7
kljkljkl       kljkl   2.3
dopasidp       poipo   0.2
kjkljlkl       kjkl    0.0
kljkljkjl      ljlk    0.8
jklhkljkl      kjkl    0.0
kljkljkljl     kjkl    0.1

dopasidp       poipo   0.2
jklhkljkl      kjkl    0.0
kjkljlkl       kjkl    0.0
kljkljkjl      ljlk    0.8
kljkljkljl     kjkl    0.1
kljkljkl       kljkl   2.3
doasidpoas   opipo     7.8
kjkljkljklji   kjklj   35.7

kjkljkljklji   kjklj   35.7
doasidpoas   opipo     7.8
kljkljkl       kljkl   2.3
kljkljkljl     kjkl    0.1
kljkljkjl      ljlk    0.8
kjkljlkl       kjkl    0.0
jklhkljkl      kjkl    0.0
dopasidp       poipo   0.2

As you can see the sorting order is not correct. What am I doing wrong ?
I also tried to use '-g' option instead of '-n' but doesnt work.

Any idea ?

Thanks
Alberto

2. Radvd sender configures itself (but shouldn't)...

3. sort sort: 0653-657 A write error occurred while sorting (4.1.3)

4. Socket Behavior Questions ?

5. Third Party Sort Programs

6. NetBSD 1.5 and sshd

7. How to separate numbers in three-digit groups in C

8. Formatting 40G IDE on Ultra10

9. . number & pretty-print a sorted file

10. Sorting articles of variable number of lines

11. number sort

12. Sorting articles of variable number of lines

13. No luck sorting numbers on a line...