In: Computer Science
IN JAVA
Array Operations
Write a program with an array that is initialized with test data. Use any primitive data type of your choice. The program should also have the following methods:
Demonstrate each of the methods in the program.
Program in Java -
public class arrayOperations{
static int getTotal(int[] a){
int sum=0;
for(int i=0;i<a.length;i++)
sum = sum+a[i];
return sum;
}
static float getAverage(int[] a){
int sum=0;
for(int i=0;i<a.length;i++)
sum = sum+a[i];
float average=sum/a.length;
return average;
}
static int getHighest(int[] a){
int max=a[0];
for(int i=0;i<a.length;i++)
if(a[i]>max)
max=a[i];
return max;
}
static int getLowest(int[] a){
int min=a[0];
for(int i=0;i<a.length;i++)
if(a[i]<min)
min=a[i];
return min;
}
public static void main(String []args)
{
int a[]=new int[]{55,66,11,33,99,2,4,6,88,77};
int Sum = getTotal(a);
float Average = getAverage(a);
int Highest=getHighest(a);
int Lowest=getLowest(a);
System.out.println("Sum of array values="+Sum);
System.out.println("Average of array values="+Average);
System.out.println("Highest value in array="+Highest);
System.out.println("Lowest value in array="+Lowest);
}
}
The output of the Program -