‫Hello my friends, and welcome to coding Exercise number nine.
‫In this exercise, we are asked to write a program to add two matrices of the same size.
‫For example, about the addition of two matrices.
‫This the first metric metrics and this is the second matrix.
‫One, two one, two, one, three, three, two, one.
‫We need to add it to two.
‫One, two, three, two, one, one, three, two.
‫We start by the index here, the first index, the first element here at row one and column one and
‫we add it when the with the number with the same index, row one, column one, So one plus two will
‫give me three.
‫And the new matrix the result matrix in the same index row one and a column one, then two with one.
‫It will give me three.
‫Two here with three will give me five, one with two will give me three and so on.
‫Pause the video and think about how to create such a program that add that adds to matrices.
‫And I hope you solve it.
‫Well and correctly.
‫I am waiting your solutions in the comment below.
‫When talking about matrices, think about multi dimensional arrays.
‫So I will create three or four numbers of type integers, M and C and D.
‫I will show you what these numbers are.
‫Scanner scan is one equal to new scanner.
‫I need the user to enter the following.
‫Please enter the number of rows of the matrix so system that output print l enter the number of rows
‫and here I will place the number of columns in the multi dimensional array.
‫We start by the rows and then the columns.
‫So m equal to s one dot.
‫Next integer.
‫I will name it as a row R.
‫And these are the common.
‫This role, I will create the variables row in order to understand what are these variables and here
‫column equal se one.
‫Next here we stored the number that the user entered with the number of rows in row variable.
‫And here the number of columns is inside the call variable.
‫Now here we are creating the matrices.
‫We start by creating an array array one of two.
‫Curly or do angled brackets.
‫You see, guys, these square brackets, these are two square brackets.
‫And I'm telling Android Studio that this array one is multi dimensional array, how to specify the number
‫of rows and number of columns.
‫It's very simple.
‫We specify here row and here the call variables here.
‫I'm telling that the array one would be a multi dimensional array of number of rows row, which is the
‫enter by the user and column as the number of the columns that it's entered by the user.
‫Also, the same way, I need to create an array to in order to add them together and I will create the
‫same way a sum array.
‫The same way, the same size and the same number of rows and the same number of columns.
‫This is array one, this is error two, and this is the sum of the Metro sixes.
‫Now let's get getting the elements of Matrix one.
‫This will allow the user to enter the variables and the elements of the matrix.
‫Number one system out to print and enter the elements of.
‫First metrics.
‫I will start looping through this array in order to add then the elements in the correct order.
‫So see equal or call.
‫C equal to zero.
‫The counter equal to zero.
‫C is less than.
‫Arrow and C plus plus what we need to make we need add another loop because we are dealing with multi
‫dimensional arrays.
‫So always when you deal with multi dimensional arrays, you need nested to use nested loops.
‫The less than an or columns and the plus plus.
‫You see, guys, I am dealing with the rose at first and then the columns.
‫So always bear in mind that I need to start with arrows and here, start by the columns.
‫Exactly that what we have done in the previous video.
‫All right one at index see element and index see and index add element DX is equal to scanner one dot
‫next.
‫And here I am telling Android to keep waiting for the input of the user to enter the elements at index
‫C and the until reaching the end.
‫So if the user enters the row equals three and the column is three, then enter instead you will wait
‫for nine parameters, nine variables entered by the user.
‫Again, this is getting the elements of the error in.
‫I will copy it and paste it here.
‫Enter the elements of the second array or second matrix.
‫This is the second matrix c0d equal to zero.
‫Start filling the second array by rows at first and then the columns and everything will work fine.
‫Okay.
‫Now.
‫Let's get the summation of the matrices.
‫The result I'm getting here.
‫Making the summation.
‫The summation.
‫Part of the addition of two matrices is we start by a four loop because we have the same way I'm using
‫a multi dimensional arrays.
‫So I need to go through these first eight rows and second add columns in the same way I will get and
‫make a summation matrix some here, some matrix at index column or a rows at first and then column.
‫Or C because I'm using these indices equal to array one at index C and D, because the first element
‫in the first row and this first column of the first.
‫Array, plus the first element which is placed in the first row and the first column in the second array
‫will give me a result that it will be displayed in the first row of the first column of the summation
‫matrices.
‫I think it may be b confusing, but I want from you to understand how this algorithms work, how to
‫think about these problems dynamically and with the coding way.
‫This is how I'm teaching you how to think.
‫Not teaching you these codes, Doctor, to write them down.
‫I'm teaching you how to think, how to solve problems in a coding way, in a simple way.
‫Array number two at index C and index D.
‫This is the translation of what we have talked about in mathematics.
‫Okay, now system dot out, dot print l and the sum sum of matrices is
‫I will start displaying now displaying here, displaying the summation or that result.
‫Matrix four.
‫I will start again using this.
‫For loop again, because I will start from C equal to zero and D equal to zero.
‫This is for the rows and these for columns.
‫But here I need to change this so I will print system that output print.
‫The sum array at index C and index D plus tap OC plus.
‫This the skipping character that will give me a tab.
‫And here we will print a new line.
‫After finishing the arrow, it will go to the second row.
‫Let me run and see this program.
‫I hope you understand well, because there is some complex there are some complex set topics here,
‫the the rows, the columns, the nested loops and so on.
‫But it's very it's very simple if you understand well, and you know, some metro mathematics enter
‫the number of rows I will get three and the number of columns is three.
‫Enter the elements of the first matrix.
‫The first matrix, it would be one, two, three, four, five, six, seven, eight and nine.
‫Enter the elements of the second matrix, it will be one, two, three, four, five, six, seven,
‫eight and nine.
‫The sum of matrices is two, four, six, eight, ten, 12, 14, 16 and 18.
‫If we sum the last number with the last number it will give me 18.
‫And this is the last element.
‫So congratulations, guys.
‫We have succeeded in creating a program that sum them and add up to matrices and it will result in this
‫in a matrix of the same size and the same number of frozen number of columns.
‫Congratulations, guys.
‫Please don't mess to write us five stars to support us and help you in your coding journey.