Does any one know how to get the index value in a 1 dim array of a returned character or integer?

For example the value is 15 but I need to know where 15 is on the array, position a or a etc.

thanking you in advance

## Recommended Answers

I think a Simple Linear Search Algo will solve ur problem

i.e:

int index=0;
while(arr[index] != 15) //15 as an example the value we r looking //for
{
index++;
}
printf("The required index in the array for 15 is : %d \n",index);

hope that helps

int index=0;
while(arr[index] != 15)
//15 as an example the value we r looking for
{
index++;
}

What if '15' does not exist in the array, what happens then ?
How far will index go ?

or, if there are multiple copies of '15'?

## All 5 Replies

I think a Simple Linear Search Algo will solve ur problem

i.e:

int index=0;
while(arr[index] != 15) //15 as an example the value we r looking //for
{
index++;
}
printf("The required index in the array for 15 is : %d \n",index);

hope that helps

int index=0;
while(arr[index] != 15)
//15 as an example the value we r looking for
{
index++;
}

What if '15' does not exist in the array, what happens then ?
How far will index go ?

or, if there are multiple copies of '15'?

Perfect, thank you!

well in case 15 doesnot exist the value of index may go outside the bounadries of the array leading to a runtime error so another condition must be set
i.e:
int arr;
while(arr[index]!= 15 && index <7)

and in case there are multiple Copies of 15 only the first copy index will be produced

I have the value of an int from the array already, just by using a for loop and pulling out the largest value, i just need to print out it's position, so it works for me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.