Thread: finding the element number in an array

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    78

    finding the element number in an array

    I am trying to find the element number in an array. I already found the minimum number, but I need to find the element number of that minimum number. Here's the code:
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    float minimum(float[], int);
    
    int main()
    {
    	int a;
    	float ticketPrices[6];
    
    	for (a=0;a<=5;++a)		/*enter 6 ticket prices*/
    	{
    		printf("Please enter the ticket prices:   ");
    		scanf("%f", &ticketPrices[a]);
    	}
    	for (a=0;a<=5;++a)
    		printf("\nTickets prices that you entered are:   %.2f", ticketPrices[a]);
    
    	printf("\nThe minimum value is:  %.2f", minimum(ticketPrices, 6));
    
    	printf("\nThis is element number:  %d", );
    	
    	getch();
        
    	return 0;
    }
    
    float minimum(float number[], int numEls)
    {
    	int a;
    	float min=number[0];
    
    	for (a=1;a<numEls;++a)
    		if(min>number[a])
    			min=number[a];
    
    	return(min);
    }
    The only help I need is where the printf statement is bold. Not sure how to find the subscript number (element number). Do I need another variable in the For loop? Thanks......Tommy

  2. #2
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    least complex but more coding way?
    create another function similar to your minimum function, but return the index of the minimum value instead.

    Any other method would atleast require pointers if you plan on using the same function to obtain both min value and index.

    or you could just do the printfs in the function. Very non reusable code then though...

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by tommy69
    I am trying to find the element number in an array. I already found the minimum number, but I need to find the element number of that minimum number. Here's the code:
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    float minimum(float[], int);
    
    int main()
    {
    	int a;
    	float ticketPrices[6];
    
    	for (a=0;a<=5;++a)		/*enter 6 ticket prices*/
    	{
    		printf("Please enter the ticket prices:   ");
    		scanf("%f", &ticketPrices[a]);
    	}
    	for (a=0;a<=5;++a)
    		printf("\nTickets prices that you entered are:   %.2f", ticketPrices[a]);
    
    	printf("\nThe minimum value is:  %.2f", minimum(ticketPrices, 6));
    
    	printf("\nThis is element number:  %d", );
    	
    	getch();
        
    	return 0;
    }
    
    float minimum(float number[], int numEls)
    {
    	int a;
    	float min=number[0];
    
    	for (a=1;a<numEls;++a)
    		if(min>number[a])
    			min=number[a];
    ////                     isn't THIS^ value the element number?
    	return(min);
    }
    The only help I need is where the printf statement is bold. Not sure how to find the subscript number (element number). Do I need another variable in the For loop? Thanks......Tommy
    So how does one pass back the index and/or the min value? If you have the index value, do you actually need the number itself? A change in the minimum function or a second function can be used.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    I thought [a] was the element number but it's not as I already tried that. I was thinking that I need another variable in the for loop, but isn't there an easier way?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by tommy69
    I thought [a] was the element number but it's not as I already tried that. I was thinking that I need another variable in the for loop, but isn't there an easier way?
    Yes it is. Consider the following:
    Code:
    #include <stdio.h>
    int main( void )
    {
        int array[5] = { 34, 56, 1, 88, 723 };
        int index = 0;
    
        for( index = 0; index < 5; index++ )
            printf("array[%d] holds %d. index is %d\n", index, array[index], index );
    
        return 0;
    }
    Run it.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    I tried moving the printf statement within the bottom for loop and I used a and it still doesn't give me the correct element number. I will work on it when I get to work in an hour. Thanks for the help.

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by tommy69
    I tried moving the printf statement within the bottom for loop and I used a and it still doesn't give me the correct element number. I will work on it when I get to work in an hour. Thanks for the help.
    OK, I guess we gotta get specific here. Instead of searching specifically for the max number, search for the index of the max number and return the index instead.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by WaltP
    OK, I guess we gotta get specific here.
    I guess it just goes to show that the old adage is correct...

    You can lead would-be programmers to the answer, but you can't make'm think.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Finding largest element in array
    By Chaplin27 in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2005, 09:18 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. assigning a rand number to each element in an array
    By agerealm in forum C++ Programming
    Replies: 3
    Last Post: 12-15-2002, 01:12 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM