Difference between revisions of "Find Element x Using Linear Search"

From CompSciWiki
Jump to: navigation, search
 
m (Added comment for elementExists method)
 
(15 intermediate revisions by 7 users not shown)
Line 2: Line 2:
  
 
|Problem=
 
|Problem=
Create a method that accepts an array of integers and a key integer value x to determine whether or not the key value exists in the given array. The method should return true if the key value was found in the array, otherwise the method should return false.
+
Create a method that accepts an array of integers and a key value (integer) to determine whether or not the key value exists in the given array.
  
|SideSection=
+
The method should return true if the key value was found in the array, otherwise the method should return false.
[[Image:OperatingSystemExample.jpg|float|267px]]
+
<BR>
+
Taken from http://www.flickr.com/photos/daniello/565304023/
+
  
An image or By Students section
+
|SideSectionTitle=Array Algorithms
 +
|SideSection=
 +
[[Image:Wiki_array03.jpg|center]]<BR>
  
 
|Solution=
 
|Solution=
You will want to keep a boolean variable to keep track of whether or not you have found the key value x. At the beginning of your method, initialize this boolean variable to false since you have not yet found the key value. Use a for loop to go through each element in the given array, along with an if statement to compare each element with the given key value. In order to compare each element with the key value, use the '==' operator. If the current element is equal to the key value, that means you have found the element in the array so you can set your boolean variable to true. Return this boolean variable.
+
Start by creating the method, this method should accept an array of integers and return an integer value.
 +
{{CodeBlock|Code=
 +
public static boolean elementExists(int[] array, int x) {
  
<pre>
+
}
 +
}}
 +
 
 +
You'll want to keep a boolean variable to keep track of whether or not you have found the key value x. At the beginning of your method, initialize this boolean variable to false since you have not yet found the key value.
 +
{{CodeBlock|Code=
 +
boolean exists = false;
 +
 
 +
return exists;
 +
}}
 +
 
 +
Use a for loop to go through each element in the array to check if it is the value you're looking for.
 +
{{CodeBlock|Code=
 +
for(i = 0; i < array.length; i++) {
 +
 
 +
}
 +
}}
 +
 
 +
Use an if statement to compare each element with the given key value x. In order to compare each element with x, use the '==' operator. If the current element is equal to the key value, that means you have found the element in the array, so you can set your boolean variable exists to true.
 +
{{CodeBlock|Code=
 +
if(array[i] == x) {
 +
    exists = true;
 +
}
 +
}}
 +
 
 +
After the for loop, return your boolean exists variable.
 +
{{CodeBlock|Code=
 +
return exists;
 +
}}
 +
 
 +
Putting everything together, here's what the entire method should look like:
 +
{{CodeBlock|Code=
 
public static boolean elementExists(int[] array, int x) {
 
public static boolean elementExists(int[] array, int x) {
 
     int i; // loop iterator
 
     int i; // loop iterator
Line 27: Line 58:
 
     return exists;
 
     return exists;
 
}
 
}
</pre>
+
}}
  
 +
|SolutionCode=
 +
/*
 +
This program asks the user to input a value between 0-15,
 +
creates an array of ten random numbers between 0 and 15
 +
and checks to see if the user's given value is in the array
 +
*/
  
 +
import javax.swing.JOptionPane;
 +
 +
public class LinearSearchForX {
 +
    public static void main(String args[]) {
 +
        int i, x;
 +
int[] numbers = new int[10];
 +
boolean xFound = false;
 +
 +
// Ask user to input a value to search for
 +
x = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter a value between (0-15) to search for:"));
 +
 +
// Create an array of random numbers
 +
for(i = 0; i < numbers.length; i++) {
 +
    numbers[i] = (int)(Math.random() * 15); // generates a random number from 0-15
 +
    System.out.println("numbers["+i+"] = "+numbers[i]);
 +
}
 +
 +
// Call your method to check if the element exists
 +
xFound = elementExists(numbers, x);
 +
 +
// Display the results of your search.
 +
if(xFound) {
 +
    System.out.println( x + " was found in the array" );
 +
} else {
 +
    System.out.println( x + " was not found in the array" );
 +
}
 +
    }
 +
   
 +
    // This method returns a boolean indicating whether the element was found
 +
    // in the array or not
 +
    public static boolean elementExists(int[] array, int x) {
 +
        int i; // loop iterator
 +
        boolean exists = false;
 +
 +
        for(i = 0; i < array.length; i++) {
 +
            if(array[i] == x) {
 +
                exists = true;
 +
            }
 +
        }
 +
 +
        return exists;
 +
    }
 +
 +
}
 
}}
 
}}

Latest revision as of 13:17, 7 December 2011

Back to the Program-A-Day homepage

Problem

Create a method that accepts an array of integers and a key value (integer) to determine whether or not the key value exists in the given array.

The method should return true if the key value was found in the array, otherwise the method should return false.

 

Array Algorithms

Wiki array03.jpg

Solution

Start by creating the method, this method should accept an array of integers and return an integer value.

 public static boolean elementExists(int[] array, int x) {

} 

You'll want to keep a boolean variable to keep track of whether or not you have found the key value x. At the beginning of your method, initialize this boolean variable to false since you have not yet found the key value.

 boolean exists = false;

return exists; 

Use a for loop to go through each element in the array to check if it is the value you're looking for.

 for(i = 0; i < array.length; i++) {

} 

Use an if statement to compare each element with the given key value x. In order to compare each element with x, use the '==' operator. If the current element is equal to the key value, that means you have found the element in the array, so you can set your boolean variable exists to true.

 if(array[i] == x) {
    exists = true;
} 

After the for loop, return your boolean exists variable.

 return exists; 

Putting everything together, here's what the entire method should look like:

 public static boolean elementExists(int[] array, int x) {
    int i; // loop iterator
    boolean exists = false;

    for(i = 0; i < array.length; i++) {
        if(array[i] == x) {
            exists = true;
        }
    }

    return exists;
} 

Code

Solution Code

Back to the Program-A-Day homepage