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

gcah
0
Newbie Poster

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

Jump to PostI 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

Jump to Postint 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'?

TheCo3der
0
Newbie Poster

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

zalezog
43
Light Poster

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'?

gcah
0
Newbie Poster

Perfect, thank you!

TheCo3der
0
Newbie Poster

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

gcah
0
Newbie Poster

Be a part of the DaniWeb community

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

Broken Link

**You're trying to visit a URL that doesn't currently exist on the web.**
Most likely, a member posted a link a long time ago to a web page that has since been removed.
It's also possible that there was a typo when posting the URL.
We redirect you to this notice instead of stripping out the link to preserve the integrity of the post.