Difference between revisions of "Return Array"

From CompSciWiki
Jump to: navigation, search
Line 22: Line 22:
 
   for( int i = 0; i < n.length; i++ )
 
   for( int i = 0; i < n.length; i++ )
 
   {
 
   {
       n[i] = i;
+
       n[i] = i+1;
 
       System.out.print( n[i] + "," );
 
       System.out.print( n[i] + "," );
 
   }
 
   }

Revision as of 12:43, 30 March 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. Print the array to show the contents have been changed and return it.
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...

public static int[] returnArray( int[] n )
{
   for( int i = 0; i < n.length; i++ )
   {
       n[i] = i+1;
       System.out.print( n[i] + "," );
   }
   return n;
}

Code

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

Back to the Program-A-Day homepage