public class MethodFold
{
public static int[] fold (int[] numbers)
{
int[] sum = new int [numbers.length / 2]; // = 8/2 (dont forget that sum is an array)
for (int i = 0 ; i < sum.length ; i++)
{ // = i < 4
sum [i] = numbers [i] + numbers [i + sum.length]; // + "numbers" array value "i" + 4
//System.out.println (sum[i]); ===> outputs properly but during calculation, not after.
}
return sum; // sum is the array thats taken from all this code in method^
}
public static void main (String[] args)
{
int[] nums = {1, 2, 3, 4, 5, 6, 7, 8};
int[] array = fold (nums); //variable that calls the method above and takes the values into its array as calculated
for (int i = 0 ; i < array.length ; i++)
{ //outputs the variable array once everything is calculated within for
System.out.println (array [i]);
}
}
}