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

Consider an array of data x[]. For our project, we define a peak as an element o

ID: 674066 • Letter: C

Question

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

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[] . This is the very common operation of building a list that contains elements with “interesting” properties; we’ll discuss how to do this in class.

Explanation / Answer

#include<stdio.h>
#include<math.h>
#include<conio.h>
void factor(float);
void sort(float[]);
float peak[10];
int noe=0,j=0;
main()
{
   float x[]={0.2f,0.5f,0.1f,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 i;
   clrscr();
   for(i=0;i<10;i++)
   printf(" %.2f",x[i]);
   printf(" ");
   for(i=10;i<19;i++)
       printf(" %.2f",x[i]);
   printf(" The Peak elements in the array are");
   for(i=0;i<20;i++)
   {
       if(i<=0)
       {
           if(x[i]>x[i+1])
           {
               factor(x[i]);
           }
       }
       else if(i>0 && i< 19)
       {
           if(x[i]>x[i-1] && x[i]>x[i+1])
           {
                       factor(x[i]);
           }
       }
       else
       {
           if(x[i]>x[i-1])
           {
               factor(x[i]);
           }
       }
   }
   sort(peak);
return 0;
}

void factor( float y)
{
   float i;int count=0,k;int l;
   for(i=0.02;i<y;i=i+0.01)
   {
       k=fmod(y,i);
       if(k==0)
           count ++;
   }
   if(count>=2)
   {
       printf(" %.2f", y);
       peak[j]=y;
       j++;
       noe++;
   }

}

void sort(float peak[])
{
int i,j;
float temp;
for(i=0;i<noe;i++)
   {
       for(j=i+1;j<=noe;j++)
       {
           if(peak[i]>peak[j])
           {
               temp=peak[i];
               peak[i]=peak[j];
               peak[j]=temp;
           }
       }
   }
   for(i=0;i<noe;i++)
   {
       printf(" ");
       printf(" Peak element at peak[%d] id %.2f",i,peak[i]);
   }
}