tags:

views:

366

answers:

7

I have written following code in Perl:

#!/usr/bin/perl

@array = (3,6,8,1,2);

my $second_largest = 0;
my $largest = 0;

for (@array)
{
   if($_ > $largest)
   {
       $second_largest = $largest;
       $largest = $_;
   }
   if($_ > $second_largest && $_ < $largest)
   {
        $second_largest = $_;
   }
}
print "Second largest::".$second_largest;
print "largest::".$largest;

But I need a general code to find out "Nth" largest and smallest number of an array

Please help me

A: 

Just sort the array - the Nth largest will then be at the (SIZE - Nth) location.

Paul R
Sorry I forget to mention that I need this with out using sort functionSo can some one plz tell me how to get the value with out using sorting function
@user136104: maybe if you do something about your 0% accept rate people will be a little more inclined to help you with your assignment ?
Paul R
Update your question with clarifying information. Don't bury that stuff in comments.
brian d foy
A: 
use strict;
use warnings;
use Data::Dumper;
my @array=(1,2,10,0,20,45,12);
my @new=sort (@array);
print "The largest number is:$new[$#new]\n";
print "The second largest number is:$new[$#new-1]\n";
muruga
Please don't provide complete solutions to homework problems - just helpful hints.
Paul R
It's not marked homework and the questioner has some real-looking questions in their portfolio that are nothing like homework. And, frankly, if anyone wants to cheat, that's their problem. They're foolish to think sites like these aren't monitored.
paxdiablo
True - it just smells like homework to me - that and the 0% accept rate makes me think: student.
Paul R
Sorry I forget to mention that I need this with out using sort function
So can some one plz tell me how to get the value with out using sorting function
@user136104: Why can't you use `sort` ? Is it because this is a homework exercise, by any chance ? If so, please tag appropriately.
Paul R
Yes, someone can. But nobody should till you answer Paul's Q.
DVK
Update your question with clarifying information. Don't bury that stuff in comments
brian d foy
+1  A: 

You need to look into the sort function. By creating a new array as the result of sorting your given one, you can then just use subscripting to get the Nth smallest and largest. Such as the code:

@array = (3,6,8,1,2);

@sorted = sort(@array);
print "@array " . "\n";
print "@sorted " . "\n";

print @sorted[1]  . "\n"; #  1 is the second smallest (since we're zero-based).
print @sorted[-2] . "\n"; # -2 is the second largest.

which outputs:

3 6 8 1 2
1 2 3 6 8
2
6

Be warned, if this is homework, you will almost certainly be caught for plagiarism if you use that code. This is a public forum that your educators will be able to see as well as you can. Read it, learn from it, write your own.

You may also want to add error checking code in case you try to find the 10th largest number in a seven-element array.

paxdiablo
+5  A: 

Most simple idea without using sort: for each element of the array: count the number of greater and smaller elements. If there are exactly N-1 greater elements, you have found your solution. And now do the rest of your homework by yourself, please.

Doc Brown
+1  A: 

You can use the following code also,

use strict;
use warnings;
use Data::Dumper;
use Sort::Array qw(Sort_Table);
my @array=(1,2,10,0,20,45,12);

my @new =Sort_Table(
    cols      => '1',
    field     => '1',
    sorting   => 'ascending',
    structure => 'csv',
    data      => \@array,
);

print "The largest number is:$new[$#new]\n";
print "The second largest number is:$new[$#new-1]\n";
muruga
A: 

Even though you say that you can't use Perl's sort, you can implement your own sort. Merge and quick sorts are well-known, explained in detail in many places, and really aren't a lot of code. Make friends with Google.

Once sorted, you merely go to the right array index.

If I were making this assignment to students, I'd give the highest marks to someone who:

  • sorted the array
  • maintained the sorted version
  • allowed repeated inputs to select different values of N without re-sorting
brian d foy