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[0] or a[15] etc.

thanking you in advance

Recommended Answers

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[7];
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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.