দুটি ম্যাট্রিক্স ইডেন্টিক্যাল কিনা চেক করতে প্রথমে ডাইমেনশন মিলিয়ে নিন। তুলনার জন্য রো এবং কলাম সমান হতে হবে।
if (row1 != row2 && col1 != col2) {
Console.Write("Matrices can't be compared:\n");
}
এলস ব্লকে এলিমেন্ট বাই এলিমেন্ট তুলনা করুন একটি ফ্ল্যাগ ব্যবহার করে:
else {
Console.Write("Comparison of Matrices: \n");
for (i = 0; i < row1; i++) {
for (j = 0; j < col2; j++) {
if (arr1[i, j] != arr2[i, j]) {
flag = 0;
break;
}
}
}
if (flag == 1)
Console.Write("Our matrices are equal!\n\n");
else
Console.Write("Our matrices are not equal!");
}
সম্পূর্ণ উদাহরণ
using System;
namespace Demo {
public class ApplicationOne {
public static void Main() {
int[,] arr1 = new int[10, 10];
int[,] arr2 = new int[10, 10];
int flag = 1;
int i, j, row1, col1, row2, col2;
Console.Write("Rows in the 1st matrix: ");
row1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Columns in the 1st matrix: ");
col1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Rows in the 2nd matrix: ");
row2 = Convert.ToInt32(Console.ReadLine());
Console.Write("Columns in the 2nd matrix: ");
col2 = Convert.ToInt32(Console.ReadLine());
Console.Write("Elements in the first matrix:\n");
for (i = 0; i < row1; i++) {
for (j = 0; j < col1; j++) {
Console.Write("element - [{0}],[{1}] : ", i, j);
arr1[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.Write("Elements in the second matrix:\n");
for (i = 0; i < row2; i++) {
for (j = 0; j < col2; j++) {
Console.Write("element - [{0}],[{1}] : ", i, j);
arr2[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
// ম্যাট্রিক্স প্রিন্ট এবং তুলনা লজিক (উপরোক্তরূপ)
if (row1 != row2 || col1 != col2) { // লজিক সংশোধন: || ব্যবহার করা উচিত
Console.Write("Matrices can't be compared:\n");
} else {
// তুলনা লজিক...
}
}
}
}
মূল কোডে লজিক্যাল ত্রুটি ছিল: && به জায়গায় || হওয়া উচিত। সংশোধন করা হয়েছে।