Document Sample
T6 Powered By Docstoc
					                                                                 SCE/CPE102/CSC102/Tutorial 6

Tutorial 6 (Week 9)
1. Write a method reverseAr() which can print the array of integers in reverse order.
   For example, if ar[5] = {1,2,3,4,5}, then the output 5, 4, 3, 2, 1 will be
   printed on the display after applying the method reverseAr(ar). The method
   header is given as follows:

       public static void reverseAr(int[] ar)

2. Write a method averageAr() which returns the average of double values stored in
   an array. The method header is given as follows:

       public static double averageAr(int[] ar)

3. Write a method find() that returns the subscript of the first appearance of a number
   in the array. For example, if the ar = {3,6,9,4,7,8}, then find(ar,3) will
   return 0 where 3 is the number to find, and find(ar,9) will return 2. If the
   required number is not in the array, the method will return –1. The method header is
   given as follows:

       public static int find(int[] ar, int number)

4. Write a program which will draw the histogram for n integers from 0 to 99. N is input by
   the user. Each of the n numbers will be generated by calling Math.random(). The
   program will consist of two functions (i) to collect the frequency distribution of the
   numbers (ii) to print the histogram. An example histogram is shown here.

       0–9       |*********************
       10 – 19   |************
       20 – 29   |*************
       30 – 39   |**
       90 – 99   |***************

5. Write the code for the following functions :

   (1) public static int[][] transpose(int[][] M);
   /* transposes a square matrix and returns the resulting matrix. */

   (2) public static int[][] swap2Rows(int[][] M, int r1, int r2);
   /* swaps row M[r1] with row M[r2] and returns the resulting matrix */

   (3) public static void swap2Cols(int[][] M, int c1, int c2);
   /* swaps column M[][c1] with column M[][c2] and retruns the resulting matrix*/


Shared By: