Question

There are many approaches to locating peaks. Consider an array of data x[]. For this program,...

There are many approaches to locating peaks. Consider an array of data x[]. For this program, 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.

Write a Java 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 the 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 data for each peak.

A sample run:

% 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:

0.5

0.7

0.45

0.85

0.65

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[] .

0 0
Add a comment Improve this question Transcribed image text
Answer #1


/**
* The java program FindPeaks that finds the peaks
* of the given array x and prints the peaks
* array to console
* */
//FindPeaks.java
public class FindPeaks {  
   public static void main(String[] args) {
      
       //initialize variables
       int count=0;      
       int pIndex=0;
      
       //intialize float array
       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};
      
       //create a float array of given size of x
       float[] peaks=new float[x.length];
       System.out.println("Data array:");
       //print data to console
       for (int i = 0; i < x.length; i++)
       {
           if((i+1)%10==0)
               System.out.println();
           else
               System.out.printf("%-8.4f",x[i]);          
       }
      
      
       System.out.println("\n");
       //find peaks
       for (int i = 1; i < x.length-1; i++)
       {
           float val=x[i];
           //checking condition of peak element
           if(val>2*x[i-1] && val>2*x[i+1])
           {
               peaks[pIndex]=val;
               pIndex++;
               count++;
           }
       }
       //print peaks to console
       System.out.println(count+" peaks found:");
       for (int i = 0; i < count; i++)
       {
           System.out.println(peaks[i]);
       }      
   }

}

------------------------------------------------------------------------------------------------------------

Sample output:

Data array:
0.2000 0.5000 0.1000 0.1500 0.2000 0.1300 0.3000 0.2500 0.3000
0.7000 0.2000 0.4500 0.1500 0.2000 0.8500 0.3000 0.6500 0.2000


5 peaks found:
0.5
0.7
0.45
0.85
0.65

Add a comment
Know the answer?
Add Answer to:
There are many approaches to locating peaks. Consider an array of data x[]. For this program,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT