c - 我可以通过同时在c中索引两个矩阵来将矩阵的值传递给另一个矩阵吗?
问题描述
我正在编写一段使用结构和 5 个预定义函数的代码,我在将所选索引处的输入 2d 数组的值传递给新的 2d 数组时遇到问题。
我已经包含了我的函数和我遇到困难的函数的代码。
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.
First 函数允许用户命名并定义矩阵的维度,然后从包含 10x10 整数的 .txt 文件中填充该矩阵。
第二个函数显示矩阵,第三个函数计算轨迹。
第四个函数要求用户选择一个 2x2 矩阵,它是原始矩阵的子集。2x2 矩阵的内容必须连同其名称和大小一起存储在一个新的结构矩阵中。
我“认为”我所做的是询问用户从哪里开始子矩阵并将值存储在 k 和 l 处,然后我将这些值用作索引。
我认为我的问题发生的地方是将这些值传递给新矩阵,在嵌套的 for 循环中,我增加了 i 和 j 来索引新矩阵,并增加 l 和 k 来索引我从中传递值的矩阵。
注意:我以前从未见过在 for 循环中增加 2 个值,所以我希望它没有做我“认为”它正在做的事情,因为子矩阵的第 2 行已经移动了 2 列。
任何帮助,将不胜感激。
解决方案
在最内层循环中递增的 l 是罪魁祸首
for(j=0; j<2; j++,l++)
你需要重新初始化它
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");
l=l-2;
}
因为在第一行中 l 增加了两次,在第二行中它又增加了两次。
我建议您避免使用一个字母变量,尤其是 @JohnColeman 所说的“l”,因为它们是调试的噩梦。
推荐阅读
- html - 为什么使用这个粘性页脚不起作用?
- php - Webhook 在使用 Stripe CLI 和事件触发器进行调试时测试不一致
- android - 如何将图像索引为二维数组(在 Android 上)?
- javascript - 在 JavaScript 中的单个按钮上使用 addEventListener 和 removeEventListener 两次
- javascript - 如何在运行嵌套的for循环javascript时最小化执行时间
- node.js - node.js 中的两个 http 调用
- javascript - 从 background.js 执行 content.js 脚本代码
- r - 在 R 中安装 FIELDimageR 包时出错
- laravel - 如何将变量从刀片文件传递到 laravel 中的控制器?
- activemq - ActiveMQ 缓存 LDAP 管理员无法阅读建议