Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        3年前关闭。
                                                                                            
                
        
我仍在学习C的基础知识,并且分配了编码一个程序,以显示用户输入的两个2D数组的乘积。这个想法是不使用像malloc这样的内存功能。

代码工作正常,但是当我添加一条if语句以检查运行时第一个数组的行与下一个数组的列相等时,将跳过用于输入第一个数组的数据并显示它的整个部分它直接去要求下一个数组。我试图清理缓冲区并使用getch()试图强制编译器执行该部分,但它一直被跳过。

这是整个代码:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>
int main(void);
int main (void)
#define MAXR 36
#define MAXC 18
{
    int Rows_A,Columns_A,Rows_B,Columns_B,Rows_C,Columns_C;
int Counter_Rows=0,Counter_Columns=0,Counter_Multiplier=0;
int Matrix_A[MAXR][MAXC],Matrix_B[MAXR][MAXC],Results_Matrix_C[MAXR][MAXC];
printf("\n\tTo multiply two matrices both have to be the same size of rows from the first matrix and the columns from the next one. \n\tPlease enter the size of the matrices:\n");

printf("\n\tMatrix A:\n\tNumber of rows :\t\t");
scanf(" %d",&Rows_A);
printf("\n\tNumber of columns :\t\t");
scanf(" %d",&Columns_A);
printf("\n\tMatrix B:\n\tNumber of rows :\t\t");
fflush(stdin);
scanf(" %d",&Rows_B);
printf("\n\tNumber of columns :\t\t");
scanf(" %d",&Columns_B);
if (Rows_A=!Columns_B)
{
    printf("\n\tMatrix B number of columns and Matrix A number of rows are not the same therefore they cannot be multiplied.");
    return 0;
}
else
{
    Rows_C=Rows_A;
    Columns_C=Columns_B;
    printf("\n\tEnter the data for Matrix A:");
    for (Counter_Rows=0;Counter_Rows < Rows_A;Counter_Rows++)
    {
        for(Counter_Columns=0;Counter_Columns < Columns_A; Counter_Columns++)
        {
            printf("\n\tEnter the value of the position [%d][%d] of the matrix A:  ",Counter_Rows+1,Counter_Columns+1);
            scanf(" %d",&Matrix_A[Counter_Rows][Counter_Columns]);
        }
    }
    printf("\n\tMatrix A.\n\t");//Matrix A
    for (Counter_Rows= 0; Counter_Rows < Rows_A; Counter_Rows++)
    {
        for (Counter_Columns= 0; Counter_Columns < Columns_A; Counter_Columns++)
        {
            printf("[%d] ",Matrix_A[Counter_Rows][Counter_Columns]);
        }
        printf("\n\t");
    }
    fflush(stdin);
    printf("\n\tEnter the data for Matrix B:");
    for (Counter_Rows=0;Counter_Rows < Rows_B;Counter_Rows++)
    {
        for(Counter_Columns=0;Counter_Columns < Columns_B; Counter_Columns++)
        {
            printf("\n\tEnter the value of the position [%d][%d] of the matrix B:  ",Counter_Rows+1,Counter_Columns+1);
            scanf(" %d",&Matrix_B[Counter_Rows][Counter_Columns]);
        }
    }
    printf("\n\tMatrix B.\n\t");//Matrix B.
    for (Counter_Rows= 0; Counter_Rows < Rows_B; Counter_Rows++)
    {
        for (Counter_Columns= 0; Counter_Columns < Columns_B; Counter_Columns++)
        {
            printf("[%d] ",Matrix_B[Counter_Rows][Counter_Columns]);
        }
        printf("\n\t");
    }
    //Calculating product matrix C.
    for (Counter_Rows=0;Counter_Rows < Rows_C;Counter_Rows++)
    {
        for(Counter_Columns=0;Counter_Columns < Columns_C; Counter_Columns++)
        {
            //initializes the Matrix  C in 0's
            Results_Matrix_C[Counter_Rows][Counter_Columns]=0;
            for(Counter_Multiplier=0; Counter_Multiplier < Columns_A; Counter_Multiplier++)
            {
                Results_Matrix_C[Counter_Rows][Counter_Columns]=Results_Matrix_C[Counter_Rows][Counter_Columns]+(Matrix_A[Counter_Rows][Counter_Multiplier]*Matrix_B[Counter_Multiplier][Counter_Columns]);
            }
        }
    }
    printf("\n\tProduct Matrix C:\n\t");//Matrix B.
    for (Counter_Rows= 0; Counter_Rows < Rows_C; Counter_Rows++)
    {
        for (Counter_Columns= 0; Counter_Columns < Columns_C; Counter_Columns++)
        {
            printf("[%d] ",Results_Matrix_C[Counter_Rows][Counter_Columns]);
        }
        printf("\n\t");
    }
}
return 0;


}

这是被跳过的部分:

printf("\n\tEnter the data for Matrix A:");
    for (Counter_Rows=0;Counter_Rows < Rows_A;Counter_Rows++)
    {
        for(Counter_Columns=0;Counter_Columns < Columns_A; Counter_Columns++)
        {
            printf("\n\tEnter the value of the position [%d][%d] of the matrix A:  ",Counter_Rows+1,Counter_Columns+1);
            scanf(" %d",&Matrix_A[Counter_Rows][Counter_Columns]);
        }
    }
    printf("\n\tMatrix A.\n\t");//Matrix A
    for (Counter_Rows= 0; Counter_Rows < Rows_A; Counter_Rows++)
    {
        for (Counter_Columns= 0; Counter_Columns < Columns_A; Counter_Columns++)
        {
            printf("[%d] ",Matrix_A[Counter_Rows][Counter_Columns]);
        }
        printf("\n\t");
    }

最佳答案

您的示例中有if (Rows_A=!Columns_B)。我想你要if (Rows_A != Columns_B)

关于c - 为什么我的代码跳过整个for循环? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35563584/

10-14 08:08