小编Sat*_*aty的帖子

使用Heron公式计算C中的平方根

我已经实现了这个功能:

double heron(double a)
{
    double x = (a + 1) / 2;
    while (x * x - a > 0.000001) {
        x = 0.5 * (x + a / x);
    }
    return x;
}
Run Code Online (Sandbox Code Playgroud)

该功能按预期工作,但是我希望对其进行改进。它应该使用不间断的while循环来检查是否类似于x * xis aa是用户应输入的数字。

到目前为止,我没有使用该方法的工作功能...这是我惨败的尝试:

double heron(double a)
{
    double x = (a + 1) / 2;
    while (x * x != a) {
        x = 0.5 * (x + a / x);
    }
    return x;
}
Run Code Online (Sandbox Code Playgroud)

这是我的第一篇文章,因此,如果有任何不清楚或需要补充的内容,请告诉我。

尝试失败次数2:

double …
Run Code Online (Sandbox Code Playgroud)

c square-root

4
推荐指数
1
解决办法
388
查看次数

如何在C中按值"删除"数组中的每个元素

我一直试图解决这个问题大约5天..找不到任何解决方案请发送帮助.我应该实现一个函数来按值"删除"数组中的每个元素.假设我的数组是"Hello",我想删除每个"l".到目前为止,我只能删除一次.顺便说一下,请记住我不允许使用指针来实现这个功能......(我们学校还没有学到这一点)这是我的代码:

#include <stdio.h>
#include <string.h>

void strdel(char array[], char c);

int main(void)
{
    char source[40];
    printf("\nStrdel test: ");
    strcpy(source, "Hello");
    printf("\nsource = %s", source);
    strdel(source, 'l');
    printf("\nStrdel: new source = %s", source);
    return 0;
}

void strdel(char array[], char c)
{
    int string_lenght;
    int i;
    for (string_lenght = 0; array[string_lenght] != '\0'; string_lenght++) {} 

    for (i = 0; i < string_lenght; i++) {
        if (array[i] == c) {
            for (i = i; array[i] != '\0'; ++i)
                array[i] = array[i + …
Run Code Online (Sandbox Code Playgroud)

c arrays for-loop while-loop

0
推荐指数
1
解决办法
108
查看次数

标签 统计

c ×2

arrays ×1

for-loop ×1

square-root ×1

while-loop ×1