c - 在c中填充斜边表

标签 c

我是编程新手,有些概念对我来说还是很难理解,所以我在这里寻求你的帮助。以下代码通过使用 A 边和 B 边的随机值填充数组表来计算直角三角形的斜边。

我想在此代码中修改的一件事是不要让它用随机值填充表;我希望能够输入我的特定值。如何才能做到这一点?

提前致谢。

#include <stdio.h>   //header for input/ output
#include <math.h>    //header for sqrt() and pow() 

double calcHypotenuse(double a, double b)
{
    double a_square = pow(a, 2);
    double b_square = pow(b, 2);
    return sqrt(a_square + b_square);
}

//Print the table of a,b,c with headings  

void printTable(double table[4][3])
{
    int i;

    // display the header columns
    printf("%19s%21s%21s\n", "Value for Side A", "Value for Side B", 
"Hypotenuse");

// display the data rows
for (i = 0; i < 4; i++)
{
    printf("%15.3f%21.3f%23.3f\n", table[i][0], table[i][1], table[i][2]);
    }

}

int main()
{
double array[4][3];   //two dimensional array to store values of a, b and Hypotenuse 
double a, b, c;      //variables  of side A, B, and C 
int i;             // loop index variable

//populate the table with rand values for a and b 
for (i = 0; i < 4; i++)
{
    a = 2 + rand() % 16;
    b = 3 + rand() % 10;
    array[i][0] = a;
    array[i][1] = b;
    array[i][2] = 0;
}

// print the array table 
printf("\nInput Table:\n");
printTable(array);

//populate the hypotenuse values in the array table 
for (i = 0; i < 4; i++)
{
    a = array[i][0];
    b = array[i][1];
    c = calcHypotenuse(a, b);
    array[i][2] = c;
}

// print the results 
printf("\nOutput Table:\n");
printTable(array);

return 0;
}

同样,预期的结果只是让我能够输入 A 面和 B 面的值,并将计算结果显示在输出表中。

最佳答案

对于这种简单的情况,使用 scanf 从终端获取输入并将其存储在代码变量中真的很容易。我修改了一些您的代码以提供任意输入的功能。

#include <stdio.h>   //header for input/ output
#include <stdlib.h>
#include <math.h>    //header for sqrt() and pow() 

#define MAX_ARRAY_SIZE 100

double calcHypotenuse(double a, double b)
{
    double a_square = pow(a, 2);
    double b_square = pow(b, 2);
    return sqrt(a_square + b_square);
}

//Print the table of a,b,c with headings  

void printTable(double table[MAX_ARRAY_SIZE][3],  int n)
{
    int i;

    // display the header columns
    printf("%19s%21s%21s\n", "Value for Side A", "Value for Side B", 
"Hypotenuse");

    // display the data rows
    for (i = 0; i < n; i++)
        printf("%15.3f%21.3f%23.3f\n", table[i][0], table[i][1], table[i][2]);

}

int main()
{

double array[MAX_ARRAY_SIZE][3];   //two dimensional array to store values of a, b and Hypotenuse 
double a, b, c;      //variables  of side A, B, and C 
int i;             // loop index variable

int n;
printf("Input Number of Triangles ");
scanf("%d", &n);

printf("Parsing Input Table\n");
for (i = 0; i < n; i++)
{
    scanf ("%lf", &a);
    scanf ("%lf", &b);

    array[i][0] = a;
    array[i][1] = b;
    array[i][2] = calcHypotenuse(a, b);;
}

// print the results 
printf("\nOutput Table:\n");
printTable(array, n);

return 0;
}

运行代码时,系统会提示您先输入三角形的数量,然后一一输入每条边的长度。

但是在实践中,生成输入文件并使用 scanf 非常方便,您可以立即将输入文件发送到程序,而不必一直一一输入您的值。

例如,在我的例子中,我生成了这个输入文件 input.txt:

6
1 2
3 4
5 6
3 4
7 8
10 11

在 linux 上(也可能在 mingw 上)你可以按如下方式将它传递给程序

a.out < input.txt

它会生成以下输出:

Output Table:
   Value for Side A     Value for Side B           Hypotenuse
          1.000                2.000                  2.236
          3.000                4.000                  5.000
          5.000                6.000                  7.810
          3.000                4.000                  5.000
          7.000                8.000                 10.630
         10.000               11.000                 14.866

只是一个一般性的注意事项:使用 scanf 时要非常小心,因为它可能会导致未定义的行为。在继续计算之前尝试验证输入值。

关于c - 在c中填充斜边表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54138777/

相关文章:

c - 对于函数指针 "fptr",为什么 "fptr"和*fptr的值相同?*fptr到底是什么意思?我只知道(*fptr)()或fptr()

arrays - 在 C 中使用第一个数组元素作为数组长度是一种很好的编程习惯吗?

c - 从C调用带有函数参数的Rust函数会导致SegFault

c - 在 C 语言中,为什么多重声明对全局变量有效但对局部变量无效?

c - 使用#ifndef#define #endif 处理 C 头文件的最佳实践

c - typedef __u32 __bitwise __be32 在 linux 中的意思

c - 程序无法按预期工作

c - 反向打印链表(或反向填充?)

c++ - 为什么 imread() 总是返回空数据?

c - undefined symbol : PyExc_ImportError when embedding Python in C