为什么我的代码跳过整个for循环?

时间:2016-02-22 20:51:56

标签: c

我还在学习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");
    }

1 个答案:

答案 0 :(得分:2)

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