Help with Linear Search of Array

Page 1 of 1

6 Replies - 867 Views - Last Post: 02 December 2012 - 11:03 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=302402&amp;s=d6f6a826c543bccae004681eeac9e91d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 greyn

Reputation: 0
• 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

• D.I.C Lover

Reputation: 2250
• 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.

#3 greyn

Reputation: 0
• 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!

#4 GregBrannon

• D.I.C Lover

Reputation: 2250
• 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?

#5 greyn

Reputation: 0
• 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.

#6 GregBrannon

• D.I.C Lover

Reputation: 2250
• 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

#7 darek9576

• D.I.C Lover

Reputation: 203
• 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.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }