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

From CompSciWiki
Jump to: navigation, search
m (reworded the first solution step)
m (Added comment for elementExists method)
 
(4 intermediate revisions by 4 users not shown)
Line 2: Line 2:
  
 
|Problem=
 
|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.
+
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.
  
 
|SideSectionTitle=Array Algorithms
 
|SideSectionTitle=Array Algorithms
Line 9: Line 11:
  
 
|Solution=
 
|Solution=
Start by creating a method this method should accept an array of integers and return an integer value.
+
Start by creating the method, this method should accept an array of integers and return an integer value.
<pre>
+
{{CodeBlock|Code=
 
public static boolean elementExists(int[] array, int x) {
 
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.
 
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.
<pre>
+
{{CodeBlock|Code=
 
boolean exists = false;
 
boolean exists = false;
  
 
return exists;
 
return exists;
</pre>
+
}}
  
 
Use a for loop to go through each element in the array to check if it is the value you're looking for.
 
Use a for loop to go through each element in the array to check if it is the value you're looking for.
<pre>
+
{{CodeBlock|Code=
 
for(i = 0; i < array.length; i++) {
 
for(i = 0; i < array.length; i++) {
  
 
}
 
}
</pre>
+
}}
  
 
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.  
 
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.  
<pre>
+
{{CodeBlock|Code=
 
if(array[i] == x) {
 
if(array[i] == x) {
 
     exists = true;
 
     exists = true;
 
}
 
}
</pre>
+
}}
  
 
After the for loop, return your boolean exists variable.
 
After the for loop, return your boolean exists variable.
<pre>
+
{{CodeBlock|Code=
 
return exists;
 
return exists;
</pre>
+
}}
  
 
Putting everything together, here's what the entire method should look like:
 
Putting everything together, here's what the entire method should look like:
<pre>
+
{{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 56: Line 58:
 
     return exists;
 
     return exists;
 
}
 
}
</pre>
+
}}
  
 
|SolutionCode=
 
|SolutionCode=
<pre>
 
 
/*
 
/*
 
This program asks the user to input a value between 0-15,
 
This program asks the user to input a value between 0-15,
Line 93: Line 94:
 
}
 
}
 
     }
 
     }
 
+
   
 +
    // This method returns a boolean indicating whether the element was found
 +
    // in the array or not
 
     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 108: Line 111:
  
 
}
 
}
</pre>
 
 
 
}}
 
}}

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