C程序 检查两个矩阵是否相等

C程序 检查两个矩阵是否相等

在这里,我们将看到如何使用C语言程序来检查两个矩阵是否相等

输入:

First Matrix:   
1, 2, 3, 4
1, 2, 3, 4
1, 2, 3, 4
1, 2, 3, 4

Second matrix:
1, 2, 3, 4
1, 2, 3, 4
1, 2, 3, 4
1, 2, 3, 4

输出:

Matrices are equal.

步骤:

要使任何两个矩阵相等,两个矩阵的行数和列数应该相等,相应的元素也相等。

示例:

// C Program to check if two matrices are equals or not
#include <stdio.h>
#define N 4 // Macros
  
// This function returns 1 if A[][] and B[][] are equal
// otherwise it returns 0
int areSame(int A[][N], int B[][N])
{
    int i, j;
    for (i = 0; i < N; i++)
        for (j = 0; j < N; j++)
            if (A[i][j] != B[i][j])
                return 0;
    return 1;
}
  
// driver code
int main()
{
  
    // create two matrices
    int A[N][N] = { { 1, 2, 3, 4 },
                    { 1, 2, 3, 4 },
                    { 1, 2, 3, 4 },
                    { 1, 2, 3, 4 } };
  
    int B[N][N] = { { 1, 2, 3, 4 },
                    { 1, 2, 3, 4 },
                    { 1, 2, 3, 4 },
                    { 1, 2, 3, 4 } };
  
    // Display first matrix
    printf("\n First Matrix \n");
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            printf(" %d", A[i][j]);
        }
        printf("\n");
    }
  
    // Display second matrix
    printf("\n Second Matrix \n");
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            printf(" %d", B[i][j]);
        }
        printf("\n");
    }
  
    // check if Matrices are equals or not
    // areSame(A, B) function accept two matrices as input
    // and return 1 if matrices are equals otherwise return
    // 0
    if (areSame(A, B))
        printf("\n Matrices are equal");
    else
        printf("\n Matrices are not equal");
    return 0;
}

输出

 First Matrix 
 1 2 3 4
 1 2 3 4
 1 2 3 4
 1 2 3 4

 Second Matrix 
 1 2 3 4
 1 2 3 4
 1 2 3 4
 1 2 3 4

 Matrices are equal

时间的复杂性: O(n2)。
辅助空间: O(1).

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

C语言 实例