Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

When working with a sequence of data with a lot of variation, such as the averag

ID: 675292 • Letter: W

Question

When working with a sequence of data with a lot of variation, such as the average temperature over a number of days, it is often useful to find the well-defined peaks in the data that stand out from the background variation. These correspond to the days that are hotter than usual.

There are many approaches to locating peaks. Consider an array of data x[]. For our project, we define a peak as an element of the array that is a local maximum, greater than each of its neighbors by a factor of 2 or more. The first and last elements in an array have only one neighbor each, and by definition cannot be peaks. Hence, given this array x[] with 20 terms:

0.2000 0.5000 0.1000 0.1500 0.2000 0.1300 0.3000 0.2500 0.3000 0.3000

0.7000 0.2000 0.4500 0.1500 0.2000 0.8500 0.3000 0.6500 0.2000 0.1000

The peaks are at x[1] = 0.5, x[10] = 0.7, x[12] = 0.45, x[15] = 0.85, x[17] = 0.65.

For this project, you will write a program that scans through an array of data, finds the peaks based on the criterion defined above, and sorts them into non-descending order. Here’s what you program should do:

Declare and initialize an array x[] of 20 floats.

float [] x = {0.2f, 0.5f, 0.1f, 0.15f, 0.2f, 0.13f, 0.3f, 0.25f, 0.3f, 0.3f,

0.7f, 0.2f, 0.45f, 0.15f, 0.2f, 0.85f, 0.3f, 0.65f, 0.2f, 0.1f};

Print the elements of x[], 10 on each line.

Then locate the peaks in x[], place them in an array peaks[]

Print the index and data for each peak, in the sorted order

A sample run:

unixlab% java FindPeaks

Data array:

0.2000 0.5000 0.1000 0.1500 0.2000 0.1300 0.3000 0.2500 0.3000 0.3000

0.7000 0.2000 0.4500 0.1500 0.2000 0.8500 0.3000 0.6500 0.2000 0.1000

5 peaks found:

1 0.5

10 0.7

12 0.45

15 0.85

17 0.65

unixlab%

After printing the elements of x[] , your program will first step through the elements of x[] , find each peak, and add each peak to the array peaks[] .

Explanation / Answer

package chegg;

public class Peaks {

   public static void main(String[] args) {

       float [] x = {0.2f, 0.5f, 0.1f, 0.15f, 0.2f, 0.13f, 0.3f, 0.25f, 0.3f, 0.3f,

               0.7f, 0.2f, 0.45f, 0.15f, 0.2f, 0.85f, 0.3f, 0.65f, 0.2f, 0.1f};

       int peakCount = 0;

       System.out.print("DATA ARRAY:");

       for (int i = 0; i < x.length; i++) {

           if (i%10 == 0) {

               System.out.println();

           }

           System.out.printf("%.4f ", x[i]);

       }

       System.out.println();

       for (int i = 1; i < x.length-1; i++) {

           if(x[i] > x[i-1]*2.0 && x[i] > x[i+1]*2.0) {

               peakCount++;

           }

       }

      

       int[] peaks = new int[peakCount];

       int index=0;

       for (int i = 1; i < x.length-1; i++) {

           if(x[i] > x[i-1]*2.0 && x[i] > x[i+1]*2.0) {

               peaks[index++] = i;

           }

       }

      

       System.out.println(peakCount+" peaks found");

       for (int i = 0; i < peakCount; i++) {

           System.out.println(peaks[i]+" "+x[peaks[i]]);

       }

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote