Write C++ Program to check whether two matrices are equal or not


I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #define size 2 // Matrix size declaration
  5.  
  6. int main()
  7. {
  8. int A[size][size];
  9. int B[size][size];
  10.  
  11. int row, col, isEqual;
  12.  
  13. // Input elements in first matrix from user
  14. cout<<"Enter elements in matrix A of size "<<size<<" x "<<size<<"\n";
  15. for(row=0; row<size; row++)
  16. {
  17. for(col=0; col<size; col++)
  18. {
  19. cin>>A[row][col];
  20. }
  21. }
  22.  
  23. // Input elements in second matrix from user
  24. cout<<"Enter elements in matrix B of size"<<size<<" x "<<size<<"\n";
  25. for(row=0; row<size; row++)
  26. {
  27. for(col=0; col<size; col++)
  28. {
  29. cin>>B[row][col];
  30. }
  31. }
  32.  
  33. // Assumes that the matrices are equal
  34. isEqual = 1;
  35.  
  36. for(row=0; row<size; row++)
  37. {
  38. for(col=0; col<size; col++)
  39. {
  40.  
  41. //If the corresponding entries of matrices are not equal
  42.  
  43. if(A[row][col] != B[row][col])
  44. {
  45. isEqual = 0;
  46. break;
  47. }
  48. }
  49. }
  50.  
  51. /*
  52.   * Checks the value of isEqual
  53.   * As per our assumption if isEqual contains 1 means both are equal
  54.   * If it contains 0 means both are not equal
  55.   */
  56. if(isEqual == 1)
  57. {
  58. cout<<"\nMatrix A is equal to Matrix B";
  59. }
  60. else
  61. {
  62. cout<<"\nMatrix A is not equal to Matrix B";
  63. }
  64.  
  65. return 0;
  66. }
  67.  

Result

C++ language techstudy.org

     





Related Topics




Popular Posts