473,473 Members | 1,768 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Finding 2 of the same elements in array


int find_index_of_min( float num[], int arraySize )
{
int index, min;

min = 0;
for( index = 1; index < arraySize; index++ )
{
if( num[min] > num[index] )
{
min = index;
}
}

return min;
}
How can i modify this code so that if an array contains 2 or more of
the exact elements which are the smallest, it will return the index
for both of those elements?
*---------------------------------*
Posted at: http://www.GroupSrv.com
*---------------------------------*

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 14 '05 #1
2 2253


slickn_sly wrote:
int find_index_of_min( float num[], int arraySize )
{
int index, min;

min = 0;
for( index = 1; index < arraySize; index++ )
{
if( num[min] > num[index] )
{
min = index;
}
}

return min;
}
How can i modify this code so that if an array contains 2 or more of
the exact elements which are the smallest, it will return the index
for both of those elements?


There are 2**arraySize - 1 possible return values,
so if you happen to know that arraySize is small you could
return a bit map of the positions holding the minimum.

For larger arraySize, you would need to create an array
to hold all the indices and return the array (you'd also need
a way to tell the caller how big the array is; perhaps you'd
stick a -1 after all the actual indices).

A different approach would be to break the problem up
into multiple calls. Use the function as it stands to find
the lowest-indexed occurrence of the minimum, and write a
second function to find the next occurrence of that same
value:

int find_next(float num[], int arraySize, int min) {
int pos;
for (pos = min; ++pos < arraySize; ) {
if (num[pos] == num[min])
return pos;
}
return -1; /* no more minima */
}

The caller could use the two functions to find the first
minimum and all its duplicates, something like

int min;
min = find_index_of_min(array, size);
printf ("Minimum %g appears at %d", array[min], min);
while ((min = find_next(array, size, min)) >= 0)
printf (" and %d", min);
printf ("\n");

--
Er*********@sun.com

Nov 14 '05 #2
slickn_sly wrote:
int find_index_of_min( float num[], int arraySize )
{
int index, min;

min = 0;
for( index = 1; index < arraySize; index++ )
{
if( num[min] > num[index] )
{
min = index;
}
}

return min;
}
How can i modify this code so that if an array contains 2 or more of
the exact elements which are the smallest, it will return the index
for both of those elements?


Sounds like homework. Maybe you should write the modifications
as far as you can get and come back with that code.
-Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Harsha Srinath | last post by:
Athough this might not be directly relayed to C syntax, I thought some of you may find this an interesting problem :- 1.One number, in an array integers from 1 to 1,000,000 is present twice. How...
19
by: gk245 | last post by:
Trying to write a program that will figure out if a number is perfect or not. Here is my logic: 1) Read in the number 2) Split it up (number - 1) 3) Put all the split up numbers into an...
2
by: equinox1248 | last post by:
Hi, I thought this would be answered several time in this group, but I couldn't find anything relevant. What would be the most efficient way of calculating sum of absolute values of two...
1
by: psmahesh | last post by:
Hi folks, I am comparing two arrays and removing matches from the second array from the first array. Can someone take a look at this code below and mention if this is okay and perhaps if there...
4
by: deLenn | last post by:
Hi, Does scipy have an equivalent to Matlab's 'find' function, to list the indices of all nonzero elements in a sparse matrix? Cheers.
4
by: Sandman | last post by:
Hi, So I have 2 arrays: one contains userids. It may look like: user_id =12, user_id =30, user_id =43 The other is a multi-dimensional array with fields like: user_info = Array (
4
by: goldy1980 | last post by:
Hi , I have two character arrays, I want to get the all the elements from 2nd array which are not in 1st array. Can some one help. thanks goldy
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
2
by: awaisworld13 | last post by:
I want to write a program which take input and save the poistions of the array elements in another array. let if i enter an array A= 4 A=1 A=2 A=3 now the output array will hold 1 2 3
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.