发布网友 发布时间:2022-05-15 09:45
共1个回答
热心网友 时间:2023-10-18 15:31
第一题结果:
#include <stdio.h>
#define DATALEN 27
int data[DATALEN]=
{13, 15, 16,
16, 19, 20,
20, 21, 22,
22, 25, 25,
25, 25, 30,
33, 33, 35,
35, 35, 35,
36, 40, 45,
46, 52, 70};
int nSmoothByMeans[DATALEN]={0};
int nSmoothByMedians[DATALEN]={0};
int nSmoothByBoundaries[DATALEN]={0};
void SmoothByMeans(int depth)
{
int i=0,j=0;
int sum=0 , mean=0;
for(i=0;i<27;i=i+3)
{
for (j=i;j<i+depth;j++)
{
sum+=data[j];
}
mean = sum/depth;
for (j=i;j<i+depth;j++)
{
nSmoothByMeans[j]=mean;
}
sum = 0;
}
}
void SmoothByMedians(int depth)
{
int i=0,j=0;
for(i=1;i<27;i=i+3)
{
for (j=i-1;j<i+depth;j++)
{
nSmoothByMedians[j]=data[i];
}
}
}
void SmoothByBoundaries(int depth)
{
int i=0,j=0;
for(i=0;i<27;i++)
{
nSmoothByBoundaries[i]=data[i];
}
for (i=1;i<27;i=i+3)
{
if (data[i]-data[i-1]>data[i+1]-data[i])
{
nSmoothByBoundaries[i]=data[i+1];
}
else
{
nSmoothByBoundaries[i]=data[i-1];
}
}
}
void main()
{
int depth = 3;
int i=0;
int j=0;
SmoothByMeans(3);
SmoothByMedians(3);
SmoothByBoundaries(3);
printf("原始数据:\n");
for(i=0,j=1;i<27;i=i+3,++j)
{
printf("Bin %d : %d,%d,%d\n",
j,data[i],data[i+1],data[i+2]);
}
printf("使用平均值:\n");
for(i=0,j=1;i<27;i=i+3,++j)
{
printf("Bin %d : %d,%d,%d\n",
j,nSmoothByMeans[i],nSmoothByMeans[i+1],nSmoothByMeans[i+2]);
}
printf("使用中值:\n");
for(i=0,j=1;i<27;i=i+3,++j)
{
printf("Bin %d : %d,%d,%d\n",
j,nSmoothByMedians[i],nSmoothByMedians[i+1],nSmoothByMedians[i+2]);
}
printf("使用边界值:\n");
for(i=0,j=1;i<27;i=i+3,++j)
{
printf("Bin %d : %d,%d,%d\n",
j,nSmoothByBoundaries[i],nSmoothByBoundaries[i+1],nSmoothByBoundaries[i+2]);
}
}