Difference between revisions of "Return Array"

From CompSciWiki
Jump to: navigation, search
Line 6: Line 6:
 
int[] n = {45,32,78,45,890};<BR>
 
int[] n = {45,32,78,45,890};<BR>
 
returnArray(n);<BR>
 
returnArray(n);<BR>
Output:1,2,3,4,5
+
Output:1/2/3/4/5/
  
  
Line 23: Line 23:
 
{
 
{
 
}
 
}
public static int[] returnArray( int[] n )
+
public static int[] returnArray( int[] array  )
 
{
 
{
 
}
 
}
Line 32: Line 32:
 
public static void main()
 
public static void main()
 
{
 
{
   int[] n = {45,32,78,45,890};
+
   int[] array = {45,32,78,45,890};
 +
  int[] tempArray;
  
   for( int i = 0; i < n.length; i++ ) // This will loop through every item in the array
+
  tempArray = returnArray(array);
 +
 
 +
   for( int i = 0; i < tempArray.length; i++ ) // This will loop through every item in the array
 
   {       
 
   {       
       System.out.print( n[i] + "," ); // Print the changed value
+
       System.out.print( tempArray[i] + "/" ); // Print the changed value
 
   }
 
   }
 
}
 
}
Line 44: Line 47:
 
}
 
}
 
</pre>
 
</pre>
 +
 +
<BR>
 +
Now we will fill in returnArray.  We know that it will be passed an array and we need to overwrite its contents with values increasing values starting at 1. We will then need to return the array:
 +
 +
<pre>
 +
public static int[] returnArray( int[] array )
 +
{
 +
  for( int i = 0; i < array .length; i++ ) // This will loop through every item in the array
 +
  {
 +
      array [i] = i+1; // Change the items to i+1 - we don't want start at i because we don't want 0
 +
  }
 +
  return n; // Return the changed array
 +
}
 +
</pre>
  
 
<BR>Now put it all together:<BR>
 
<BR>Now put it all together:<BR>
Line 50: Line 67:
 
public static void main()
 
public static void main()
 
{
 
{
   int[] n = {45,32,78,45,890};
+
   int[] array = {45,32,78,45,890};
 +
  int[] tempArray;
 +
 
 +
  tempArray = returnArray(array);
  
   for( int i = 0; i < n.length; i++ ) // This will loop through every item in the array
+
   for( int i = 0; i < tempArray.length; i++ ) // This will loop through every item in the array
 
   {       
 
   {       
       System.out.print( n[i] + "," ); // Print the changed value
+
       System.out.print( tempArray[i] + "/" ); // Print the changed value
 
   }
 
   }
 
}
 
}
Line 60: Line 80:
 
public static int[] returnArray( int[] n )
 
public static int[] returnArray( int[] n )
 
{
 
{
   for( int i = 0; i < n.length; i++ ) // This will loop through every item in the array
+
   for( int i = 0; i < array .length; i++ ) // This will loop through every item in the array
 
   {
 
   {
       n[i] = i+1; // Change the items to i+1 - we don't want start at i because we don't want 0
+
       array [i] = i+1; // Change the items to i+1 - we don't want start at i because we don't want 0
 
   }
 
   }
 
   return n; // Return the changed array
 
   return n; // Return the changed array

Revision as of 11:51, 1 April 2010

Back to the Program-A-Day homepage

Problem

Write a method which will take an array of ints as a parameter. The method should change the contents of the array to 1-5. Call this method from main and then print the returned array in main.

Example: int[] n = {45,32,78,45,890};
returnArray(n);
Output:1/2/3/4/5/

 

SideSectionTitle

float
Taken from http://www.flickr.com/photos/daniello/565304023/

An image or By Students section

Solution

The solution... To solve this problem we are going to create a skeleton structure. We know that we will have two methods: main & returnArray. We know that returnArray will accept an integer array and return that array:

public static void main()
{
}
public static int[] returnArray( int[] array  )
{
}


Now that we have the skeleton we can start by creating an array in main, we will pass that array to our returnArray method and return it into a temporary array:

public static void main()
{
   int[] array = {45,32,78,45,890};
   int[] tempArray;

   tempArray = returnArray(array);

   for( int i = 0; i < tempArray.length; i++ ) // This will loop through every item in the array
   {       
       System.out.print( tempArray[i] + "/" ); // Print the changed value
   }
}

public static int[] returnArray( int[] n )
{
}


Now we will fill in returnArray. We know that it will be passed an array and we need to overwrite its contents with values increasing values starting at 1. We will then need to return the array:

public static int[] returnArray( int[] array )
{
   for( int i = 0; i < array .length; i++ ) // This will loop through every item in the array
   {
       array [i] = i+1; // Change the items to i+1 - we don't want start at i because we don't want 0
   }
   return n; // Return the changed array
}


Now put it all together:


public static void main()
{
   int[] array = {45,32,78,45,890};
   int[] tempArray;

   tempArray = returnArray(array);

   for( int i = 0; i < tempArray.length; i++ ) // This will loop through every item in the array
   {       
       System.out.print( tempArray[i] + "/" ); // Print the changed value
   }
}

public static int[] returnArray( int[] n )
{
   for( int i = 0; i < array .length; i++ ) // This will loop through every item in the array
   {
       array [i] = i+1; // Change the items to i+1 - we don't want start at i because we don't want 0
   }
   return n; // Return the changed array
}

Code

SolutionCode goes here. Please DO NOT put your code in <pre> tags!

Back to the Program-A-Day homepage