# Thread: finding the element number in an array

1. ## 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. 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. 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.

4. 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. 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.

6. 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. 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.

8. 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.