-1

I've spent like an hour trying to figure this one out and I couldn't. This is an exercise for a Java elective course I am taking and I could use some help.

Write a method linearSearch() that takes an array of integers, and an integer value. It should then return the index of the value inside the array (by using a for loop to traverse the elements one by one in order). If the value is not found, -1 should be returned. If more than one value is found, the first occurrence should be returned. Write a program to test your method.

So this is what I tried doing.

public class Exercise6 {
    public static void main(String [] args) {
        int[] a = {3, 6, 70, 3, 7, 9};
        linearSearch(a, 3);
        
    }

    public static void linearSearch(int[] a, int n){  
        int index;      

        for (int i = 0; i < a.length; i++){
            if (a[i] == n){
                index = i;
                break;
            }
            else {
                index = -1;
            }
        }
        System.out.print(index);
    }
}

But this is obviously wrong. Could you please point me in the right direction? I don't necessarily want you to give me the answer, just give me an idea about the steps.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    you don't need that variable index. Once you found your match, just return the index. "This is obviously wrong." Why is it wrong? What's wrong with it? – Stultuske Mar 23 '22 at 14:15
  • 4
    Why do you think your code is wrong? Because it prints `0` (which is the correct solution)? – maloomeister Mar 23 '22 at 14:17
  • 4
    One thing that is wrong, is that your method doesn't return anything. It should return the index, not just print something. – Stultuske Mar 23 '22 at 14:17
  • Your method does not "return" anything it currently just prints. If you need a method that returns then reading [Differences between System.out.println() and return in Java](https://stackoverflow.com/questions/25456472/differences-between-system-out-println-and-return-in-java) might be a good idea. – OH GOD SPIDERS Mar 23 '22 at 14:18
  • 1
    Please explain why you think this is "obviously wrong" – jhamon Mar 23 '22 at 14:18

3 Answers3

0

The problem is that you didn't initialize the variable (index):

int index = -1;

but you should think at another way as follows:

1 - read what you want to do

2- You need to make a function that return the index of a specific value of array

3- Define the return type of the function ==> you need to return the index then the function return type is int

4- Define the parameters ==> you have 2 parameters : array and the value

5- You should return the index of value or -1 if it doesn't exist then you will initialize the index = -1

6- Code will be as follows:

public class Exercise6
{
    public static int linearSearch(int[] a, int n){  
        int index = -1;      

        for (int i = 0; i < a.length; i++){
            if (a[i] == n){
                index = i;
                break;
            }
        }
        return index;
    }

    public static void main(String[] args) {
        int[] a = {3, 6, 70, 3, 7, 9};
        System.out.println(linearSearch(a, 7));
    }
}

Think before Coding

All the best

0

There are several problems.

  • you want to return a value so you need to show that in the method by having a return type of int and not void.

  • you don't need an index variable, you already have one in your for loop (i) so use that.

  • As you iterate over the loop, as soon as you find the value, just do a return i; to return the index. You do not need any break statements in your method.

  • you don't need to keep assigning -1 to index so get rid of the else clause (remember you don't even need index).

  • if you finish the loop the value must not be there so just return -1;.

  • and lastly since you are returning a value, you need to assign it when you call the method. So do int ret = linearSearch(a, 3);. Then print the value.

WJS
  • 36,363
  • 4
  • 24
  • 39
-1

This will through compilation error as int index; is not initialized. Once initialized, else part would become redundant. Rest would work and deliver you the expected output.