C 语言的众数、中位数、平均值、标准差、方差计算器

发布于 2025-01-02 09:24:18 字数 2241 浏览 3 评论 0原文

尝试编写这个小程序来帮助我在统计课上,除了中位数之外,所有内容似乎都在相应计算。我缺少什么?

如果有人愿意为我做方差函数,则额外加分 x)。

使用 GCC 编译器运行 OSX。

#include<stdio.h>  
#include<math.h>   

float mean1(float[],int);  
float median1(float[],int);  
float mode1(float[],int);  
double standarddeviation1(float[],int);  

int main()  
{  
int i,n,choice;  
float array[100],mean,median,mode;  
double standarddeviation;  

printf("Enter No of Elements\n");  
   scanf("%d",&n);  
printf("Enter Elements\n");  

for(i=0;i<=n-1;i++)  
    scanf("%f",&array[i]);  
do  
{  
    printf("\n\tEnter Choice\n\t1.Mean\n\t2.Median\n\t3.Mode\n\t4.Standard deviation\n\t5.Exit\n");  
    scanf("%d",&choice);  

    switch(choice)  
    {  
        case 1: mean=mean1(array,n);  
            printf("\n\tMean = %f\n",mean);  
            break;  
        case 2: median=median1(array,n);  
            printf("\n\tMedian = \n",median);  
            break;  
        case 3: mode=mode1(array,n);  
            printf("\n\tMode = %f\n",mode);  
            break;   
        case 4: standarddeviation=standarddeviation1(array,n);  
            printf("\n\tStandard deviation = %f\n",standarddeviation); 
            break;  
        case 5: break;  
        default:printf("Wrong Option");  
            break;  
    }  

}while(choice!=5);  
  getchar(); 

return 0;  
}  

float mean1(float array[],int n) {  
    int i;  
    float sum=0;  
    for(i=0;i<=n;i++)  
    sum=sum+array[i];  
return (sum/n);  
}  
float median1(float array[],int n) {  
float temp;  
int i,j;  
for(i=n-1;i>=0;i--)  
    for(j=0;j<=i;j++)  
        if(array[j]>=array[j+1])  
        {  
            temp=array[j];  
            array[j]=array[j+1];  
            array[j+1]=temp;  
        }  

if(n%2==0)  
    return (array[n/2]+array[n/2-1])/2;  
else  
    return array[n/2];  
}  
float mode1(float array[],int n) {  
return (3*median1(array,n)-2*mean1(array,n));  
}  
double standarddeviation1(float array[],int n) {  
int j;   
double max[100],sum,variance,mean;  
mean=mean1(array,n);  
sum=0;  
for(j=0;j<=n;j++)   
{  
    max[j]=pow((array[j]-mean),2);  
    sum+=max[j];  
}  
variance=sum/(j-1);   
return sqrt(variance);  
}  

Trying to write this small program to help me in my Stats class, everything seems to be calculating accordingly except for the Median. What am i missing?

Extra Credit if anyone is willing to do the Variance function for me as well x).

Running OSX with the GCC compiler.

#include<stdio.h>  
#include<math.h>   

float mean1(float[],int);  
float median1(float[],int);  
float mode1(float[],int);  
double standarddeviation1(float[],int);  

int main()  
{  
int i,n,choice;  
float array[100],mean,median,mode;  
double standarddeviation;  

printf("Enter No of Elements\n");  
   scanf("%d",&n);  
printf("Enter Elements\n");  

for(i=0;i<=n-1;i++)  
    scanf("%f",&array[i]);  
do  
{  
    printf("\n\tEnter Choice\n\t1.Mean\n\t2.Median\n\t3.Mode\n\t4.Standard deviation\n\t5.Exit\n");  
    scanf("%d",&choice);  

    switch(choice)  
    {  
        case 1: mean=mean1(array,n);  
            printf("\n\tMean = %f\n",mean);  
            break;  
        case 2: median=median1(array,n);  
            printf("\n\tMedian = \n",median);  
            break;  
        case 3: mode=mode1(array,n);  
            printf("\n\tMode = %f\n",mode);  
            break;   
        case 4: standarddeviation=standarddeviation1(array,n);  
            printf("\n\tStandard deviation = %f\n",standarddeviation); 
            break;  
        case 5: break;  
        default:printf("Wrong Option");  
            break;  
    }  

}while(choice!=5);  
  getchar(); 

return 0;  
}  

float mean1(float array[],int n) {  
    int i;  
    float sum=0;  
    for(i=0;i<=n;i++)  
    sum=sum+array[i];  
return (sum/n);  
}  
float median1(float array[],int n) {  
float temp;  
int i,j;  
for(i=n-1;i>=0;i--)  
    for(j=0;j<=i;j++)  
        if(array[j]>=array[j+1])  
        {  
            temp=array[j];  
            array[j]=array[j+1];  
            array[j+1]=temp;  
        }  

if(n%2==0)  
    return (array[n/2]+array[n/2-1])/2;  
else  
    return array[n/2];  
}  
float mode1(float array[],int n) {  
return (3*median1(array,n)-2*mean1(array,n));  
}  
double standarddeviation1(float array[],int n) {  
int j;   
double max[100],sum,variance,mean;  
mean=mean1(array,n);  
sum=0;  
for(j=0;j<=n;j++)   
{  
    max[j]=pow((array[j]-mean),2);  
    sum+=max[j];  
}  
variance=sum/(j-1);   
return sqrt(variance);  
}  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明媚如初 2025-01-09 09:24:18

您的 mean1 也不正确。这两个错误都有相同的原因,您访问的元素超过指定的编号 n

float mean1(float array[],int n) {  
    int i;  
    float sum=0;  
    for(i=0;i<=n;i++)  
    sum=sum+array[i];  
return (sum/n);  
}

您在此处添加 n+1 元素,但除以 n。第 n+1 个元素不属于计算范围。使循环条件i << n

float median1(float array[],int n) {  
float temp;  
int i,j;  
for(i=n-1;i>=0;i--)  
    for(j=0;j<=i;j++)

在冒泡排序中,您也可以访问 array[n] ,它具有任意值(如果 n == 100 超出了分配的数组,因此会导致未定义的行为) 。使内循环条件j < i 或使用 i = n-2 启动外循环。

standarddeviation1 中,您也超出了数组范围。

Your mean1 isn't correct either. Both errors have the same reason, you access an element past the assigned number n

float mean1(float array[],int n) {  
    int i;  
    float sum=0;  
    for(i=0;i<=n;i++)  
    sum=sum+array[i];  
return (sum/n);  
}

You are adding n+1 elements here but divide by n. The n+1st element doesn't belong in the calculation. Make the loop condition i < n.

float median1(float array[],int n) {  
float temp;  
int i,j;  
for(i=n-1;i>=0;i--)  
    for(j=0;j<=i;j++)

In your bubble sort, you access array[n] too, which has an arbitrary value (and if n == 100 is past the allocated array, hence causes undefined behaviour). Make the inner loop condition j < i or start the outer loop with i = n-2.

In standarddeviation1 you overstep the array bounds too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文