要檢查矩陣是否相同,您需要先檢查矩陣是否可以比較,因為為了進行比較,至少兩個矩陣的維度應該相同。
if (row1 != row2 && col1 != col2) { Console.Write("Matrices can't be compared:"); }
現在,在 else 條件下檢查指標是否相同。我們也在這裡設定了一個標誌 -
if (row1 != row2 && col1 != col2) { Console.Write("Matrices can't be compared:"); } else { Console.Write("Comparison of Matrices: "); 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!"); 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:"); 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:"); 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()); } } Console.Write("Matrix 1:"); for (i = 0; i < row1; i++) { for (j = 0; j < col1; j++) Console.Write("{0} ", arr1[i, j]); Console.Write(""); } Console.Write("Matrix 2:"); for (i = 0; i < row2; i++) { for (j = 0; j < col2; j++) Console.Write("{0} ", arr2[i, j]); Console.Write(""); } if (row1 != row2 && col1 != col2) { Console.Write("Matrices can't be compared:"); } else { Console.Write("Comparison of Matrices: "); 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!"); else Console.Write("Our matrices are not equal!"); } } } }
輸出
Rows in the 1st matrix: Columns in the 1st matrix: Rows in the 2nd matrix: Columns in the 2nd matrix: Elements in the first matrix: Elements in the second matrix: Matrix 1: Matrix 2: Comparison of Matrices: Our matrices are equal!
以上是檢查兩個矩陣是否相同的 C# 程序的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本文解釋瞭如何使用printf中的\ n逃脫序列在C中創建新線字符並列出函數。 它詳細介紹了功能並提供了代碼示例,以說明其用於輸出中的線路斷裂。

本文探討了C中的無指針啟用的挑戰。它認為問題本身不是零,而是濫用。 本文詳細介紹了預防退出的最佳實踐,包括提出前檢查,指針pitiberi

本文指導初學者選擇C編譯器。 它認為,海灣合作委員會由於其易用性,廣泛的可用性和廣泛的資源,最適合初學者。 但是,它也比較了海灣室,Clang,MSVC和TCC,突出了它們的差異

本文強調了NULL在現代C編程中的持續重要性。 儘管取得了進步,但NULL對於明確的指針管理仍然至關重要,從而通過標記沒有有效的內存地址來防止細分故障。 最好的prac

本文回顧了初學者的在線C編譯器,重點是易用性和調試功能。 在線GDB和REPL。 其他選項,例如Programiz和Compil

本文討論了C IDE中的有效代碼複製。 它強調,複製是IDE功能,而不是編譯器功能,並且詳細提高了效率的策略,包括使用IDE選擇工具,代碼折疊,搜索/替換,Templa

本文比較在線C編程平台,突出了諸如調試工具,IDE功能,標準合規性和內存/執行限制等功能的差異。 它認為“最佳”平台取決於用戶需求

該教程通過在Windows,MacOS和Linux上安裝C編譯器來指導用戶。 它詳細介紹了流行編譯器(Mingw,Visual Studio,Xcode,GCC)的安裝,解釋了環境變量配置,並提供故障排除步驟


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3漢化版
中文版,非常好用

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

Dreamweaver CS6
視覺化網頁開發工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中