Passing Arrays

From CompSciWiki
Jump to: navigation, search

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.

 

...By Students

"I remember finding out that I didn't have pass arrays back from my methods when I needed to change them, like in sorts. This turned out to be very useful at first, but then it became a hassle. Especially when I needed to do some computation on the array that could potentially alter it. A new method then needed to be created that created a copy of my array so then I could pass the copy without altering the original. This problem will compound further once more complex datatypes are involved. But this did give a nice starting block to learning about how datatypes behave."

Solution

First step is to create skeleton structure of the program. The methods will looks something like this:

 public static int[] createArray(int n){
}
public static int addArray(int[] array){
}
public static void setZeroes(int[] array){
} 

In the main method there will be an array variable that we will use to store the array created by createArray. This array variable will have to be of the same type as the array returned by createArray. The method createArray will be very simple, it will take an integer value to specify the size of the array to be created and then store values 1 to the size of the array sequentially in the array. This is done easiest with a for loop.

 public static int[] createArray(int n){
    int[] array = new int[n];
    
    for(int i=0; i<n; i++)
      array[i] = i;

    return array;
} 

The method addArray will simply sum all the values in the array and return the total. This is also easiest done with a for loop and having a separate variable to hold the sum, which will be returned.

 public static int addArray(int[] array){
    int total = 0;

    for(int i=0; i<array.length; i++)
      total += array[i];

    return total;
} 

The method setZeroes will also use a for loop, as it's the easiest way to go through an array sequentially. This method will take the array passed and set every variable in the array to zero. So if this this array was to passed to addArray it should return 0. setZeroes will only take an array as a variable but will not return anything.

 public static void setZeroes(int[] array){
    for(int i=0; i<array.length; i++)
      array[i] = 0;
} 

In the main method we will want to see the effects of these methods. Store the created array of createArray in the variable in the main method, for example of length 10. Then we will want to see what the sum of this array is. Then we will use the third method to set all the values in the array to 0 and see if the zeroes are stored even though the array is not passed back. After the program is run the output would looks something like this:

 45
0
Program completed successfully.
Press any key to continue . . . 

As we can see the method altered the values in the array even though we didn't make the method return the array.

Code

Solution Code

Back to the Program-A-Day homepage