Difference between revisions of "Equating Two Arrays"

From CompSciWiki
Jump to: navigation, search
(nvm)
(Changed grammar/spelling mistakes, Changed to codeblocks)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
 
{{1010PrAD|ProblemName=Equating Two Arrays
 
{{1010PrAD|ProblemName=Equating Two Arrays
  
|Problem= This problem envolves you seeing if two hard coded arrays are equal. You should use two int arrays to equate. Even though your arrays are hard coded into your program, you should not assume to know the length of the arrays. In other words use: <pre> array.length</pre> Remember that your arrays start indexing at 0 and that you should watch out for loops indexing arrays out of bounds. These are the most common pitfalls with a problem like this. If you can try to structure your code so that your loop is only comparing to the length of one of the arrays and not both of them. Print out whether your arrays are equal or not to the console using: <pre>System.out.println ();</pre>
+
|Problem= This problem involves checking if two hard coded arrays are equal. You should use two int arrays to equate. Even though your arrays are hard coded into your program, you should not assume to know the length of the arrays. In other words use: {{CodeBlock|Code=array.length}} Remember that your arrays start indexing at 0 and that you should watch out for loops indexing arrays out of bounds. These are the most common pitfalls with a problem like this. Try to structure your code so that your loop is only comparing to the length of one of the arrays and not both of them. Print out whether your arrays are equal or not to the console using: {{CodeBlock|Code=System.out.println();}}
  
 
|SideSectionTitle=Introducing Arrays
 
|SideSectionTitle=Introducing Arrays
 
|SideSection=[[Image:Wiki_array01.jpg|center]]<BR>
 
|SideSection=[[Image:Wiki_array01.jpg|center]]<BR>
  
|Solution=The Solution to this problem is to use a boolean value to say whether or not the arrays are equal. Assume that they are equal at first and initialize you boolean variable to ''true''. Once you have your two int arrays set up in the code the next part is to start finding a solution to the problem. The best way is to start by checking the two lengths of the arrays by doing this:
+
|Solution=The Solution to this problem is to use a boolean value to say whether or not the arrays are equal. Assume that they are equal at first and initialize the boolean variable to ''true''. Once you have your two int arrays set up in the code the next part is to start finding a solution to the problem. The best way is to start by checking the two lengths of the arrays by doing this:
<pre>
+
{{CodeBlock|Code=
 
if (array1.length == array2.length)
 
if (array1.length == array2.length)
 
{
 
{
 
}
 
}
 
else
 
else
   equals = false;
+
   equal = false;
</pre>
+
}}
This is done because if the two arrays are not equal in length then the are obviously not equal. By checking this simple property first you save yourself later on from having to check the length of two arrays when iterating through them as well as some computing time. The next step is to actually compare the values of the two arrays together by iterating through them with a loop. The best kind of loop for this task would be a for loop.  
+
This is done because if the two arrays are not equal in length then they are obviously not equal. By checking this simple property first, you save yourself from having to check the length of two arrays when iterating through them. The next step is to actually compare the values of the two arrays together by iterating through them with a loop. The best kind of loop for this task would be a for loop.  
<pre>
+
{{CodeBlock|Code=
 
for (int i = 0; i < array1.length; i++)
 
for (int i = 0; i < array1.length; i++)
 
{
 
{
 
   if (array1[i] != array2[i])
 
   if (array1[i] != array2[i])
     equals = false;
+
     equal = false;
 
}
 
}
</pre>
+
}}
By checking previously for the length of our arrays being equal, if the code gets to the loop we only need to use one of the arrays length properties. It doesn't matter which one you use. By inserting this for loop we have just written into our if statement from before we have a simple algorithm to test if two arrays are equal or not. You do a simple if statement at the bottom to print out your result based on the boolean variable ''equals''
+
Because we already checked that the lengths were the same, we can use either value for both array lengths. It doesn't matter which one you use. By inserting this for loop into our if statement from before, we have a simple algorithm to test if two arrays are equal or not. You do another if statement at the bottom to print out your result based on the boolean variable ''equal''.
 
|SolutionCode=
 
|SolutionCode=
<pre>
 
 
public class ArrayEquate
 
public class ArrayEquate
 
{
 
{
Line 33: Line 32:
 
     boolean equal = true;                  //boolean to say whether the arrays are equal or not.
 
     boolean equal = true;                  //boolean to say whether the arrays are equal or not.
 
      
 
      
     //Check to see if the arrays are equal in lenght, if they are not the arrays can not be equal.
+
     //Check to see if the arrays are equal in length, if they are not the arrays can not be equal.
 
     if (array1.length == array2.length)
 
     if (array1.length == array2.length)
 
     {
 
     {
Line 54: Line 53:
 
   }
 
   }
 
}
 
}
</pre>
 
 
}}
 
}}

Latest revision as of 15:52, 4 December 2011

Back to the Program-A-Day homepage

Problem

This problem involves checking if two hard coded arrays are equal. You should use two int arrays to equate. Even though your arrays are hard coded into your program, you should not assume to know the length of the arrays. In other words use:
 array.length 
Remember that your arrays start indexing at 0 and that you should watch out for loops indexing arrays out of bounds. These are the most common pitfalls with a problem like this. Try to structure your code so that your loop is only comparing to the length of one of the arrays and not both of them. Print out whether your arrays are equal or not to the console using:
 System.out.println(); 
 

Introducing Arrays

Wiki array01.jpg

Solution

The Solution to this problem is to use a boolean value to say whether or not the arrays are equal. Assume that they are equal at first and initialize the boolean variable to true. Once you have your two int arrays set up in the code the next part is to start finding a solution to the problem. The best way is to start by checking the two lengths of the arrays by doing this:

 if (array1.length == array2.length)
{
}
else
  equal = false; 

This is done because if the two arrays are not equal in length then they are obviously not equal. By checking this simple property first, you save yourself from having to check the length of two arrays when iterating through them. The next step is to actually compare the values of the two arrays together by iterating through them with a loop. The best kind of loop for this task would be a for loop.

 for (int i = 0; i < array1.length; i++)
{
  if (array1[i] != array2[i])
    equal = false;
} 

Because we already checked that the lengths were the same, we can use either value for both array lengths. It doesn't matter which one you use. By inserting this for loop into our if statement from before, we have a simple algorithm to test if two arrays are equal or not. You do another if statement at the bottom to print out your result based on the boolean variable equal.

Code

Solution Code

Back to the Program-A-Day homepage