gpt4 book ai didi

无法读取 C 中 for 循环的第一次迭代

转载 作者:太空宇宙 更新时间:2023-11-04 00:28:29 25 4
gpt4 key购买 nike

我正在尝试创建一个能够计算拉格朗日多项式的程序,但我遇到了一个可能是微不足道的问题。我得到了一些 x 和 y 值,我应该使用这些值来近似某个其他 x 的函数。变量 nodes 指的是我给出的 x 和 y 值对的数量。

我无法读取 x 值的第一次迭代,它会直接跳到读取 y 值。即它只打印 x(0),然后打印 y(0),而不让我为 x(0) 输入任何内容。这对于第一个循环之后的任何循环都不是问题。任何帮助,将不胜感激。

#include "stdio.h"
#include "math.h"
#define SIZE 40

int main(){

// Defining variables and arrays
int i, j, nodes;
float nodex[SIZE], nodey[SIZE], appx;

// Find how many nodes there are
printf("How many nodes are being referenced?\n");
scanf("%d", &nodes);

// Find what number we are approximating for x
printf("For what value of x are we approximating?\n");
scanf("%d", &appx);

for(i=0 ; i < nodes ; i++)
{
printf("\nEnter x(%d)", i);
scanf("%f", &nodex[i]);
printf("\nEnter y(%d)", i);
scanf("%f", &nodey[i]);
}
}

最佳答案

如评论中所述:

You're using %d with appx, instead of %f. The %d stops reading at the decimal point, and the &nodex[0] input resumes where the %d left off — at the decimal point. The value in appx is garbage too, of course.

You should test the return value from scanf(); it should be 1 for each call you show. You should print the values read so you know that what was read matches what you expected.

一些固定代码:

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

#define SIZE 40

static void err_exit(const char *msg)
{
fprintf(stderr, "%s\n", msg);
exit(EXIT_FAILURE);
}

int main(void)
{
// Defining variables and arrays
int nodes;
float nodex[SIZE], nodey[SIZE], appx;

// Find how many nodes there are
printf("How many nodes are being referenced?: ");
fflush(stdout);
if (scanf("%d", &nodes) != 1)
err_exit("failed to read number of nodes");
if (nodes < 3 || nodes > SIZE)
err_exit("number of nodes not in range 0..40");

// Find what number we are approximating for x
printf("For what value of x are we approximating?: ");
fflush(stdout);
if (scanf("%f", &appx) != 1)
err_exit("failed to read value");

for (int i = 0; i < nodes; i++)
{
printf("Enter x(%d): ", i);
fflush(stdout);
if (scanf("%f", &nodex[i]) != 1)
err_exit("failed to read x-value");
printf("Enter y(%d): ", i);
fflush(stdout);
if (scanf("%f", &nodey[i]) != 1)
err_exit("failed to read y-value");
}

printf("Approximating: %g\n", appx);
printf("%d nodes:\n", nodes);
for (int i = 0; i < nodes; i++)
printf("%2d (%g,%g)\n", i, nodex[i], nodey[i]);

return 0;
}

我假设您支持 C99。如果没有,您需要申报 int i;在循环外使用 for (i = 0; i < nodes; i++)作为循环。

编译:

gcc -O3 -g -std=c11 -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes \
read-float-83.c -o read-float-83

样本运行:

$ ./read-float-83
How many nodes are being referenced?: 5
For what value of x are we approximating?: 3.67
Enter x(0): 1.23
Enter y(0): 2.95
Enter x(1): 1.98
Enter y(1): 3.46
Enter x(2): 2.47
Enter y(2): 4.51
Enter x(3): 3.02
Enter y(3): 2.87
Enter x(4): 4.18
Enter y(4): -1.96
Approximating: 3.67
5 nodes:
0 (1.23,2.95)
1 (1.98,3.46)
2 (2.47,4.51)
3 (3.02,2.87)
4 (4.18,-1.96)
$

关于无法读取 C 中 for 循环的第一次迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49089173/

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