简体   繁体   English

为什么定义指令在这里不起作用?

[英]Why define directive is not working here?

This is the code I'm trying to run and this is just taking one input and giving me the standard deviation result.这是我要运行的代码,这只是接受一个输入并给我标准偏差结果。 Also the 'SIZE' is highlighted as red in GNU nano.此外,“SIZE”在 GNU nano 中以红色突出显示。 I wanna know why, so please help.我想知道为什么,所以请帮助。

This is the code-这是代码-

#include<stdio.h>
#include<math.h> 
#define SIZE 5  
float std_dev(float a[], int n);
float mean(float a[], int n);

int main(){
    float value[SIZE];
    int i;
    printf("Enter float values\n",SIZE);
    for(i=0; i<SIZE; i++){
        scanf("%f",&value[i]);
        printf("Std.deviation is %f\n", std_dev(value, SIZE));
    }
}

float std_dev(float a[], int n){
    int i;
    float x, sum=0.0;
    x=mean(a,n);
    for(i=0; i<n; i++){
        sum+=(x-a[i])*(x-a[i]);
        return(sqrt(sum/(float)n));
    }
}

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

Commands I gave for the output are-我为 output 发出的命令是 -

gcc StandardDeviation.c -o compilfile -lm gcc StandardDeviation.c -o compilfile -lm
./compilfile ./编译文件

One more thing, I'm using Kali Linux.还有一件事,我正在使用 Kali Linux。

I'm trying to get the result as the standard deviation after giving the five float values.在给出五个浮点值后,我试图将结果作为标准偏差。

Edit: I did the basic mistake here that I was using return statement inside the loop.编辑:我在这里犯了一个基本错误,我在循环中使用了 return 语句。 It should be something like this-应该是这样的——

float std_dev(float a[], int n){
int i;
float x, sum=0.0;
x=mean(a,n);
for(i=0; i<n; i++){
sum+=(x-a[i])*(x-a[i]);
}
return(sqrt(sum/(float)n));
}

-in every function I created here. - 在我在这里创建的每个 function 中。 Thank you for this solution @axiac.感谢您提供此解决方案@axiac。 Few more points-还有几点——

  1. I am using Visual Studio Code now.我现在正在使用 Visual Studio Code。

  2. I am learning how to format the code and ask question properly in this platform.我正在学习如何在这个平台上格式化代码并正确提问。

  3. I implemented all the suggestions you guys gave me.我实施了你们给我的所有建议。 Thank you!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM