# finding maximum

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

•  Subject
• Author
• Posted on
Hello Friends ~

How would you find the maximum of
(a) two numbers
(b) a set of numbers
in idiomatic C? How would you do it in idiomatic Perl? Use this as a basis
for a discussion of the differences in expressive power of the two
languages.

Thx!

Sandeep

## Re: finding maximum

sandeep wrote:

OK. To make it easier for you, can you tell us

We will send him the answer directly.

thanks

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32

## Re: finding maximum

Can you let us have your teacher's email address, so that we
can get your homework answers to him directly, to save you the
effort.

Phil
--
Marijuana is indeed a dangerous drug.
It causes governments to wage war against their own people.
-- Dave Seaman (sci.math, 19 Mar 2009)

## Re: finding maximum

wrote:

Or perhaps it's flamebait disguised as homework.

## Re: finding maximum

The poster's address should have been a big clue.

I have that one killfiled.

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

Congratulations!

-sln

## 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=\$_ 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

In our last episode, the evil Dr. Lacto had captured our hero,

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.

## Re: finding maximum

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;
}

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

## Re: finding maximum

sandeep wrote:

In C,
the conditional operator would be more idiomatic
for two numbers of any type.

#define max(A, B) ((B) > (A) ? B : (A))

--
pete

## Re: finding maximum

pete wrote:

Avoiding multiple evaluation is a problem with this.  It needs to be a
bit longer.

## Re: finding maximum

You're saying that it can be written without evaluating the arguments
more than once? How?

## Re: finding maximum

sandeep wrote:

But where's your solution in INTERCAL?

--
RGB

## Re: finding maximum

sandeep wrote:

A better idea would be for you to do the homework that you've been
assigned. The assignment was for a reason, that is to teach you some
programming concepts and techniques.

Brian

--
Day 56 of the "no grouchy usenet posts" project

## 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

What is "idiomatic" is that a washing machine gone bad?

-sln

## Re: finding maximum

sandeep wrote:

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
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

## Re: finding maximum

Leaving out "Tenne C" is inexcusable.

http://www.netfunny.com/rhf/jokes/88q3/0tenny.html

--