我可以通过在c中同时索引两个矩阵来将一个矩阵的值传递给另一个矩阵吗?
我正在编写一段使用结构和5个预定义函数的代码,在将选定索引处的输入二维数组的值传递到新的二维数组时遇到问题 我已经包括了我的函数和我遇到困难的函数代码我可以通过在c中同时索引两个矩阵来将一个矩阵的值传递给另一个矩阵吗?,c,function,pointers,multidimensional-array,C,Function,Pointers,Multidimensional Array,我正在编写一段使用结构和5个预定义函数的代码,在将选定索引处的输入二维数组的值传递到新的二维数组时遇到问题 我已经包括了我的函数和我遇到困难的函数代码 struct matrix { char name; int mValues[10][10]; int ncols; int nrows; }; void matrixInput(struct matrix *matA); void matrixDisplay(struct matrix matA); void matrixTrace(stru
struct matrix
{
char name;
int mValues[10][10];
int ncols;
int nrows;
};
void matrixInput(struct matrix *matA);
void matrixDisplay(struct matrix matA);
void matrixTrace(struct matrix matA, int *trace);
void matrixDeterminant(struct matrix m1, struct matrix *m2, int * determinant);
///function body
void matrixDeterminant(struct matrix m1, struct matrix *m2, int * determinant)
{
int i, j, k, l;
FILE* fin;
fin = fopen("marks.txt", "r");
if(((m1.nrows)>2))
{
printf("\n\nFinding the deterinamt now.\n");
if(fin!=NULL)
{
do
{
printf("Please assign a letter to name your matrix A - Z : ");
scanf(" %c", &((*m2).name));
}
while((((*m2).name)<'A') || ((*m2).name)>'Z');
do
{
printf("\n\nEnter the row where you want to start the 2x2 matrix.\nNumber needs to be between 1 and %d : ", ((m1).nrows-1));
scanf("%d", &k);
}
while((k) >= ((m1).nrows));
do
{
printf("\n\nEnter the column where you want to start you 2x2 mtrix.\nNumber needs to be between 1 and %d : ", ((m1).ncols-1));
scanf("%d", &l);
}
while((l) >= ((m1).ncols));
}
for(i=0; i<2; i++,k++)
{
printf("Row %d:\t", i+1);
for(j=0; j<2; j++,l++)
{
((*m2).mValues[i][j]) = ((m1).mValues[k-1][l-1]);
printf("%d\t",((*m2).mValues[i][j]));
}
printf("\n");
}
}
/// Input/Output
Please assign a letter to name your matrix A - Z : H
Please enter the number of rows in matrix H > 1 < 10 :8
Please enter the number of cols in matrix H > 1 < 10 :8
Matrix H has 8 rows and 8 columns and contains:
Row 1: 55 7 40 30 32 45 43 77
Row 2: 72 1 20 65 85 40 46 22
Row 3: 45 77 88 32 30 55 59 99
Row 4: 72 37 33 18 44 73 44 12
Row 5: 88 2 11 55 7 40 30 32
Row 6: 24 73 13 99 99 22 45 77
Row 7: 88 32 22 11 98 34 38 37
Row 8: 33 18 44 73 22 45 77 88
Trace of matrix H = 317
Finding the deterinamt now.
Please assign a letter to name your matrix A - Z : F
Enter the row where you want to start the 2x2 matrix.
Number needs to be between 1 and 7 : 3
Enter the column where you want to start you 2x2 mtrix.
Number needs to be between 1 and 7 : 3
Row 1: 88 32
Row 2: 44 73 // This has shifted 2 columns.
Process returned 0 (0x0) execution time : 14.807 s
Press ENTER to continue.
结构矩阵
{
字符名;
int m值[10][10];
int ncols;
int nrows;
};
无效矩阵输入(结构矩阵*matA);
无效矩阵显示(结构矩阵matA);
void matrixTrace(结构矩阵matA,int*trace);
无效矩阵确定式(结构矩阵m1,结构矩阵*m2,整数*行列式);
///功能体
无效矩阵确定符(结构矩阵m1,结构矩阵*m2,整数*行列式)
{
int i,j,k,l;
文件*fin;
fin=fopen(“marks.txt”、“r”);
如果((m1.nrows)>2))
{
printf(“\n\n现在正在查找deterinamt。\n”);
如果(fin!=NULL)
{
做
{
printf(“请指定一个字母来命名矩阵a-Z:”);
scanf(“%c”和(*m2.name));
}
而((*m2.name)'Z');
做
{
printf(“\n\n输入要开始2x2矩阵的行。\n数字必须介于1和%d之间:”,((m1).nrows-1));
scanf(“%d”和“&k”);
}
而((k)>=(m1.nrows));
做
{
printf(“\n\n输入要启动2x2 mtrix的列。\n数字必须介于1和%d之间:”,((m1.ncols-1));
scanf(“%d”和“&l”);
}
而((l)>=((m1.ncols));
}
对于(i=0;i1<10:8
矩阵H有8行8列,包含:
第一排:55740302454377
第2排:72 120 65 85 40 46 22
第3行:457788323555999
第4排:72 37 33 18 44 73 44 12
第5排:88 2 11 55 7 40 30 32
第6行:24731399229577
第7行:88 32 22 11 98 34 38 37
第8行:33 18 44 73 22 45 77 88
矩阵H=317的迹
现在就找到威慑力。
请指定一个字母来命名矩阵a-Z:F
输入要开始2x2矩阵的行。
数字必须介于1和7:3之间
输入要启动2x2 mtrix的列。
数字必须介于1和7:3之间
第1行:88 32
第2行:44 73//这已经移动了2列。
进程返回0(0x0)执行时间:14.807秒
按ENTER键继续。
第一个函数允许用户名和定义矩阵的维度,然后从包含10x10整数的.txt文件填充矩阵
第二个函数显示矩阵,第三个函数计算轨迹
第四个函数要求用户选择一个2x2矩阵,它是原始矩阵的子集。2x2矩阵的内容必须存储在一个新的结构矩阵中,以及它的名称和大小
我“认为”我所做的是询问用户子矩阵的起始位置,并将值存储在k和l处,然后使用这些值作为索引
我认为我的问题在于将这些值传递给新矩阵,在嵌套的for循环中,我增加了I和j来索引新矩阵,增加了l和k来索引传递值的矩阵
注意:我以前从未见过for循环中有2个值递增,所以我认为它并没有像我“认为”的那个样做,因为子矩阵的第2行移动了2列
任何帮助都将不胜感激。当您退出第二个循环(
j
循环)时,您的索引l
应重置为初始值
在您的情况下,索引在执行期间是这样的:
loop i => i=0, k=3, j=0, l=3
loop j => i=0, k=3, j=0, l=3
loop j => i=0, k=3, j=1, l=4
exit loop j => i=0, k=3, j=2, l=5
loop i => i=1, k=4, j=2, l=5
so on...
因此,当您退出第二个循环时,您的索引已损坏!当您退出第二个循环(
j
循环)时,您的索引l
应重置为初始值
在您的情况下,索引在执行期间是这样的:
loop i => i=0, k=3, j=0, l=3
loop j => i=0, k=3, j=0, l=3
loop j => i=0, k=3, j=1, l=4
exit loop j => i=0, k=3, j=2, l=5
loop i => i=1, k=4, j=2, l=5
so on...
因此,当您退出第二个循环时,索引已损坏!最内层循环中递增的l是罪魁祸首
对于(j=0;j来说,在最内层循环中增加的l是罪魁祸首
for(j=0;jOff主题:小写“L”变量名不是最令人高兴的选择。它们太容易与数字1混淆。如果你不本能地避免这种选择,你将在将来为自己设置一个困难的调试会话。@JohnColeman谢谢你的建议,我会记住继续前进。
struct matrix m1,struct matrix*m2
似乎不太可能istent。为什么其中一个参数是指针而另一个不是?@johncolman我相信问题在于我在嵌套for循环中传递值的方式,子矩阵的第2行是+2列,我试图从中获取它。我包括了函数和函数体,试图尽可能具体,我对编程不是很有经验如果我的问题模棱两可,我深表歉意。@johncolman函数是预先确定的,所以我别无选择,只能按声明的方式使用它们。离题:小写“L”变量名不是最令人高兴的选择。它们太容易与数字1混淆。如果你不本能地避免这种选择,你将在将来为自己设置一个困难的调试会话。@JohnColeman谢谢你的建议,我会记住继续前进。struct matrix m1,struct matrix*m2
似乎不太可能istent。为什么其中一个参数是指针而另一个不是?@johncolman我相信问题在于我在嵌套for循环中传递值的方式,子矩阵的第2行是+2列,我试图从中获取它。我包括了函数和函数体,试图尽可能具体,我对编程不是很有经验如果我的问题模棱两可,我深表歉意。@johncleman函数是预先确定的,所以我别无选择,只能按声明的方式使用它们。哇!这解决了我的噩梦,给我带来了新的想法,非常感谢:)在另一个