#### Do you have a question? Post it now! No Registration Necessary. Now with pictures!

## Re: finding maximum

Friends ~

Thanks for the answers. Actually this is not a homework, I am just curious.

Anyways, here is my solution:

(a) in C:

int max(int a, int b)

{

if(a>b)

return a;

return b;

}

in Perl:

sub max

{

my $max = shift;

for(@_) {

$max=$

}

$max;

}

(b) in C:

#define NUMMAX (10)

int max(int a[NUMMAX])

{

int max = -1;

for(int i=0; i < NUMMAX;)

if(a[i++]>max)

max = a[i-1];

return max;

}

In Perl: the solution for (a) still works.

Conclusion: Perl is more expressive as a simple function for two arguments

often generalizes to any list.

Best greetings

Sandeep

On Wed, 01 Apr 2009 01:18:57 +0300, Phil Carmody wrote:

Thanks for the answers. Actually this is not a homework, I am just curious.

Anyways, here is my solution:

(a) in C:

int max(int a, int b)

{

if(a>b)

return a;

return b;

}

in Perl:

sub max

{

my $max = shift;

for(@_) {

$max=$

___ if($___>$max)}

$max;

}

(b) in C:

#define NUMMAX (10)

int max(int a[NUMMAX])

{

int max = -1;

for(int i=0; i < NUMMAX;)

if(a[i++]>max)

max = a[i-1];

return max;

}

In Perl: the solution for (a) still works.

Conclusion: Perl is more expressive as a simple function for two arguments

often generalizes to any list.

Best greetings

Sandeep

On Wed, 01 Apr 2009 01:18:57 +0300, Phil Carmody wrote:

## Re: finding maximum

a "set of numbers" is not defined as having less than 10 elements

I hope at least one of your numbers is positive.

Surely there's a more convoluted way to do this?

--hymie! http://lactose.homelinux.net/~hymie hymie@lactose.homelinux.net

------------------------ Without caffeine for 883 days ------------------------

## Re: finding maximum

If performance matters, C will probably be a little faster.

If your programmers who maintain the code are Perl programmers, then

Perl is the logical choice, and vice-versa.

In either case, unless you are processing a billion items, the

language choice does not matter much.

If a language is Turing complete (and most of them are) then something

simple like finding the max from a list can be done without much fuss.

Which language to choose to solve some programming problem usually

turns into a pissing match, especially when cross-posted to multiple

language groups.

For my opinion, I don't think either one is a better choice.

IMO-YMMV

## Re: finding maximum

sandeep wrote:

all of which are verbose compared to (max list):

>;; maximum of one integer

(max 3)

3

>

;; maximum of two integers

(max 483 6932487534)

6932487534

>

;; maximum of two floats and an integer

(max 4.72 49.1 -3)

49.100000000000001

>

;; maximum of a list, including several types

(max -3 49/3 5.37s-1 -34.3l-2 5.1)

49/3

>

;; minimum of the same list

(min -3 49/3 5.37s-1 -34.3l-2 5.1)

-3

>

;; print value of the list

>'(-3 49/3 5.37s-1 -34.3l-2 5.1)

(-3 49/3 0.537S0 -0.34299999999999997 5.0999999999999996)

Conclusion: lisp is more expressive, period.

all of which are verbose compared to (max list):

>;; maximum of one integer

(max 3)

3

>

;; maximum of two integers

(max 483 6932487534)

6932487534

>

;; maximum of two floats and an integer

(max 4.72 49.1 -3)

49.100000000000001

>

;; maximum of a list, including several types

(max -3 49/3 5.37s-1 -34.3l-2 5.1)

49/3

>

;; minimum of the same list

(min -3 49/3 5.37s-1 -34.3l-2 5.1)

-3

>

;; print value of the list

>'(-3 49/3 5.37s-1 -34.3l-2 5.1)

(-3 49/3 0.537S0 -0.34299999999999997 5.0999999999999996)

Conclusion: lisp is more expressive, period.

## Re: finding maximum

["Followup-To:" header set to comp.lang.perl.misc.]

Yeah right.

That is a load of hooey.

You are not comparing languages, you are comparing algorithms.

If you had used the high water mark algorithm in C's (a), then it would have

generalized just as well as the high water mark algorithm in Perl's (a) did.

#define NUMMAX (10)

int max(int a[NUMMAX])

{

int max = a[0];

for(int i = 1; i < NUMMAX; i++)

if(a[i] > max)

max = a[i];

return max;

}

--

Tad McClellan

email: perl -le "print scalar reverse qq/moc.noitatibaher0cmdat/"

## Re: finding maximum

...

http://search.cpan.org/~gbarr/Scalar-List-Utils-1.19/lib/List/Util.pm

Hopeless. We don't do your homework for you.

Sinan

--

(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:

http://www.rehabitation.com/clpmisc /

## Re: finding maximum

Negate both, find the minimum, and negate that minimum.

Be careful of corner cases: On many systems negating the most

negative integer fails to produce a positive integer (which

could lead to an incorrect answer), and on some systems there

are numbers X,Y such that X<=Y and Y<=X are

***both***false.

A subtle problem; I wish you luck with it.

Much harder, because in a set of N numbers there are

(N choose 2) = N * (N-1) / 2 pairs to consider. Even for a

fairly modest set size like N = 1000000 it can easily happen

that (N choose 2) exceeds the maximum value of the system's

integer.

So, I suggest a heuristic approach. Choose a pair of the

numbers at random, and determine their maximum by applying the

solution to (a). Then choose another pair at random and find

***their***maximum the same way. Now (and here's the crux) you

have formed the leftmost two pieces of a tree in which all the

numbers appear:

. .

. . .

/ \ .

max(A,B) max(C,D) .

/ \ / \ /

A B C D E ...

Using a stack (or possibly a priority queue) you can recursively

compute max(A,B,C,D) = max(max(A,B), max(C,D)), and then

max(A,B,C,D,E,F,G,H) = max(max(A,B,C,D), max(E,F,G,H))

= max(max(max(A,B), max(C,D)), max(max(E,F), max(G,H))) and

so on, populating the tree in a bottom-up left-to-right

fashion. Eventually, the root of the tree will be equal to the

maximum of the entire set, with a probability close to unity.

There's a strong analogy to the Mergesort algorithm: If you

consider the tree as a representation of a merge sort, then

the desired maximum of N elements is the unique element that

is not among the first N-1 emitted.

There is no such thing as "idiomatic C." There are only

"High C" (as practiced during the High C Era), and "Trade C"

(the patois that has taken hold among those who'd rather get

things done than argue about the proprieties). There are also

some debased C-ish dialects, notably "Rapper's C" or "CRap."

Ah! A much easier question! Answer: I would not do it

in any kind of Perl, idiotmatic or otherwise.

Both languages are Turing-complete, that is, capable of

expressing methods that solve all computable functions (subject

to the finiteness limits of the computers they execute on.) Also,

both languages are Virgil-complete, that is, capable of expressing

figurative and poetic thought, as in

C2shining(C)

and

Perl + Perl <- Swine

> Thx!

D'rn.

--

Eric Sosman

esosman@ieee-dot-org.invalid

#### Site Timeline

- » FAQ 2.12 Which magazines have Perl content?
- — Next thread in » PERL Discussions

- » FAQ 3.12 Where can I get perl-mode for emacs?
- — Previous thread in » PERL Discussions

- » s suffix question
- — Newest thread in » PERL Discussions

- » SSD partition alignment revisited
- — The site's Newest Thread. Posted in » Computer Hardware