我可以通过同时索引 c 中的两个矩阵来将矩阵的值传递给另一个矩阵吗?

标签 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(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.

第一个函数让用户命名并定义矩阵的维度,然后从包含 10x10 整数的 .txt 文件填充该矩阵。

第二个函数显示矩阵,第三个函数计算迹线。

第四个函数要求用户选择一个 2x2 矩阵,它是原始矩阵的子集。 2x2 矩阵的内容必须存储在一个新的结构矩阵中,连同它的名称和大小。

我“认为”我所做的是询问用户从哪里开始子矩阵并将值存储在 k 和 l 处,然后我将这些值用作索引。

我认为我的问题发生在将这些值传递给新矩阵时,在嵌套的 for 循环中,我增加了 i 和 j 以索引新矩阵,并增加了 l 和 k 以索引我从中传递值的矩阵。

注意:我之前从未见过 2 个值在 for 循环中递增,所以我预计它不会按照我“认为”的方式执行,因为子矩阵的第 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”,因为它们是调试时的噩梦。

关于我可以通过同时索引 c 中的两个矩阵来将矩阵的值传递给另一个矩阵吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50528247/

相关文章:

c - VxWorks结构体操作如何保证原子性?

c - 为什么这个编译没有错误?

c++ - 好友功能看不到私有(private)成员

c - 函数参数中的数组名称的处理方式是否与本地声明的数组不同(自动)

c++ - 函数指针在C和C++中都立即返回吗?

c - C 中的套接字 : Proper way to close socket

c - 更新 MD5 哈希值?

python - 我可以将一个函数分配给一个变量以在另一个函数中使用吗?

function - 局部函数可以在 Lua 中替换\覆盖自身吗

c - 具有结构体和指针的算法