6 Replies - 867 Views - Last Post: 02 December 2012 - 11:03 AM Rate Topic: -----

#1 greyn   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-October 12

Help with Linear Search of Array

Posted 02 December 2012 - 01:30 AM

Hi, I am new to the forum and very new to programming as well. I've got an assignment where I need to write a method to perform a linear search on an array of integers and return the position of a specific integer. The program seems to accept the user inputs just fine but always returns 0; I can't quite figure out why. Here is my code:

import java.util.Scanner;

public class ArrayOps
{
   public static int findValue(int values[], int valueToFind)
   {
     int pos = 0;
     int searchValue = valueToFind;
     boolean found = false;
     while (pos < values.length && !found)
     {
      if (values[pos] == searchValue)
      {
      	found = true;
      }
      else
      {
        pos++;
      }
     }
     if (found)
     {
       return pos;
     }
     else
     {
       return values.length;
     }
   }

   public static void main(String[] args)
   {
     int valueToFind = 0;
     int[] values = new int[10];
     int index = findValue(values, valueToFind);
     
     System.out.println("Enter a series of 10 integers: ");
     Scanner in = new Scanner(System.in);
     for (int i = 0; i < values.length; i++)
     {
    	values[i] = in.nextInt();
     }
     
     System.out.println("Which number to find? ");
     valueToFind = in.nextInt();

     System.out.print(index);
   }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Help with Linear Search of Array

#2 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Help with Linear Search of Array

Posted 02 December 2012 - 02:55 AM

Great first post!

You have to call the method you've created to find the user's number. Change the last line of your main() method to:

System.out.print( findValue( values, valueToFind ) );

I didn't test it, so there may be other problems, but that'll get you on the right track.
Was This Post Helpful? 1
  • +
  • -

#3 greyn   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-October 12

Re: Help with Linear Search of Array

Posted 02 December 2012 - 03:35 AM

Beautiful! That was the problem.

I had the line: int index = findValue(values, valueToFind); thinking that I could call the method while at the same time assigning its return to a variable. This apparently doesn't work?

Thank you Greg!
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Help with Linear Search of Array

Posted 02 December 2012 - 04:17 AM

Ahh, I didn't see that. Of course it works, and you proved it works by running your program and getting an answer. What answer did you get? Zero. Why is that?
Was This Post Helpful? 0
  • +
  • -

#5 greyn   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-October 12

Re: Help with Linear Search of Array

Posted 02 December 2012 - 05:18 AM

Ah, because I basically called the method too early; ie at a point when valueToFind = 0 and values[] was empty.

I rewrote it as such and it worked:

public static void main(String[] args)
   {
     int valueToFind = 0;
     int[] values = new int[10];
     int index = 0; 
     
     System.out.println("Enter a series of 10 integers: ");
     Scanner in = new Scanner(System.in);
     for (int i = 0; i < values.length; i++)
      { values[i] = in.nextInt(); }
     
     System.out.println("Which number to find? ");
     valueToFind = in.nextInt();

     index = findValue(values, valueToFind);

     System.out.print(index);
   }



It takes a line or two more code than the solution you gave me but now I see what I did wrong.
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Help with Linear Search of Array

Posted 02 December 2012 - 05:41 AM

Excellent analysis and absolutely correct. You've shown that the order/placement of a program's instructions can make a big difference.

Those coming to Java from some other languages commonly declare and initialize ALL variable at the top of their source code, because it was required by their former language. Java encourages programmers to not define variables until just before they're needed and/or used, and that is a common practice for many. Others, due to old habits, fall somewhere in between early and just-in-time variable declaration and initialization.

Your results show a good reason for sticking with Java's convention of not defining variables until they're needed.

This post has been edited by GregBrannon: 02 December 2012 - 05:41 AM

Was This Post Helpful? 0
  • +
  • -

#7 darek9576   User is offline

  • D.I.C Lover

Reputation: 203
  • View blog
  • Posts: 1,735
  • Joined: 13-March 10

Re: Help with Linear Search of Array

Posted 02 December 2012 - 11:03 AM

You can improve a bit your code by using the final keyword.
e.g.
value to be searched should probably not change. same applies to the array holding the values.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1