C 我该如何确保这两个矩阵具有非零、正的行和列值?

C 我该如何确保这两个矩阵具有非零、正的行和列值?,c,matrix,C,Matrix,我在设置代码时没有考虑到需要检测的事实,因此我现在似乎不知道如何实现它。这是一个简单的矩阵乘法器 基本上,它需要检测两种情况 如果矩阵1中的列与矩阵2中的行不相等,则退出程序并显示错误消息。我已经这样做了 如果用户为matrix1和matrix2的行值和列值输入的值为非零值,则为正值。我在这方面遇到了麻烦 我们的教授设置它的方式是,我们复制并粘贴一组值,它就这样做了。但是如果我们复制并粘贴到输入中,我不知道它应该如何检测行和列的错误,而为了接受我用于for循环的其余数据来扫描其中的所有值,for

我在设置代码时没有考虑到需要检测的事实,因此我现在似乎不知道如何实现它。这是一个简单的矩阵乘法器

基本上,它需要检测两种情况

  • 如果矩阵1中的列与矩阵2中的行不相等,则退出程序并显示错误消息。我已经这样做了

  • 如果用户为matrix1和matrix2的行值和列值输入的值为非零值,则为正值。我在这方面遇到了麻烦

  • 我们的教授设置它的方式是,我们复制并粘贴一组值,它就这样做了。但是如果我们复制并粘贴到输入中,我不知道它应该如何检测行和列的错误,而为了接受我用于for循环的其余数据来扫描其中的所有值,for循环的长度取决于matrix1和matrix2的行和列的输入。如果这些都是负数,for循环无论如何都不会工作

    真的,任何帮助都将不胜感激,除了这个小细节,我已经完成了所有其他部分。非常感谢

    #include <stdio.h>
    
    int main(int argc, char *argv[]) {
        int rows1 = 1, columns1 = 1, rows2 = 1, columns2 = 1;    // variables for number of rows and columns in each matrix 
        int i, j, k;                                             // loop variables 
    
        // These will affect the loop's length
        scanf("%d %d", &rows1, &columns1);
        int matrix1[rows1][columns1];
        for (i = 0; i < rows1; i++) {
            for (j = 0; j < columns1; j++) {
                scanf("%d", &matrix1[i][j]);
            }
        }
    
        scanf("%d %d", &rows2, &columns2);
        int matrix2[rows2][columns2];
        for (i = 0; i < rows2; i++) {
            for (j = 0; j < columns2; j++) {
                scanf("%d", &matrix2[i][j]);
            }
        }
    
        int resultMatrix[rows1][columns2]; // Initialization of resultMatrix, that holds the result of the multiplication
    
        int matrix1RowIndex = 0, matrix1ColIndex = 0, matrix2ColIndex = 0;
    
        if (columns1 != rows2) {
            printf("Sorry, the amount of columns in the first matrix must equal the amount of rows in the second matrix.\n");
        }
        else {      
            // Loop to set the values of resultMatrix's indices
            for (i = 0; i < rows1; i++) { // rows
                for (j = 0; j < columns2; j++) { // columns
                    resultMatrix[i][j] = 0;
                    for (k = 0; k < columns1; k++) { // for each individual index to be summed for the resultMatrix's value
                        resultMatrix[i][j] += (matrix1[matrix1RowIndex][matrix1ColIndex + k] * matrix2[matrix1ColIndex + k][matrix2ColIndex]);
                    }
                    matrix2ColIndex++;
                }
                matrix1RowIndex++;
                matrix2ColIndex = 0;
            }
    
            //prints resultMatrix
            for (i = 0; i < rows1; i++) { // rows
                for (j = 0; j < columns2; j++) { // columns
                    printf("%6d", resultMatrix[i][j]);
                }
                printf("\n");
            }
        }
    }
    
    #包括
    int main(int argc,char*argv[]){
    int rows1=1,columns1=1,rows2=1,columns2=1;//每个矩阵中行数和列数的变量
    int i,j,k;//循环变量
    //这些将影响循环的长度
    scanf(“%d%d”、&rows1、&columns1);
    int matrix1[rows1][columns1];
    对于(i=0;i
    您似乎可以使用if语句检查所讨论的值是否大于零,对吗?

    不,我不能,因为我上面概述的原因。这根本行不通,我不认为这是真的。读取一个值后,您可以简单地对其应用一个条件。我会为您演示代码,但这被标记为“家庭作业”,我已经在scanf函数之后直接运行了多次代码,但它仍然不起作用。不过还是要谢谢你!:如果你不告诉我们你试过的代码,我们就无法知道你做错了什么。