Implementing Addition of Two Matrices Using 2D-Arrays:
using System;namespace MatrixAdditionProgram { class Program { static void Main(string[] args) { //using 2D-ARRAYS int RowSize = 5, ColSize = 5; int[,] Matrix1 = new int[RowSize, ColSize]; int[,] Matrix2 = new int[RowSize, ColSize]; int[,] ResultMatrix = new int[RowSize, ColSize]; int i, j; try { Console.WriteLine("\n >>> PROGRAM To PRINT ADDITION OF TWO MATRICES <<<"); Console.Write("\n Enter the Size of a Matrix N*N (For Example:3*3) : "); string s = Console.ReadLine(); string[] values = s.Split('*'); RowSize = int.Parse(values[0]); ColSize = int.Parse(values[1]); if (RowSize > 5 || ColSize > 5) //limiting the size of matrix { Console.BackgroundColor = ConsoleColor.DarkRed; //changing background color to red Console.WriteLine(" The Size Of Matrix should Be in Less Than 5 (limiting size of array)"); System.Console.ResetColor(); ///resetting color Console.WriteLine("\n\n\t Press Enter key to exit...."); Console.ReadKey(); return; } else { //Initializing all the elements to zero for (i = 0; i < RowSize; i++) { for (j = 0; j < ColSize; j++) { Matrix1[i, j] = 0; Matrix2[i, j] = 0; } } //Reading elements of Matrix1 Console.WriteLine("\n Enter the elements of Matrix1({0}*{1})", RowSize, ColSize); for (i = 0; i < RowSize; i++) { for (j = 0; j < ColSize; j++) { Console.Write(" Matrix1[{0},{1}] : ", i, j); Matrix1[i, j] = Convert.ToInt32(Console.ReadLine()); } } //Reading elements of Matrix2 Console.WriteLine("\n Enter the elements of Matrix2({0}*{1})", RowSize, ColSize); for (i = 0; i < RowSize; i++) { for (j = 0; j < ColSize; j++) { Console.Write(" Matrix2[{0},{1}] : ", i, j); Matrix2[i, j] = Convert.ToInt32(Console.ReadLine()); } } //calculating ResultMatrix, by adding Matrix1 and Matrix2 for (i = 0; i < RowSize; i++) { for (j = 0; j < ColSize; j++) { ResultMatrix[i, j] = Matrix1[i, j] + Matrix2[i, j]; } } //Printing Result Matrix Console.Write("\n\n\t*** Result Matrix ***\n\n\t"); for (i = 0; i < RowSize; i++) { for (j = 0; j < ColSize; j++) { if (ResultMatrix[i, j] < 10) { Console.Write(" 0" + Convert.ToString(ResultMatrix[i, j])); //Making number as 01,02,etc,. } else { Console.Write(" " + Convert.ToString(ResultMatrix[i, j])); } if (j == ColSize - 1) { Console.Write("\n\t"); } } } } } catch //to catch exceptions,suppose string entered as aRowSize or Colsize of matrix { Console.BackgroundColor = ConsoleColor.DarkRed; Console.WriteLine("WARNING:only Number are allowed, Enter Correct Input"); Console.ResetColor(); } Console.WriteLine("\n\n\t Press Enter key to exit...."); Console.ReadLine(); } } }
In the above program the code can be shortened by reducing the number of for loops, but for making it easier to understand by the beginners, the program is made as simple as possible. Some of the different Sample Outputs of the above program are shown below:
Output1:
Output 3:
For C Program visit:
C PROGRAM TO PERFORM ADDITION OF TWO MATRICES
More Posts:
Appreciation for nice Updates, I found something new and folks can get useful info about BEST ONLINE TRAINING
ReplyDeleteI am glad that I saw this post. It is informative blog for us and we need this type of blog thanks for share this blog, Keep posting such instructional blogs and I am looking forward for your future posts. Python Projects for Students Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account. Project Center in Chennai
DeleteGreat Explanation..Very good Example..
ReplyDeleteOnline CSharp
Training
C Sharp Training
Online C# Training from India
C Sharp Training in Chennai
C# Training
Online Dot Net Training
.net online Training
ASP NET Training
nice article .thank you.
ReplyDeleteweb programming tutorial
welookups
This is good piece of writing and pleasant urging commented
ReplyDeletehere, I am really enjoying by these.
KissAnime
After reading this web site I am very satisfied simply because this site is providing comprehensive knowledge for you to audience.
ReplyDeleteThank you to the perform as well as discuss anything incredibly important in my opinion. We loose time waiting for your next article writing in addition to I beg one to get back to pay a visit to our website in
Selenium training in bangalore
Selenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training
Really very happy to say, your post is very interesting to read. I never stop myself to say something about it. You’re doing a great job. Keep it up…
ReplyDeleteLooking for Software Training in Bangalore , learn from Softgen Infotech Software Courses on online training and classroom training. Join today!
It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful .Oracle training in Bangalore
ReplyDeleteI have read your blog its very attractive and impressive. I like it your blog.Microsoft Training in Bangalore
ReplyDeleteGreat post!I am actually getting ready to across this information,i am very happy to this commands.Also great blog here with all of the valuable information you have.Well done,its a great knowledge.Software Testing Training in Bangalore
ReplyDeleteGreat post!I am actually getting ready to across this information,i am very happy to this commands.Also great blog here with all of the valuable information you have.Well done,its a great knowledgez. ORACLE APPS training in bangalore
ReplyDeleteVery nice information, it is valuable and useful to so many people. It gives the beautiful knowledge especially to the technical people.
ReplyDeletemicrosoft training in bangalore
microsoft training institutes in bangalore
microsoft course content
microsoft training centres in bangalore
microsoft course syllabus
microsoft training
It was a very good experience,Faculty members are very knowledgeable and cooperative. Specially My trainer teaching more as he focused upon practical rather than theory. All together it was an enlightening and informative course.
ReplyDeletemicrosoft training and placement support in bangalore
microsoft training free demo class
microsoft placement bangalore
microsoft online training
microsoft classroom training
microsoft training with lab facilities
microsoft training with certified and experienced trainers
This information is very useful in your blog. It's very awesome.
ReplyDeletePython Training in Chennai | Certification | Online Training Course | Python Training in Bangalore | Certification | Online Training Course | Python Training in Hyderabad | Certification | Online Training Course | Python Training in Coimbatore | Certification | Online Training Course | Python Training in Online | Python Certification Training Course