using java Q3) Write a program to perform an experimental analysis of the two al
ID: 3698404 • Letter: U
Question
using java
Q3) Write a program to perform an experimental analysis of the two algorithms prefixAverage1 and prefixAverage2, Returns an array a such that, for all j, a equals the average of x[O], .I public static doublel ] prefixAverage1(doublel ] x) int n x.length; double[] a- new double[n]; II filled with zeros by default for (int j-0; jnj) double total 0;/ begin computing x[0] + for (int i-0; j;+) +XU totalx; all total /G+1) record the average return a Returns an array a such that, for all j, aul equals the average of x[O],. public static doublel ] prefixAverage2(doublel ] x) double[ ] a new double[n]; I/ filled with zeros by default double total 0; compute prefix sum as x[O] x1]+ for (int j-0:jn:i+) totalx;/update prefix sum to include x] lcompute average based on current sum return a; )Explanation / Answer
Solution:
The driver method for both the functions is given below:
DriverAverageFunction.java:
package chegg;
public class DriverAverageFunctions {
public static void main(String[] args) {
double x[]= {1, 2, 3, 4, 5, 6,7 ,8 , 9, 10};
long timeStart = System.currentTimeMillis();
prefixAverage1(x);
long timeStop = System.currentTimeMillis();
long timeElapsed = timeStop - timeStart;
System.out.println("Time taken by prefixAverage1 is: " + timeElapsed);
long timeStart2 = System.currentTimeMillis();
prefixAverage2(x);
long timeStop2 = System.currentTimeMillis();
long timeElapsed2 = timeStop2 - timeStart2;
System.out.println("Time taken by prefixAverage2 is: " + timeElapsed2);
}
}
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.