+ 1
Can anyone will be able to provide me a program of linear search of arrays in Java ? Please 🙏🙏
What is the mistake here? Why is the output not coming? Can anyone please help me please 🙏🙏 import java.io.*; public class LinSearch { public void Search(int num) { int x[] = {2,5,8,9,4,6}; int j; int pos = 0; int foundwk for( j = 0; j < 5; j++) { if( num == x[j] ) { pos = j; found = 1; break; } } if( found == 1) { System.out.println( num+ " is found at :- " +pos+ "index"); } else { System.out.println ( num+ " is not found "); } } } https://code.sololearn.com/cLoC5CCh7bOb/?ref=app
4 odpowiedzi
0
Sorry but your code is working neither.
+ 3
Not sure what you're trying to do exactly. But you're missing the main... maybe for a reason?
Here's some fixing I've done for you.
https://code.sololearn.com/c7f4k7121UL9/?ref=app
+ 1
import java.io.*;
import java.util.Scanner;
public class LinSearch
{
public static void search(int num)
{
int x[] = {2,5,8,9,4,6};
int j, pos = 0, found = 0;
for( j = 0; j < 5; j++)
{
if( num == x[j] )
{
pos = j; found = 1;
break;
}
}
if( found == 1)
{
System.out.println( num+ " is found at :- " +pos+ "index");
}
else
{
System.out.println ( num+ " is not found ");
}
}
public static void main(String arg[]){
Scanner sc = new Scanner(System.in);
search(sc.nextInt());
}
}
Try this, although your original code is in a messy, looping method is weird too.
0
You have no main and no search function call…