Min

G

Gregc.

Hi

I am trying to find the min of a list of numbers. For example, if a
user enters 10 numbers 1,2,3,4,5,6,7,8,9,9 then the min would be 1.
I've looked as past posts on the topic, and there was one that
mentioned macro's. Could someone explain how do macro's (if at all)
work within the C programming environment.

Thanks

Greg
 
I

Ian Collins

Gregc. said:
Hi

I am trying to find the min of a list of numbers. For example, if a
user enters 10 numbers 1,2,3,4,5,6,7,8,9,9 then the min would be 1.
I've looked as past posts on the topic, and there was one that
mentioned macro's. Could someone explain how do macro's (if at all)
work within the C programming environment.
How do you think you would do it?

Write down the steps, the convert them to code. Come back here if you
have problems with the code.

My (possibly minority) advice on macros is for most applications,
ignorance is bliss. Learn to implement your design cleanly without them.
 
O

osmium

Gregc. said:
I am trying to find the min of a list of numbers. For example, if a
user enters 10 numbers 1,2,3,4,5,6,7,8,9,9 then the min would be 1.
I've looked as past posts on the topic, and there was one that
mentioned macro's. Could someone explain how do macro's (if at all)
work within the C programming environment.

The mention you saw was most likely a way to compare two numbers and use the
same code on either an int or a double, for example. That isn't a problem
needing solving in the question in front of you. Macros are, IMO, kind of
advanced. and I suggest you learn to solve simple problems like the one you
pose here first. Later on you can delve into macros. Doing so now would
just be a confusing digression.
 
E

Eric Sosman

Gregc. wrote On 05/03/06 17:40,:
Hi

I am trying to find the min of a list of numbers. For example, if a
user enters 10 numbers 1,2,3,4,5,6,7,8,9,9 then the min would be 1.
I've looked as past posts on the topic, and there was one that
mentioned macro's. Could someone explain how do macro's (if at all)
work within the C programming environment.

A guess: You're thinking of using a macro that
calculates the minimum of two numbers, e.g.,

#define MIN(a,b) ((a) < (b) ? (a) : (b))

.... and then extending it via "nesting" to form macros
that calculate the minima of larger populations

#define MIN4(a,b,c,d) MIN(MIN(a,b), MIN(c,d))

#define MIN8(a,b,c,d,e,f,g,h) \
MIN(MIN4(a,b,c,d), MIN4(e,f,g,h))

#define MIN10(a,b,c,d,e,f,g,h,i,j) \
MIN(MIN8(a,b,c,d,e,f,g,h), MIN(i,j))

You'd then apply this super-macro to your list of ten
numbers

x = MIN10(a[0], a[1], a[2], a[3], a[4],
a[5], a[6], a[7], a[8], a[9]);

If that's what you're intending, my advice is "Don't!"
That innocent-appearing final line expands to

x=((((((((a[0])<(a[1])?(a[0]):(a[1])))<(((a[2])<(a[3])?(
a[2]):(a[3])))?(((a[0])<(a[1])?(a[0]):(a[1]))):(((a[2])<
(a[3])?(a[2]):(a[3])))))<(((((a[4])<(a[5])?(a[4]):(a[5])
))<(((a[6])<(a[7])?(a[6]):(a[7])))?(((a[4])<(a[5])?(a[4]
):(a[5]))):(((a[6])<(a[7])?(a[6]):(a[7])))))?(((((a[0])<
(a[1])?(a[0]):(a[1])))<(((a[2])<(a[3])?(a[2]):(a[3])))?(
((a[0])<(a[1])?(a[0]):(a[1]))):(((a[2])<(a[3])?(a[2]):(a
[3]))))):(((((a[4])<(a[5])?(a[4]):(a[5])))<(((a[6])<(a[7
])?(a[6]):(a[7])))?(((a[4])<(a[5])?(a[4]):(a[5]))):(((a[
6])<(a[7])?(a[6]):(a[7])))))))<(((a[8])<(a[9])?(a[8]):(a
[9])))?(((((((a[0])<(a[1])?(a[0]):(a[1])))<(((a[2])<(a[3
])?(a[2]):(a[3])))?(((a[0])<(a[1])?(a[0]):(a[1]))):(((a[
2])<(a[3])?(a[2]):(a[3])))))<(((((a[4])<(a[5])?(a[4]):(a
[5])))<(((a[6])<(a[7])?(a[6]):(a[7])))?(((a[4])<(a[5])?(
a[4]):(a[5]))):(((a[6])<(a[7])?(a[6]):(a[7])))))?(((((a[
0])<(a[1])?(a[0]):(a[1])))<(((a[2])<(a[3])?(a[2]):(a[3])
))?(((a[0])<(a[1])?(a[0]):(a[1]))):(((a[2])<(a[3])?(a[2]
):(a[3]))))):(((((a[4])<(a[5])?(a[4]):(a[5])))<(((a[6])<
(a[7])?(a[6]):(a[7])))?(((a[4])<(a[5])?(a[4]):(a[5]))):(
((a[6])<(a[7])?(a[6]):(a[7]))))))):(((a[8])<(a[9])?(a[8]
):(a[9]))));

