Find out the problem of my code.

Reply

Join Date: Jul 2004
Posts: 16
Reputation: shantuli is an unknown quantity at this point 
Solved Threads: 0
shantuli shantuli is offline Offline
Newbie Poster

Find out the problem of my code.

 
0
  #1
Nov 26th, 2004
hi,
can anyone pls find out the problem of my code.


Code:

public class binarysearch {
public static void main ( String[] args ) {
int[] array = {2,2,10,20,55,100};
int target = 2;
int left = 0;
int right = array.length - 1;
int mid = -1;

while ( left <= right ) {
mid = ( left + right ) / 2;

if ( target == array[mid] )
break;
else if ( target < array[mid] )
right = mid - 1;
else
left = mid - 1;
}

if ( mid != -1 )
System.out.println ( target + " found at index " + mid );
else
System.out.println ( target + " not found" );
}
}



Problem :
here it's only showing me the 1st position, but i want it to show me both the position. can anyone pls fix-up the error of my code.

thank you

shantuli
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 609
Reputation: freesoft_2000 is an unknown quantity at this point 
Solved Threads: 7
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Re: Find out the problem of my code.

 
0
  #2
Dec 4th, 2004
hi everyone,

What is this program supposed to do ?
Please explain in detail so we can help you out

Richar West
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 111
Reputation: ChaseVoid is an unknown quantity at this point 
Solved Threads: 12
ChaseVoid's Avatar
ChaseVoid ChaseVoid is offline Offline
Junior Poster

Re: Find out the problem of my code.

 
0
  #3
Oct 21st, 2007
Are you trying to find the INDEX of any one of the value in the integer array? Since you have declared only a single dimensional array, there is only one Index for it.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 111
Reputation: ChaseVoid is an unknown quantity at this point 
Solved Threads: 12
ChaseVoid's Avatar
ChaseVoid ChaseVoid is offline Offline
Junior Poster

Re: Find out the problem of my code.

 
0
  #4
Oct 21st, 2007
ooh! I get it, you want to find all the indexes where a number "target" has appeared, right? Well it's showing 0, you also need 1, cause '2' also appears in that index. However, according to your program, there is no way to put that position. I' have no idea about Java, but I have done it in C++ (just now LOL), so I'll try to convert it, and tell you. You can use continue statement too for more clarity.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,185
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 482
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Find out the problem of my code.

 
0
  #5
Oct 21st, 2007
I' have no idea about Java, but I have done it in C++
for once this is java forum, secondly this post is nearly 3 years old so what you trying to achive?
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 111
Reputation: ChaseVoid is an unknown quantity at this point 
Solved Threads: 12
ChaseVoid's Avatar
ChaseVoid ChaseVoid is offline Offline
Junior Poster

Re: Find out the problem of my code.

 
0
  #6
Oct 21st, 2007
>this post is nearly 3 years old
Argh! no ways.. I thought I was a recent one, I saw it in the display of similar threads -_-' Sorry. I didn't wanted to resurrect an old thread. I'm so sorry.

I'm trying to learn java, so I was going through this forum, and saw the thread. Didn't realize it was THAT old. I feel like a crap. and couldn't even get the effin result right T_T
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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