Lab 10
Version 1

 

  1. Write a method which takes an array of doubles and replaces each element with its absolute value. Use the Arraytest program from the previous labs to test the method.

  2. Write a method which takes an array of doubless and returns a new array in which each element is the reciprocal of the corresponding element in the original array. Again, use Arraytest to test your method.

  3. Add a method that takes two 3-place vectors and returns their cross-product in a new 3-place vector. Recall that for two 3-place vectors, A and B,

    A x B = ((A2B3 - A3B2), (A3B1 - A1B3), (A1B2 - A2B1))
    Your method need not check that the vectors given as actual parameters have length 3, you may assume they do.

    To test this method, add the definition of two 3-place arrays to your main method, and apply this method to them.

  4. This problem and the following ones are phrased in terms of grade arrays of the sort used in the lecture. Grade arrays are two dimensional arrays of ints. However, they are not rectangular: the individual exams have different numbers of grades.

    Write a method named printArray which takes a two-dimensional array of ints and displays its contents on the screen in neat rows and columns. You may assume the numbers in the array are between 0 and 100.

    You should copy the program ArrayTest2 (below) and add your methods to the end of it. It includes tests of each of the methods you are supposed to produce. Just un-comment the tests you want to run.

  5. Write a method named averageOfExam which takes a grade array and an exam number and returns the average of the scores for that exam. (Do not use the code from the notes.)

  6. Write a method named averageOfStudent which takes a grade array and an student number and returns the average of the scores for that student. (Do not use the code from the lecture.)

  7. Write a method named highestOnExam which takes a grade array and an exam number and returns the student number of the student who received the highest score on the exam.

  8. Write a method named averageOverAll which takes a grade array and returns the average of all the test scores.

  9. Write a method named copyStudent which takes a grade array and an student number and returns a one-dimensional array of ints containing the scores for that student.

// Program: ArrayTest2
 
import HMC.HMCSupport;
 
class ArrayTest2 {
 
  public static void main(String args[]) {
 
    int[][] examGrades = {{90,75,88,69,72,55,75,92,84,77},
                          {88,69,72,90,75,88,92,84,77},
                          {72,90,75,88,69,92,84,77,69,72}};
                          
    // HMCSupport.out.println("The array before the test:");
    // print_array(examGrades);
 
    // HMCSupport.out.println("The second exam average: " 
    //                        + averageOfExam(examGrades,1));
 
    // HMCSupport.out.println("The second students average: " 
    //                        + averageOfStudent(examGrades,1));
 
    // HMCSupport.out.println("The highest grade on the second exam: " 
    //                        + highestOnExam(examGrades,1));
 
    // HMCSupport.out.println("The average of all the grades: " 
    //                        + averageOverall(examGrades));
 
    // int[] s2 = copyStudent(examGrades,1));
    // s2[1]++;
    // HMCSupport.out.println("The second students grades: " 
    //                        + s2[0] + " " + s2[1] + " " + s2[2]);	
 
    // HMCSupport.out.println("The array after the tests:");
    // print_array(examGrades);
  }
 
 
  // Insert your methods here
 
}

Last modified August 28 for Fall 99 cs5 by fleck@cs.hmc.edu


This page copyright ©1998 by Joshua S. Hodas. It was built with Frontier on a Macintosh . Last rebuilt on Thu, Nov 12, 1998 at 9:06:54 AM.
http://www.cs.hmc.edu/~hodas/courses/cs5/week_10/lab.html