SOS
Hey friends,
I am stuck in this the program below.
I am supposed to get the result of the item whether it's in array of not. Kindly guide me through this...where I have done the mistake.
I am new in Java.
__________________________________________________ _____________________
class LinearSearching
{
public static void main(String[] args)
{
int data[] = {11,23,54,34,44,76};
int n = data.length;
int item = 50;
int loc = 0;
data[n+1] = item;
do
{
loc = loc + 1;
}
while(data[loc]!=item);
if(loc == n+1)
{
loc = 0;
System.out.println("Unsuccessful");
}
else
System.out.println("Successful");
}
}
__________________________________________________ ______________________
6 replies to this topic
#1
Posted 26 March 2011 - 06:14 AM
|
|
|
#2
Posted 26 March 2011 - 06:22 AM
I have never seen such a weird code for a linear search :D
or even better:
or:
public class LinearSearching
{
public static void main(String[] args)
{
int data[] = {11,23,54,34,44,76};
int item = 50;
int i;
for(i=0;i<data.length;i++){
if(data[i]==item)
break;
}
if(i<data.length){
System.out.println("The element " + item + " is in the array at position " + i);
} else{
System.out.println("The element " + item + " is not in the array");
}
}
}
or even better:
public static void main(String[] args)
{
int data[] = {11,23,54,34,44,76};
int item = 50;
if(isInArray(data, item)){
System.out.println("The element " + item + " is in the array");
} else{
System.out.println("The element " + item + " is not in the array");
}
}
private static boolean isInArray(int [] array, int element){
for(int i=0;i<array.length;i++){
if(array[i]==element)
return true;
}
return false;
}
or:
public static void main(String[] args)
{
int data[] = {11,23,54,34,44,76};
int item = 23;
int x=isInArray(data, item);
if(x!=-1){
System.out.println("The element " + item + " is in the array at position " + x);
} else{
System.out.println("The element " + item + " is not in the array");
}
}
private static int isInArray(int [] array, int element){
for(int i=0;i<array.length;i++){
if(array[i]==element)
return i;
}
return -1;
}
#3
Posted 26 March 2011 - 06:42 AM
OH...thanks....and also thanks for criticizing... :crying:
#4
Posted 26 March 2011 - 07:44 AM
you're welcome =)
#5
Posted 27 March 2011 - 12:25 PM
OwsumEmam said:
SOS
int data[] = {11,23,54,34,44,76};
int n = data.length;
int item = 50;
int loc = 0;
data[n+1] = item;
int data[] = {11,23,54,34,44,76};
int n = data.length;
int item = 50;
int loc = 0;
data[n+1] = item;
First, if you initialise data[] when you create it, it will set its length automatically. So, when you do this: "data[n+1] = item", you will get an ArrayIndexOutOfBoundsException because n being the length of your array, adding a new item to it will cause it to throw an Exception.
Instead, you could've done it like this:
"int data2[] = new int[data.length+1];
Arrays.copy(data,data2);
data2[data2.length-1] = item;
"
OwsumEmam said:
SOS
do
{
loc = loc + 1;
}
while(data[loc]!=item);
do
{
loc = loc + 1;
}
while(data[loc]!=item);
Doing this will first increase loc (So, you will start searching from index value 1, rather than 0, which is the first element from the array) and then search for an element.
OwsumEmam said:
SOS
if(loc == n+1)
{
loc = 0;
System.out.println("Unsuccessful");
}
else
System.out.println("Successful");
}
}
if(loc == n+1)
{
loc = 0;
System.out.println("Unsuccessful");
}
else
System.out.println("Successful");
}
}
So, after putting your element to array index n+1 (which actually doesn't exist), if loc has the value n+1 at the end of your loop, you decide that actually your search is unsuccessful.
I hope the above will help you see your mistakes from your code and, yes, you should take a good look at all the algorithms presented above.
#6
Posted 28 March 2011 - 10:09 PM
Thanks for helping me bro but Exception still exists. Well eafkour has helped me. Thanks to you too.
#7
Posted 29 March 2011 - 03:20 PM
data[n+1] = item;Here is your culprit.
n = data.length;
Valid positions in the array are from zero to n-1.
So data[n] = error
data[n+1] = error
data[n+2] = error
data[n + ...] = error.
______________________________
Your next problem is in your do...while loop. loc continually increases from zero to n where it then errors(if your item is NOT found).
To fix this you need to add another test.
do { ... }
while(loc != n && data[loc] != item );
______________________________Another problem is you initialize loc to zero. Then inside of your while loop you immediately change it to loc = loc + 1, or loc = 1.
You never check to see if 'item' is at position 0 in the array.
______________________________
I've never seen anyone use a do-while loop for a linear search but it can be done with a few more adjustments.
I would rather see it using a regular while or for loop.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









