对于我的编程类,我们必须创建一个调用子函数的程序(保存为.h)。 我能够用简单的函数做到这一点,但是当传递2维数组(更高)时,它变得很难。 我已经尝试了多次,并没有运气。如何将矩阵传递给.h函数并返回浮点数?
这里是我的主函数调用段:
switch (selection)
{
case 1:
determinant = det(m[0],n[0],Mat_1);
printf("\nDeterminant of Matrix 1 is: %f\n",determinant);
break;
,这里是我的.h文件中找到一个2×2矩阵的行列式:
float det(int a, int b, float Mat[a][b])
{
printf("%f\n%f\n%f\n%f\n",Mat[0][0],Mat[0][1],Mat[1][0],Mat[1][1]);
float det;
det = ((Mat[0][0]*Mat[1][1])-(Mat[0][1]*Mat[1][0]));
return det;
}
基本上我是聚集子函数(det.h)似乎没有收到2维数组。
我知道这有点冗长,但这里是我的整个代码.c(主)文件。
非常感谢您的时间。
最亲切的问候, 汤姆
#include <stdio.h>
#include "det.h"
int main(void)
{
int m[3],n[3],number,i,j,selection;
float determinant;
printf("Enter number of matrices you wish to deal with:");
scanf("%d",&number);
while ((number<1.0)||(number>2))
{
printf("1 or 2 matrices only please.\n");
scanf("%d",&number);
}
for (i=0;i<number;i++)
{
printf("Enter dimensions (m x n) of Matrix [%d]:\n",i+1);
scanf("%d %d",&m[i],&n[i]);
}
float Mat_1[m[0]][n[0]],Mat_2[m[1]][n[1]];
for (i=0;i<m[0];i++)
{
for (j=0;j<n[0];j++)
{
printf("Enter Data for Matrix 1 [%d][%d]:",i+1,j+1);
scanf("%d",&Mat_1[i][j]);
}
}
if (number=2)
for (i=0;i<m[1];i++)
{
for (j=0;j<n[1];j++)
{
printf("Enter Data for Matrix 2 [%d][%d]:",i+1,j+1);
scanf("%d",&Mat_2[i][j]);
}
}
printf("\nSELECT ONE OF THE FOLLOWING:\n1. Determinant\n2. Addition\n3. Subtraction\n4. Multiply\n5. Transpose\n6. Scale\n7. Print");
scanf("%d",&selection);
while ((selection<1)||(selection>7))
{
printf("Invalid selectoin, try again (1-7):");
scanf("%d",&selection);
}
switch (selection)
{
case 1:
determinant = det(m[0],n[0],Mat_1);
printf("\nDeterminant of Matrix 1 is: %f\n",determinant);
break;
default:
printf("\nAn Error has occured.\n");
}
system("pause");
return 0;
}
OMG,你在'.h'中有函数_definition_?迟早你会遇到一些麻烦。 –
我应该如何存储单独的功能? 我们打算将它们保存为单独的文件并对它们进行调用。 –
你创建单独的源文件'.c'并编译并链接在一起。那是最好的出路。 –