ESSAY Write your answer in the space provided or on a separa

ESSAY. Write your answer in the space provided or on a separate sheet of paper.

36) Write a method to compute the average of an int array and return the value as a double. The int array and the number of elements in the array are both passed as parameters to the method in that order.

37) Write a method to compute and return the value of max - min where max is the largest element of an int array and min is the smallest element of an int array. The method is passed the int array and an int value that denotes the number of elements in the array. You may assume that the int array has at least 1 element in it.

38) Write code fragment to swap the two Strings stored by variables a and b.

39) Write a method to output all elements of a two-dimensional array of int values as a table of rows and columns. Use \"\\t\" to get elements to line up properly. The method is passed the two-dimensional array and two int values that denote the number of both dimensions (rows followed by columns).

Solution

Question 36:

public double average (int a[], int n){
       int sum = 0;
       for(int i=0; i<n; i++){
           sum = sum + a[i];
       }
       double average = sum/(double)n;
       return average;
   }

Question 37:


   public int max(int a[], int n){
       int max = a[0];
       for(int i=0; i<n; i++){
           if(max < a[i]){
               max = a[i];
           }
       }
       return max;
   }

Question 38:

   public void swap(String a, String b){
       String temp = \"\";
       temp = a;
       a = b;
       b = temp;
   }

Question 39:

public void displayArray(int a[][],int rows, int cols){
       for(int i=0; i<rows; i++){
           for(int j=0; j<cols; j++){
               System.out.print(a[i][j]+\"\\t\");
           }
           System.out.println();
       }
   }

ESSAY. Write your answer in the space provided or on a separate sheet of paper. 36) Write a method to compute the average of an int array and return the value a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site