In: Computer Science
Write a fragment of code that uses two existing arrays of doubles, a and b, creates a third array c, and assigns each element of c to the average of the corresponding elements of a and b. You may assume that the arrays a and b are the same length. (java)
Question : Write a fragment of code that uses two existing arrays of doubles, a and b, creates a third array c, and assigns each element of c to the average of the corresponding elements of a and b. You may assume that the arrays a and b are the same length. (java)
Program :
public class Sum{
public static void main(String args[]){
double a[] =
{1.23,2.11,3.54,4.76,5.66,6.78,7.22,8.112,9.10,10.11};
//declaration and intailization of double array a
double b[] =
{11.431,22.129,33.444,44.12,55.32,66.01,77.412,88.432,99.954,1010.123};
//declaration and intailization of double array b
double c[] = new
double[10]; //declaration of double array
c
int i;
double avg;
System.out.print(" ");
System.out.print("Double Array list
a : ");
for(i = 0;i <
a.length;i++){
System.out.print(a[i] + " ");
//printing double array a
}
System.out.print(" ");
System.out.print("Double Array list
b : ");
for(i = 0;i <
b.length;i++){
System.out.print(b[i] + " ");
//printing double array b
}
System.out.print(" ");
for(i = 0;i <
a.length;i++){
avg = (a[i] +
b[i])/2; //
assigning each element of c to the average of the corresponding
elements of a and b
c[i] =
avg;
}
System.out.print("Double Array list
c : ");
for(i = 0;i <
c.length;i++){
System.out.println(c[i]);
//printing double array c
}
}
}