Question

In: Computer Science

Write a fragment of code that uses two existing arrays of doubles, a and b, creates...

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)

Solutions

Expert Solution

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
       }
   }
}


Related Solutions

Write using C++ a) Inputs two 1D arrays of doubles A[i] and B[i] from the keyboard...
Write using C++ a) Inputs two 1D arrays of doubles A[i] and B[i] from the keyboard with a maximum size of 1000. The elements are input one at time alternating between A and B (ie A[0],B[0],A[1],B[1], …, A[i],B[i]) until a value of less than -1.0e6 is input or i >= 1000. Then the program continues to part b). b) Calculates C = A + B, where + is vector addition (ie C[i] = A[i] + B[i]), and prints each element...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an array which can store up to 50 student names where a name is up to 25 characters long an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
C++ Assignment Inheritance This uses some single arrays of doubles. We can use Vectors instead if...
C++ Assignment Inheritance This uses some single arrays of doubles. We can use Vectors instead if we want! Choice either vectors or arrays either one is fine We will implement a classic Inheritance hierarchy. A simple console based interface is all that is needed. Build your classes first, each in their own .h and .cpp files, then test them with the simple main method provided below. Phase 1 : Here is the following set of classes you will implement and...
Write a fragment of MIPS code to convert temperature in Kelvin to Fahrenheit
Write a fragment of MIPS code to convert temperature in Kelvin to Fahrenheit
Write a method weave that takes two arrays of ints, a and b, and that returns...
Write a method weave that takes two arrays of ints, a and b, and that returns an array that contains the elements of a and b in the order a[0], b[0], a[1], b[1], etc. If one of the arrays a or b is longer than the other, just add the extra elements at the end of the array. In your solution, you can use only 3 arrays, namely the two arrays a, and b passed to the method and the...
Write a program that uses an array of doubles initialized to whatever values you wish. Write...
Write a program that uses an array of doubles initialized to whatever values you wish. Write methods to calculate and return the minimum and a method to calculate and return the maximum value in the array. You must write YOUR ORIGINAL methods for minimum and maximum. You MAY NOT use Math class methods or other library methods. Write an additional method that accepts the array as a parameter and then creates and returns a new array with all the same...
Write a code fragment in C language that tests the value of an integer num1. If...
Write a code fragment in C language that tests the value of an integer num1. If the value is 10, square num1. If it is 9, read a new value into num1. If it is 2 or 3, multiply num1 by 99 and print out the result. Implement your code using SWITCH statements
Write a program the declares and uses two parallel arrays. One array for storing the names...
Write a program the declares and uses two parallel arrays. One array for storing the names of countries and a second array for storing the populations of those countries. As you can see per the following the Country name and it's corresponding Population are stored at the same element index in each array. China 1367960000 India 1262670000 United States 319111000 Indonesia 252164800 Brazil 203462000 Pakistan 188172000 Nigeria 178517000 Bangladesh 157339000 Russia 146149200 Japan 127090000 In the main method write a...
Write and test a merge function that uses a recursive algorithm to merge two sorted arrays...
Write and test a merge function that uses a recursive algorithm to merge two sorted arrays of integers. Neither list contains duplicates, and the resulting list should not contain duplicates either. Hint: You may want to call a helper function from merge. PROGRAM: C
Write a program the declares and uses two parallel arrays. One array for storing the names...
Write a program the declares and uses two parallel arrays. One array for storing the names of countries and a second array for storing the populations of those countries. As you can see per the following the Country name and it's corresponding Population are stored at the same element index in each array. China 1367960000 India 1262670000 United States 319111000 Indonesia 252164800 Brazil 203462000 Pakistan 188172000 Nigeria 178517000 Bangladesh 157339000 Russia 146149200 Japan 127090000 In the main method write a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT