// ITI 1120 2011 // Name: Diana Inkpen, Student# 123456 //import java.io.*; /** * This program computes the average of three numbers. */ class SumArray_rec { public static void main (String[] args) { // DECLARE VARIABLES/DATA DICTIONARY int [] a; int result; // READ IN GIVENS System.out.println( "Please enter the array:" ); a = ITI1120.readIntLine( ); // CALLS THE ALGORITHM result = sumArray(a); // PRINT OUT RESULTS AND MODIFIEDS System.out.println( "The sum is " + result ); // CALLS THE ALGORITHM result = sumArray_rec(a, a.length); // PRINT OUT RESULTS AND MODIFIEDS System.out.println( "The sum is " + result ); } // If the 'main' method calls other algorithms, put the method(s) below. /** * This method computes the average of three numbers. */ public static int sumArray ( int [] x) { // VARABLE DECLARATIONS int index; // intermediate int sum; // RESULT //BODY OF THE ALGORITHM sum = 0; for (index = 0; index < x.length; index = index +1) { sum = sum + x [index]; } // RETURN RESULTS return sum; } public static int sumArray_rec(int [] x, int n) { int s; int sum; if (n == 1) { sum = x[0]; } else { s = sumArray_rec(x, n - 1); sum = s + x[n - 1]; } return sum; } }