CodeLab 11.13.06 - CodeLab: 11.13.06 Write the definition...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CodeLab : 11.13.06 Write the definition of a method named sumArray that has one parameter, an array of int s. The method returns the sum of the elements of the array as an int. public int sumArray(int[] s) { int sum = 0; for(int i = 0; i < s.length; i++) { sum += s[i]; } return sum; } Write the definition of a method, isReverse, whose two parameters are arrays of integers of equal size. The method returns true if and only if one array is the reverse of the other. ("Reverse" here means same elements but in reverse order.) public boolean isReverse(int[] x, int[] y) { boolean result = true; for(int i = 0; i < x.length; i++) { if (x[i] != y[y.length - i - 1]) result = false; } return result; } Write the definition of a method reverse, whose parameter is an array of integers. The method reverses the elements of the array. The method does not return a value. More. .. public void reverse(int[] a) { for(int i = 0; i < a.length/2; i++) { int temp = a[i]; a[i] = a[a.length - i - 1]; a[a.length - i - 1] = temp; } }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Given an array arr, of type int, along with two int variables i and j, write some code that swaps the values of arr[i] and arr[j]. Declare any additional variables as necessary. int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; Given an array temps of double s, containing temperature data, compute the average temperature. Store the average in a variable called avgTemp. Besides temps and avgTemp, you may use only two other variables -- an int variable k and a double variable named total, which have been declared. total = 0;
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/29/2008 for the course CS 302 taught by Professor Willbenton during the Fall '07 term at University of Wisconsin.

Page1 / 5

CodeLab 11.13.06 - CodeLab: 11.13.06 Write the definition...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online