使用C中的数组计算直角三角形的斜边

时间:2018-11-04 20:08:06

标签: c multidimensional-array

我在分配任务时遇到问题,希望有人能提供帮助。目标是使用多维数组让用户放入直角三角形的side1和side2来确定斜边。现在我被困在两点:

  • 如何使用变量设置数组
  • 如何将这些变量保存到数组中并处理方程式

我的代码当前如下所示:

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

double hypotenuse(double x, double y, double z);

int main(void)
{

    double side1, side2, side3, counter;
    side3 = 1;
    int table[4][3] =
    {
/* Column 0  1  2 */
        { 1, 2, 3 }, // Initializers for for indexed by 0 or row 1
        { 4, 5, 6 }, // Initializers for for indexed by 1 or row 2
        { 7, 8, 9 }, // Initializers for for indexed by 2 or row 3
        { 10, 11, 12 } // Initializers for for indexed by 3 or row 4
    };

    for (int i = 0; i < 4; i++);
    {
        for (int j = 0; j < 3; j++)
        {
            printf("Table [%d] [%d] = %d \n", i, j, table[i][j]);
        }
    }


    for (counter = 0; counter <= 2; counter++) {
        printf("Enter values for two sides: ");
        scanf("%lf %lf", &side1, &side2);

        printf("%.2f\n", hypotenuse(side1, side2, side3));
    }

    return 0;
}

double hypotenuse(double x, double y, double z) {
    x *= x;
    y *= y;
    z = sqrt(x + y);

    return z;
}

现在第26行有一个错误,我没有定义我,我不明白这不是因为第22行应该定义它,所以我想。

任何帮助将不胜感激! 〜CRob

1 个答案:

答案 0 :(得分:0)

以下建议的代码:

  1. 干净地编译
  2. 正确检查(并处理)错误
  3. 格式化以提高用户可读性
  4. 产生所需的功能

现在,建议的代码:

<!DOCTYPE html>
<html>

<head>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link href="Main.css" rel="stylesheet" />
    <title>Page Title</title>
</head>

<body>
    <nav class="navbar navbar-expand-lg navbar-light bg-light">
        <div class="container-fluid">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-collapse-main">
                    <span class="sr-only">TOGGLE NAVIGATION</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
            </div>
            <div class="collapse navbar-collapse" id="navbar-collapse-main">
                <ul class="nav navbar-nav navbar-right">
                    <li><a class="active" href="#">Home</li>
                    <li><a href="#">About</li>
                    <li><a href="#">Serviços</li>
                    <li><a href="#">Testemunhos</li>
                    <li><a href="#">Contato</li>
                </ul>
            </div>
        </div>
    </nav>
    <div id="home">
        <div class="landing-next">
            <h1>BOOTSTRAP</h1>
            <h3>Building my first webpage.</h3>
            <a href="#" class="btn btn-success">Get Started With a Webpage Today</a>
        </div>
    </div>
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
        crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49"
        crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy"
        crossorigin="anonymous"></script>
</body>




</html>

该程序的典型运行结果为:

#include <stdio.h>    // printf(), fprintf(), scanf()
#include <stdlib.h>   // exit(0, ESIT_FAILURE
//#include <string.h>
#include <math.h>     // sqrt()

#define ROWS 4
#define COLS 3
#define NUM_CALCULATIONS 3

double hypotenuse( double x, double y );

int main( void )
{

    double side1;
    double side2;

    int table[ ROWS ][ COLS ] =
    {
/* Column 0  1  2 */
        { 1, 2, 3 },    // Initializers for for indexed by 0 or row 1
        { 4, 5, 6 },    // Initializers for for indexed by 1 or row 2
        { 7, 8, 9 },    // Initializers for for indexed by 2 or row 3
        { 10, 11, 12 }  // Initializers for for indexed by 3 or row 4
    };

    // display table to user
    for (int i = 0; i < ROWS; i++)
    {
        for (int j = 0; j < COLS; j++)
        {
            printf("Table [%d] [%d] = %d \n", i, j, table[i][j]);
        }
        puts( "" );
    }

    // input triangle legs, then display hyp to user
    for ( size_t counter = 0; counter < NUM_CALCULATIONS; counter++) 
    {
        printf("Enter values for two sides: ");

        if( scanf( "%lf %lf", &side1, &side2 ) != 2 )
        {
            fprintf( stderr, "scanf failed to read the two user inputs, exiting\n" );
            exit( EXIT_FAILURE );
        }

        printf( "%.2f\n", hypotenuse( side1, side2 ) );
    }

    return 0;
}

double hypotenuse( double x, double y )
{
    x *= x;
    y *= y;
    return  sqrt(x + y);
}