Array

14. Program

How to find out average of element of an int array ?

class U { public static void main(String args[]) { int[] a = {10, 2, 5, 3, 9, 2, 8, 10, 11, 20, 10}; double sum = 0; for(int i = 0; i < a.length; i++) { sum = sum + a[i]; } double avg = sum / a.length; System.out.println("Sum of element is : "+ sum); System.out.println("Average of element is : "+ avg); System.out.println("Length : "+ a.length); } }

15. Program

(Imp_Q) How to find out average of first half and second half element array ?

class V { public static void main(String args[]) { int[] a = {10, 2, 5, 3, 9, 2, 8, 10, 11, 20}; int mid = a.length / 2; double sum = 0; for(int i = 0; i < mid; i++) { sum = sum + a[i]; } double avg = sum / mid; System.out.println("Sum : "+ sum); System.out.println("Average of first half element is : "+ avg); System.out.println("-----------"); sum = 0; avg = 0; for(int i = mid; i < a.length; i++) { sum = sum + a[i]; } if(a.length % 2 != 0) { mid++; } avg = sum / mid; System.out.println("Sum : "+ sum); System.out.println("Average of second half element is : "+ avg); } }

16. Program

How to find out average of even index and odd index element seperatly ?

class W { public static void main(String args[]) { int[] a = {10, 2, 5, 3, 9, 2, 8, 10, 11, 20, 10}; double sum = 0; int total = 0; for(int i = 0; i < a.length; i+=2) { sum = sum + a[i]; total++; } double avg = sum / total; System.out.println("Sum : "+ sum); System.out.println("Average of even index element is : "+ avg); System.out.println("-----------"); sum = 0; avg = 0; total = 0; for(int i = 1; i < a.length; i+=2) { sum = sum + a[i]; total++; } avg = sum / total; System.out.println("Sum : "+ sum); System.out.println("Average of odd index element is : "+ avg); } }

Page....