How to get the index value in an array??

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: May 2009
Posts: 5
Reputation: gcah is an unknown quantity at this point 
Solved Threads: 0
gcah gcah is offline Offline
Newbie Poster

How to get the index value in an array??

 
0
  #1
Jul 29th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 5
Reputation: TheCo3der is an unknown quantity at this point 
Solved Threads: 2
TheCo3der TheCo3der is offline Offline
Newbie Poster

Re: How to get the index value in an array??

 
0
  #2
Jul 29th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 44
Reputation: zalezog is an unknown quantity at this point 
Solved Threads: 11
zalezog zalezog is offline Offline
Light Poster

Re: How to get the index value in an array??

 
0
  #3
Jul 29th, 2009
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'?
Last edited by zalezog; Jul 29th, 2009 at 12:53 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 5
Reputation: gcah is an unknown quantity at this point 
Solved Threads: 0
gcah gcah is offline Offline
Newbie Poster

Re: How to get the index value in an array??

 
0
  #4
Jul 29th, 2009
Perfect, thank you!
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 5
Reputation: TheCo3der is an unknown quantity at this point 
Solved Threads: 2
TheCo3der TheCo3der is offline Offline
Newbie Poster

Re: How to get the index value in an array??

 
0
  #5
Jul 29th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 5
Reputation: gcah is an unknown quantity at this point 
Solved Threads: 0
gcah gcah is offline Offline
Newbie Poster

Re: How to get the index value in an array??

 
0
  #6
Jul 29th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC