c - 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 有点老,所以很难找到太多信息。任何建议或帮助将不胜感激!
解决方案
如果我理解正确,您可以遍历数组的每一行并将函数调用的结果分配给最后一列:
for (int i = 0; i < 4; ++i) {
array[i][2] = hypotenuse(array[i][0], array[i][1]);
}
推荐阅读
- java - 如何使用 Mockito 模拟构造函数对象属性?
- kdb - kdb/q -- 如何按某些分组对行进行编号
- arrays - 以角度创建数组对象的数组
- python - 如何将下拉列表中的值传递到烧瓶中的另一个页面
- git - 在 git 中搜索与给定文件路径匹配的最后提交的文件
- javascript - 在继续下一次迭代之前等待
- cloud - 阿里云如何定义私有区域id
- python - Python PyQt5 如何使用 QWidget 显示完整的 QMenuBar
- node.js - 使用 Firebase Cloud Functions 时 Nodemailer 查询 A EREFUSED localhost
- amazon-web-services - Databricks 工作人员和 Elasticsearch 节点是否需要位于 AWS 的同一 VPC 中?