Difference between revisions of "Passing Arrays"

From CompSciWiki
Jump to: navigation, search
Line 1: Line 1:
 
{{1010PrAD|ProblemName=Passing Arrays
 
{{1010PrAD|ProblemName=Passing Arrays
|Problem= Problem...
+
|Problem= Write a program with 3 separate methods. One method will create an integer array, it will take an integer parameter as the size of the array to create and return the newly created array. Second method will take an integer array and add the contents of it and return the total. Third method will take an array set all the value in the array to 0 and will not return the array back.
 
+
  
 
|SideSectionTitle = Introducing Arrays
 
|SideSectionTitle = Introducing Arrays
Line 11: Line 10:
 
|SolutionCode=
 
|SolutionCode=
 
<pre>
 
<pre>
some code
+
public class PassingArrays{
 +
  public static void main(){
 +
    int[] intArray;
 +
 +
    intArray = createArray(10);
 +
    System.out.println(addArray(intArray));
 +
    setZeroes(intArray);
 +
    System.out.println(addArray(intArray));
 +
 
 +
    System.out.println("Program completed successfully.");
 +
  }
 +
 +
  public static int[] createArray(int n){
 +
    int[] array = new int[n];
 +
   
 +
    for(int i=0; i<n; i++)
 +
      array[i] = i;
 +
  } 
 +
 
 +
  public static int addArray(int[] array){
 +
    int total = 0;
 +
 
 +
    for(int i=0; i<array.length; i++)
 +
      total += array[i];
 +
 
 +
    return total;
 +
  }
 +
 
 +
  public static void setZeroes(int[] array){
 +
    for(int i=0; i<array.length; i++)
 +
      array[i] = 0;
 +
  }
 +
}
 
</pre>
 
</pre>
  
 
}}
 
}}

Revision as of 10:42, 8 April 2010

Back to the Program-A-Day homepage

Problem

Write a program with 3 separate methods. One method will create an integer array, it will take an integer parameter as the size of the array to create and return the newly created array. Second method will take an integer array and add the contents of it and return the total. Third method will take an array set all the value in the array to 0 and will not return the array back.

 

Introducing Arrays

Wiki array01.jpg

Solution

Code

Solution Code

Back to the Program-A-Day homepage