我试图生成正弦系列的术语并总结它们的值。但我面临错误。因为我们有 alternate + 和-terms 我有两个 for 循环。接受值后,代码停止工作。问题是用于生成术语的 for 循环。作为数组部分工作正常。
#include <stdio.h>
#include <math.h>
int main() {
int i, number, j, x, value, value1, sign = 0;
float sum, sub, res, term = 0;
int fact = 1;
printf("Enter the number of terms:\n");
scanf("%d", & number);
int a[(number)];
//Storing odd numbers in array for terms//
for (i = 0; i <= (2 * number); i++) {
if ((i % 2) != 0) {
a[j] = i;
j++;
}
}
printf("Enter the value for x:");
scanf("%d", & x);
//Generating term//
for (j = 0; j <= number;) {
a[j] = value;
value = value1;
//Finding factorial//
while (value > 1) {
fact = fact * value;
value = value - 1;
}
term = (pow(value1, j) / fact);
sum = sum + term;
j = j + 2;
}
for (j = 1; j <= number;) {
a[j] = value;
value = value1;
//Finding factorial//
while (value > 1) {
fact = fact * value;
value = value - 1;
}
term = (pow(value1, j) / fact);
sub = sub - term;
j = j + 2;
}
res = sum - sub;
printf("%f", res);
return 0;
}
变量value1
在没有初始化的情况下使用。此外,它看起来像是要更新的,但它永远不会在程序中的任何地方分配。
在两种情况下,value = value1;
实际上应该是value1 = value;
吗?
此外,value
、sum
和sub
在首次使用之前不会初始化为零。要在声明中初始化这些变量,您应该为每个变量添加= 0
。
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(20条)