简体   繁体   English

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

[英]Can I pass the values of a matrix to another matrix by simultaneously indexing both matrices in c?

I am writing a piece of code that uses a struct and 5 predefined functions, I am having problems passing the values of the input 2d array at a chosen index to a new 2d array. 我正在编写使用结构和5个预定义函数的代码,在将输入2d数组的值在选定索引处传递到新2d数组时遇到问题。

I have included my functions and the code for function I am having difficulties with. 我已经包含了我的函数和遇到困难的函数代码。

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.

The First function lets the user name and define the dimensions of the matrix which is then populated from a .txt file containing 10x10 integers. First函数让用户名和定义矩阵的尺寸,然后从包含10x10整数的.txt文件填充矩阵。

The second function displays the matrix and the third calculates the trace. 第二个函数显示矩阵,第三个函数计算轨迹。

The fourth function asks the user to choose a 2x2 matrix, which is a subset of the original matrix. 第四个功能要求用户选择2x2矩阵,该矩阵是原始矩阵的子集。 The 2x2 matrix contents must be stored in a new struct matrix, along with its name and size. 2x2矩阵内容及其名称和大小必须存储在新的结构矩阵中。

What I "think" I have done, is to ask the user where to start the sub-matrix and stored the values at k and l, I then use these values as an index. 我所做的“思考”是询问用户在哪里启动子矩阵并将值存储在k和l,然后将这些值用作索引。

Where I believe my problem occurs is in passing these vales to the new matrix, in the nested for loops I have incremented i and j to index the new matrix and l and k to index the matrix I am passing values from. 我认为发生问题的地方是将这些值传递给新矩阵,在嵌套的for循环中,我增加了i和j来索引新矩阵,并增加了l和k来索引我要从中传递值的矩阵。

NOTE: I have never seen 2 values being incremented in a for loop before so I expect it is not doing what I "think" it is doing as row 2 of the sub matrix has shifted 2 columns. 注意:我之前从未见过有2个值在for循环中递增,因此我希望它不会执行我“认为”的操作,因为子矩阵的第2行已移动2列。

Any help would be appreciated. 任何帮助,将不胜感激。

Your l that is incremented in the innermost loop is the culprit 在最内层循环中递增的l是元凶

for(j=0; j<2; j++,l++) for(j = 0; j <2; j ++,l ++)

you need to reinitialize it 您需要重新初始化

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;
        }

Because in the first row l is incremented twice, in the second row it's incremented twice again. 因为在第一行中l增加了两次,所以在第二行中l又增加了两次。

I'd advise you to avoid one letter variables and especially "l" as @JohnColeman said since they are a nightmare at debugging. 我建议您避免使用一个字母变量,尤其是@JohnColeman所说的“ l”,因为它们是调试的噩梦。

Your index l should be reset to the initial value when you exit the second loop ( j loop). 退出第二个循环( j循环)时,应将索引l重置为初始值。

In your case, this is what your indexes look like during execution: 就您而言,这就是执行期间索引的样子:

    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...

So your indexes are corrupted when you exit the second loop ! 因此,当您退出第二个循环时,您的索引已损坏!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM