On the opposite side of the sheet of paper write functions t


On the opposite side of the sheet of paper write functions that return the following values. (Assume that a and n are parameters of the functions where a is an array of int values and n is the length of the array a.) (a) The smallest element in a. (b) The average of all elements in a. (c) The number of negative elements in a.

Solution


public class Calculate {
int smallest(int arr[],int len) //Method to calculate smallest element in the array
{
int min = arr[0];
for(int i=1;i<len;i++)
{
if(min>arr[i])
{
min=arr[i];
}
}
return min;
}

float average(int arr[],int len) //Method to calculate the average of all elements of array
{
int sum=0;
for(int i=0;i<len;i++)
{
sum+=arr[i];
}
float avg=(float)sum/len;
return avg;
}

int negativeCount(int arr[],int len) //Method to calculate number of negative elements in array
{
int count=0;
for(int i=0;i<len;i++)
{
if(arr[i]<0)
{
count++;
}
}
return count;
}

public static void main (String[] args) {
int arr[]={4,-3,5,-7,2,9};
int length=arr.length;
Calculate ob=new Calculate();
int minimum_element=ob.smallest(arr, length);
float average=ob.average(arr, length);
int count=ob.negativeCount(arr, length);

System.out.println(\"Smallest element in array is :\"+minimum_element);
System.out.println(\"Average of elements in array is :\"+average);
System.out.println(\"Number of Negative elements in array is :\"+count);
}
}

 On the opposite side of the sheet of paper write functions that return the following values. (Assume that a and n are parameters of the functions where a is an

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site