gpt4 book ai didi

c - 如何迭代这个 sin 方程?

转载 作者:行者123 更新时间:2023-11-30 19:27:15 27 4
gpt4 key购买 nike

我无法创建正确的文件。程序中的方程不会迭代,它只会根据请求的数量写入相同的总和。

for 循环。

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

#define LEN 256

int main ()
{
FILE * fp;
double i=0; //sample count
double y=0; //y values
double f=0; //frequency
double t=0; //time

/* open the file for writing*/
fp = fopen ("1.dat","w");
//fprintf(fp, "sample #\ty-value\n");

printf("Enter the frequency in hertz: ");
scanf("%lg", &f);
printf("Enter the number of samples : ");
scanf("%lg", &t);



/* write 1 seconds of time data into the file stream*/
for(i = 0; i < t;i++){

//y=sin(i*M_PI/180);
y=sin(2*M_PI*f*t);

// fprintf (fp, "%g %g\n",i ,y);
fprintf (fp, "%g\n",y);
}

/* close the file*/
fclose (fp);
return 0;
}

最佳答案

我想我明白你真正想做的事情:

你的控制变量既不是 i 也不是 t,而是 i/t,它是一秒内 t 个样本中的第 i 个样本:

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

#define LEN 256

int main ()
{
FILE * fp;
unsigned int i=0; //sample count
double y=0; //y values
double f=0; //frequency
unsigned int t=0; //time

/* open the file for writing*/
fp = fopen ("1.dat","w");

printf("Enter the frequency in hertz: ");
scanf("%lg", &f);
printf("Enter the number of samples : ");
scanf("%u", &t);
/* write 1 seconds of time data into the file stream*/
for(i = 0; i < t;i++){
y=sin(2*M_PI*f*i/t);
fprintf (fp, "%g\n",y);
}

/* close the file*/
fclose (fp);
return 0;
}

关于c - 如何迭代这个 sin 方程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55927873/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com