This expression contains forty-five relational operators with
two operands each, forty-five ternary operators with three
operands each, and one hundred thirty-six references to the
array `a'. Even if it doesn't choke the compiler, it will
surely produce more (and possibly slower) code than the obvious
for-loop. Also, you wind up with inflexible code that deals
only with ten-element lists: you can't adapt to an eleven- or
nine- or hundred-element list without changing the source.

The KISS principle applies here: "Keep It Simple, Stupid!"
 
O

osmium

Eric Sosman said:
Gregc. wrote On 05/03/06 17:40,:
Hi

I am trying to find the min of a list of numbers. For example, if a
user enters 10 numbers 1,2,3,4,5,6,7,8,9,9 then the min would be 1.
I've looked as past posts on the topic, and there was one that
mentioned macro's. Could someone explain how do macro's (if at all)
work within the C programming environment.

A guess: You're thinking of using a macro that
calculates the minimum of two numbers, e.g.,

#define MIN(a,b) ((a) < (b) ? (a) : (b))

... and then extending it via "nesting" to form macros
that calculate the minima of larger populations

#define MIN4(a,b,c,d) MIN(MIN(a,b), MIN(c,d))

#define MIN8(a,b,c,d,e,f,g,h) \
MIN(MIN4(a,b,c,d), MIN4(e,f,g,h))

#define MIN10(a,b,c,d,e,f,g,h,i,j) \
MIN(MIN8(a,b,c,d,e,f,g,h), MIN(i,j))

You'd then apply this super-macro to your list of ten
numbers

x = MIN10(a[0], a[1], a[2], a[3], a[4],
a[5], a[6], a[7], a[8], a[9]);

If that's what you're intending, my advice is "Don't!"
That innocent-appearing final line expands to

x=((((((((a[0])<(a[1])?(a[0]):(a[1])))<(((a[2])<(a[3])?(
a[2]):(a[3])))?(((a[0])<(a[1])?(a[0]):(a[1]))):(((a[2])<
(a[3])?(a[2]):(a[3])))))<(((((a[4])<(a[5])?(a[4]):(a[5])
))<(((a[6])<(a[7])?(a[6]):(a[7])))?(((a[4])<(a[5])?(a[4]
):(a[5]))):(((a[6])<(a[7])?(a[6]):(a[7])))))?(((((a[0])<
(a[1])?(a[0]):(a[1])))<(((a[2])<(a[3])?(a[2]):(a[3])))?(
((a[0])<(a[1])?(a[0]):(a[1]))):(((a[2])<(a[3])?(a[2]):(a
[3]))))):(((((a[4])<(a[5])?(a[4]):(a[5])))<(((a[6])<(a[7
])?(a[6]):(a[7])))?(((a[4])<(a[5])?(a[4]):(a[5]))):(((a[
6])<(a[7])?(a[6]):(a[7])))))))<(((a[8])<(a[9])?(a[8]):(a
[9])))?(((((((a[0])<(a[1])?(a[0]):(a[1])))<(((a[2])<(a[3
])?(a[2]):(a[3])))?(((a[0])<(a[1])?(a[0]):(a[1]))):(((a[
2])<(a[3])?(a[2]):(a[3])))))<(((((a[4])<(a[5])?(a[4]):(a
[5])))<(((a[6])<(a[7])?(a[6]):(a[7])))?(((a[4])<(a[5])?(
a[4]):(a[5]))):(((a[6])<(a[7])?(a[6]):(a[7])))))?(((((a[
0])<(a[1])?(a[0]):(a[1])))<(((a[2])<(a[3])?(a[2]):(a[3])
))?(((a[0])<(a[1])?(a[0]):(a[1]))):(((a[2])<(a[3])?(a[2]
):(a[3]))))):(((((a[4])<(a[5])?(a[4]):(a[5])))<(((a[6])<
(a[7])?(a[6]):(a[7])))?(((a[4])<(a[5])?(a[4]):(a[5]))):(
((a[6])<(a[7])?(a[6]):(a[7]))))))):(((a[8])<(a[9])?(a[8]
):(a[9]))));

This expression contains forty-five relational operators with
two operands each, forty-five ternary operators with three
operands each, and one hundred thirty-six references to the
array `a'. Even if it doesn't choke the compiler, it will
surely produce more (and possibly slower) code than the obvious
for-loop. Also, you wind up with inflexible code that deals
only with ten-element lists: you can't adapt to an eleven- or
nine- or hundred-element list without changing the source.

The KISS principle applies here: "Keep It Simple, Stupid!"

Nice! That's kind of what I was getting at.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,966
Members
47,515
Latest member
Harvey7327

Latest Threads

Top