|
-
Oct 31st, 2002, 01:06 PM
#1
Thread Starter
Lively Member
lowest variable
I have 5 numbers and each is stored in its own variable. How do I find the lowest number?
-
Oct 31st, 2002, 01:22 PM
#2
Monday Morning Lunatic
Hint: Make an array of pointers, then loop through it to find the lowest.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 1st, 2002, 05:43 PM
#3
Frenzied Member
Code:
int a[5];
a[0]=23;
a[1]=13; ..... etc
printf("%d\n",min(a,5) );
...............
int min(int *whatever,int cnt){
int *ptr;
int result=0 // assume all numbers are positive
for(ptr=whatever;cnt;cnt--,ptr++)
if (*ptr>result) result=*ptr;
return result;
}
-
Nov 4th, 2002, 12:32 PM
#4
min is not a good choice for the name, there is a macro with that name.
In C++ you can use the min_element algorithm.
For parksie's suggestion, here's what you would do (in C++):
Code:
#include <algorithm>
using std::min_element;
int a, b, c, d, e; // your 5 integers
// parksie's suggestion:
int * ar[5] = {&a, &b, &c, &d, &e};
// my suggestion:
struct pred_minptr {
bool operator ()(int *pa, int *pb) {
return *pa < *pb;
};
};
// find the minimum:
int min = **min_element(ar, ar+5, pred_minptr());
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|