473,786 Members | 2,615 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Finding 2 of the same elements in array


int find_index_of_m in( 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 2273


slickn_sly wrote:
int find_index_of_m in( 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_m in(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_m in( 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
11689
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 can you determine which one? Can you think of a way to do it using little extra memory? 2. How would you find the only missing element in that array? Can you think of a way to do it while iterating through the array only once. Is overflow a...
19
4455
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 array 4) Figure out if the original number is evenly divisible by any of the numbers in the array.
2
3711
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 memory blocks? Consider A and B size of N:
1
7696
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 is a better way to achieve it for(i=0;i<arrayA.length;i++){ for(j=0;j<arrayB.length;j++){ if(arrayA==arrayB)
4
7648
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
2607
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
3753
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
12406
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
1873
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
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9496
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10164
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9961
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8989
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4066
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.