Table 1: Trace for avgPct markResult(18, 23, 19) Statement score1 score2 score3 sum avgOutOf25 avgPct Initial values 1. sum score1 + score2 + score3 2. avgOutOf25 sum / 3 3. avgPct avgOutOf25 * 4 Program Memory Exercise 4-2 Tracing a Call Working memory
Exercise 6-7 – Array Indexing The index (subscript) of an array of length l may be any integer expression that returns a value in the range 0...(l-1). Suppose k =2, and A references a[2] = a[k] = a[2*k-1] = a[a[0]+1 ] = Program Memory Exercise 6-8 – Value in Middle of Array Working memory Global Memory
Section 7 Exercises Program Memory Exercise 7-1 – Swap Values in an Array Working memory Global Memory Exercise 7-1: – Table 1, page 1, main Exercise 7-1: Trace – Table 1, page 2, main Exercise 7-1: Trace – Table 2, arraySwap(a, 0, 5)
GIVENS: (none) RESULT: (none) INTERMEDIATE: digits (reference to an integer array of digits) n (number of elements in the array) testValid (Indicated if card is valid) HEADER: main BODY: /* the main method calls the other methods in order to solve particular */ public static void main (String [ ] args) { int [ ] digits; // reference variable to array of digits boolean testValid; // indicates if card is valid // body digits = DigitsLib.readDigits( ); // call to read the data while ((digits.length == 4) && (!DigitsLib.isZero(digits))) { // sends the number to the valid() method testValid = valid(digits); // print the result if (testValid) { System.out.println("This number is valid."); } else { System.out.println("This number is invalid."); } } } GIVENS: digits (reference to array of digits) n (number of elements in the array) RESULT: isValid (TRUE if card number is FALSE otherwise) ITERMEDIATE: firstThree (first three digits of last group) lastDigit (last of credit card number) sum (sum of 15 digits) HEADER: isValid valid(digits, n) BODY: /* This method validates the credit card number */ private static boolean [ ] digits) { int firstThree; // first three digits of last group int lastDigit; // last digit of credit card number int sum; // sum of first 15 digits boolean isValid; // result: true if number is valid // find the first 3 digits pf the last group firstThree = // find the last digit of the number lastDigit = // find the sum of the first 15 digits sum = // determines the validity isValid = return isValid; } DigitsLib Class (DigitsLib.java)
GIVENS: digits (reference to array of digits) n (number of elements in the RESULT: flag (TRUE if first digit is zero, FALSE otherwise) INTERMEDIATE: (none) HEADER: flag isZero(digits, n) BODY: // first version: only the first digits need to be 0 public static boolean isZero(int [ ] digits) { boolean flag; // result flag = digits[0] == 0; return(flag); } //second version: all 16 digits need to be 0 public static boolean isZero(int [ ] digits) { } GIVENS: (none) ITERMEDIATE: RESULT: intArray (reference to integer array) n (number of elements in the array HEADER: intArray, n readDigits( ) BODY: /* This method asks the user to a credit card number as 4 integers, that will be placed in an array. This method calls readIntLine( ) from the class ITI1120 to read the array in integers. */ public static int [ ] readDigits( ) { int [ ] intArray; // reference to array System.out.println ("Please input the credit card number as four "); System.out.println ("numbers of four digits, separated by spaces;"); System.out.println("or press 0 to finish."); intArray = ITI1120.readIntLine( ); } GIVENS: x (integer number) RESULT: sum (sum of all digits in the integer number) INTERMEDIATE: HEADER: sum numDigits(x) BODY: // Returns the sum of the digits of a number x public static int sumDigits(int x) { int sum; // result – sum of digits // Body sum = 0; while (x != 0) { } return sum; }
Section 8 Exercises Program Memory Exercise 8-1 – Trace for this value of X Working memory Global Memory Exercise 8-1 – Trace, Table 1 – recSum(x, 3) statement Ref. by x n s initial values partialS recSum(x,n-1) s recSum (x,n) Exercise 8-1 – Trace, Table 2 – recSum(x, 2) partial
statement Ref. by x n partialS s initial values partialS recSum(x,n-1) s recSum (x,n) Exercise 8-1 – Trace, Table 3 – recSum(x, 1) statement Ref. by x n s initial values Program Memory Exercise 8-2 – Translating SumRec to Java Working memory Global Memory
GIVENS: a (An array of numbers) n (Number of array elements to test) RESULT: isEqual (Boolean: TRUE if all values in elements up to position n are equal) INTERMEDIATE: partialRes (partial result) HEADER: isEqual upToNSame(a, n) BODY: Efficient Version // Method upToNSame – Are elements up to n in a public static boolean upToNSame(int[] a, int n) { // VARIABLE DECLARATION / DATA DICTIONNARY boolean partialRes; // INTERMEDIATE: partial result boolean isEqual; // RESULT: expected result // ALGORITHM BODY // RETURN RESULT return isEqual; } // Method upToNSame – efficient version public static boolean upToNSame(int[] a, int n) { // VARIABLE DECLARATION / DATA DICTIONNARY boolean isEqual; // RESULT: expected result // ALGORITHM BODY // RETURN RESULT return isEqual; } Exercise 8-5 - Calculate n !.
GIVENS: a (an to sort) n (number of elements in array) RESULTS: pos (index of largest value in array) INTERMEDIATES: m (integer, smaller interval) posM (position of largest value in smaller HEADER pos LocateLargestValue( a, n ) public static void locateLargestValue(int[] a, int n) { // VARIABLE DECLARATIONS // GIVENS: a – reference to array to sort // n – number of elements to sort – note // that a.length CANNOT be used. int pos; // RESULT – position of largest value // INTERMEDIATES int m; // smaller int posM; // used for swapping // RESULT return(pos); }