我已经将 f(x) 的值存储在数组中,但是如何在 C 中从数组中打印某个元素 f(x) 的 x 值?

I have stored the values of f(x) in an array, but how do i print the value of x of a certain element f(x) from the array in C?

提问人:Bon 提问时间:10/13/2023 更新时间:10/13/2023 访问量:52

问:

对于家庭作业问题,我们被要求以数字方式找到二次方程 ax^2 + bx + c 的估计根(其中 a、b、c 由用户给出),我们不能使用 -b 公式,我们必须使用简单的基于搜索的算法进行分配。因此,我们必须在 x 的大范围内找到 f(x),然后打印 x 的值,使 f(x) 最接近于零。

我将 f(x) 的值存储在一个数组中,并搜索最接近 0 的值。我想打印函数的根 x 的值,这导致了 f(x) 的值,但不知道如何,我只能从最接近零的数组中打印 f(x) 的值。

例如,如果我的 a、b 和 c 的值分别为 1、-1 和 -6,我希望我的输出显示 x = 3 作为估计根。 现在我显示的输出是 = 0。(f(x) 的值,我最接近零的元素)

这是我的代码: (如果代码格式不正确,也很抱歉,我还是使用 stackoverflow 的新手,非常感谢任何帮助)

/*==================================================================
* Systems header files
*==================================================================*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

/*==================================================================
 * Constant definitions
 *==================================================================*/
#define SIZE 50

/*==================================================================
 * Function definitions
 *==================================================================*/
float array_roots(const float [], int); /*my function where i will search for my root value*/

int main(void)
{
    float table[SIZE];   /* array to store the function values f(x) */
    float a, b, c, x;
    int i;


    printf("*********************************************************\n");
    printf("Welcome to the quadratic root estimator.\n");
    printf("This estimates the value of one root of\n");
    printf("f(x)=ax^2+bx+c.\n");
    printf("*********************************************************\n");
    
    printf("Enter the coefficients in the form \"a b c\"\n: ");
    scanf("%f %f %f",&a, &b, &c);

 /*populating array and calling function */
    for(i=0; i<SIZE; i++)
    {
        x = 0 + i*(0.5); /* the range of values of x im using are between 1 and 50*/
        table[i] = a*x*x + b*x + c;           /* to store the value of f(x) at the correct point in the array */
    }
     

    /* Prints out value from the array which is closest to zero
       But i want it to print out the root of the function, x which gave the value of f(x) closest to zero*/
    
      printf("There is a root at: x = %.3f\n", array_roots(table, SIZE));

    return(0);
}
/*//////////////////////////////////////////////*/
/*function outside of  main to find element in array closest to zero */
/*//////////////////////////////////////////////*/

float array_roots(const float table[], int length)
{
    int i;           /* index for loop over array*/
    float root;       /* 'running' root. This will eventually be the root element of the array */

    root = table[0];  /* At the beginning, assume that the first element is the root */

    /* Next, loop through the array. For each element encountered,
       if this element is closer to zero, then
       set the running root equal to this value */
    for(i=1; i<length; i++)
        if(table[i] == 0 || abs(0-table[i]) < abs(0-root))
            root = table[i];

    /* At this point, variable 'root' holds the correct root element */
    return(root);
}
数组 C 函数 二次

评论

2赞 tadman 10/13/2023
格式看起来不错。缺少的是一些调试信息。您认为问题发生在哪里?您是否在调试器中单步执行以确认它正在执行正确的数学运算?
1赞 Some programmer dude 10/13/2023
OT:除非你有非常具体的原因,否则不要用于浮点值和变量。用。floatdouble
1赞 Tom Karzes 10/13/2023
的值是数组索引的函数。具体来说,你的代码有 所以,找到数组索引,然后用来确定。xix = 0 + i*(0.5);iix
0赞 Some programmer dude 10/13/2023
OT:为什么加到零?0 + i*(0.5)
1赞 Weather Vane 10/13/2023
你应该使用而不是使用吗?(我不明白为什么..)fabs()abs()0-

答:

2赞 Carson 10/13/2023 #1

您有一个表达式,可以将数组索引转换为 x 值:ix = 0 + i*(0.5)

为了简化此操作,请添加一个函数来执行此转换

// Returns the x value used for the index
float index_to_x(int i) {
    return i * (0.5);
}

然后,您可以将此函数放入 main:

 /*populating array and calling function */
    for(i=0; i<SIZE; i++)
    {
        x = index_to_x(i); /* the range of values of x im using are between 1 and 50*/
        table[i] = a*x*x + b*x + c;           /* to store the value of f(x) at the correct point in the array */
    }

现在您拥有了该函数,您可以更新以使用它:array_roots


float array_roots(const float table[], int length)
{
    int i;           /* index for loop over array*/
    float root;       /* 'running' root. This will eventually be the root element of the array */
    int min_i = 0;   /* index of the minimum root */

    root = table[0];  /* At the beginning, assume that the first element is the root */

    /* Next, loop through the array. For each element encountered,
       if this element is closer to zero, then
       set the running root equal to this value */
    for(i=1; i<length; i++) {
        if(table[i] == 0 || abs(0-table[i]) < abs(0-root)) {
            root = table[i];
            min_i = i;
         }
    }

    printf("The x coordinate for the minimum root is %f\n", index_to_x(min_i));

    /* At this point, variable 'root' holds the correct root element */
    return(root);
}

正如你所看到的,这将打印出用于查找根的 x 坐标。