首页 > 解决方案 > C中的多维数组,带有一些math.h

问题描述

到目前为止,我设置数组的代码是这样的:

#include <stdio.h>

void printArray(float myArray[4][3]);

int main(void)
{

    };

    printArray(sides);

    return 0;
}

void printArray(float passedArray[4][3])
{
    printf("Side A\tSideB\tSide C\n");

    for (int x = 0; x < 4; x++)
    {
        for (int y = 0; y < 3; y++)
        {
            printf("%.3f \t", passedArray[x][y]);
        }
        printf("\n");
    }
}

如果我收到用户的输入,我还创建了一种方法来评估先前代码中的斜边:

#include <stdio.h>

#include <math.h>


double hypotenuse(double lengtha, double lengthb);


int main() // Start of main function
{
    double lengtha, lengthb; //storing variables for later use

    printf("Enter the length of side A: \n"); //Prompt user for input of A

    scanf("%lf", &lengtha); //Stores input from user

    printf("Enter the length of side B: \n\n"); // Prompt user for input of B

    scanf("%lf", &lengthb); //Stores input from user

    return 0; // terminate
} /* End function main */


double hypotenuse(double sidea, double sideb)
{
    return sqrt(pow(sidea, 2) + pow(sideb, 2));
} /* End function */

但是,我遇到的主要问题是,我不确定如何从我的第一个代码/数组中获取预存储的值,将它们放入等式中,然后将它们输出到表中的 c 侧。我知道有办法,但是因为 C 有点老,所以很难找到太多信息。任何建议或帮助将不胜感激!

标签: carraysmultidimensional-array

解决方案


如果我理解正确,您可以遍历数组的每一行并将函数调用的结果分配给最后一列:

for (int i = 0; i < 4; ++i) {
    array[i][2] = hypotenuse(array[i][0], array[i][1]);
}

推荐阅读