array index of smallest number in array?
Author Message
array index of smallest number in array?

How is the index of the smallest number in an array found?
Can it implemented in C without a another array to save the counter?

--

Fri, 04 Apr 2003 03:00:00 GMT
array index of smallest number in array?

Quote:
> How is the index of the smallest number in an array found?

By looking through the array and comparing the elements to the
stored value.

Quote:
> Can it implemented in C without a another array to save the counter?

Who needs an array to save the counter?  Isn't the counter just a
number?

Victor
--
--

Sat, 05 Apr 2003 03:00:00 GMT
array index of smallest number in array?
[moderator note:  sorry to the clc++ people, I didn't notice the crosspost.
C++ answers are not being approved to clcm. Followups set.  -mod]

Quote:
> How is the index of the smallest number in an array found?

Well, let's say that you have an array of integers like this --

int arr[100];
/* Fill it with values now. */

int iOfSmallest = 0;
int i = 0;
for(; i < sizeof(arr); i++) {
if(arr[i] < arr[iOfSmallest]) {
iOfSmallest = i;
}

Quote:
}

/* Now iOfSmallest contains the index you want. */

--
Manish Jethani
Software Developer, Geodesic

A manager asked a programmer how long it would take him to
finish the
program on which he was working.  "I will be finished tomorrow,"
the programmer
promptly replied.
"I think you are being unrealistic," said the manager.
"Truthfully,
how long will it take?"
The programmer thought for a moment.  "I have some features
that I wish
to add.  This will take at least two weeks," he finally said.
"Even that is too much to expect," insisted the manager, "I
will be
satisfied if you simply tell me when the program is complete."
The programmer agreed to this.
Several years later, the manager retired.  On the way to his
retirement lunch, he discovered the programmer asleep at his
terminal.
He had been programming all night.
-- Geoffrey James, "The Tao of Programming"
--

Sat, 05 Apr 2003 03:00:00 GMT
array index of smallest number in array?
It's a simple matter of iterating through the array with a for() loop,
testing each value to see if it is less than the
previous lowest value (prime the loop by setting the index of the lowest
value to 0).  I would do this:

/* Start code snippet */

int lowest_index = 0;
int i;

for( i = 0; i < sizeof(array); i++ ) {
if(array[i] < array[lowest_index])
lowest_index = i;

Quote:
}

/* End code snippet */

-Will

Quote:
> How is the index of the smallest number in an array found?
> Can it implemented in C without a another array to save the counter?

> --

--

Sat, 05 Apr 2003 03:00:00 GMT
array index of smallest number in array?

Quote:
>How is the index of the smallest number in an array found?
>Can it implemented in C without a another array to save the counter?

I don't see the problem.

int k = 0;
for ( i = 1; i < n; i++ )
if ( x[i] < x[k] )
k = i;

Of course there are more efficient ways of doing this, however,
efficiency is probably not an issue here.

Cheers,
Mike

--

address.  It is a mail alias.  Once spammed, the alias is deleted, and
the integer 'N' incremented.  Currently, mike[32,33] are valid.  If
email to mikeN bounces, try mikeN+1.
--

Sat, 05 Apr 2003 03:00:00 GMT
array index of smallest number in array?

Quote:
> How is the index of the smallest number in an array found?

You loop through the array, remembering what the smallest member was and
where you found it?

Richard
--

Sat, 05 Apr 2003 03:00:00 GMT
array index of smallest number in array?

Quote:

> int arr[100];
> /* Fill it with values now. */

> int iOfSmallest = 0;
> int i = 0;
> for(; i < sizeof(arr); i++) {
>     if(arr[i] < arr[iOfSmallest]) {

This will count past the end of the array if sizeof(int)>1, which it
often is.  You need (sizeof arr / sizeof arr[0]) or something like
that.  I prefer having a separate #define or enum for the size,
because then you don't have to find and fix all sizeofs if you later
turn the array to a pointer.
--

Sat, 05 Apr 2003 03:00:00 GMT

 Page 1 of 1 [ 7 post ]

Relevant Pages