Hello and welcome back to this practical module…..
The way we are looking at different practical one by one, learning them, understanding them and we were solving different problem statements.
Taking that flow ahead.
We have a new practical, one new video where we will be discussing about matrix addition.
What is this matrix addition? All these things we will be seeing.
So, without any delay, we will start.
First of all, we will talk about the matrix addition, what exactly is it? So, Matrix we all must have learned in mathematics and the ones who don't remember it, we will remind them that matrix is a way to represent the data in the tabular format.
Especially in mathematics it has been used where we store the numbers in the rows and columns format and we perform some operations over them.
We call that matrix.
If we say that we have got two matrix and we have to add them.
How this matrix addition has to be done? That is what we will be seeing in this video with the help of C program.
Now, we will talk about how we will visualise them and how we will create it in our C program.
You must be remembering that in array topic, we had discussed one topic two dimensional arrays and in that we had seen that we can store our data in the format of rows and columns with the help of 2D arrays, in the same way, we would be making 2d arrays and we will be storing them in our matrix data and after that we will add those matrix so that we can print that matrix addition in our terminal.
So, without any delay we will start and see whatever is going to happen in this video.
With the help of C program we are making one matrix addition program.
We will quickly go on to the text editor.
So, here we have come near our program.
So, first of all we are using #include and with its help we will be including stdio.h, after including this file the times comes of our main function, which is a very important part of any C program.
Now we have come into our main function and you can see that the first thing that we are going to do that is defining some variables, some arrays, which means making two dimensional arrays.
We will first understand that how we will be doing the addition of this matrix.
So, first of all we will take two different two dimensional arrays, which we will name a and b, a matrix will be a two by two matrix because of which it will be easy for you to understand this concept, along with that B will also be a two by two matrix means that it will have two rows and two columns in it and we are going to do +b, which means we are going to add matrix A with matrix B, in that situation what will happen is in the corresponding position the elements which are there, they will add them themselves and the third matrix that will come.
We will call that as sum matrix, even that will be in 2 by 2 form.
It will be both matrix, which means both A and B matrix elements that are there, the corresponding elements that are there, there addition will be store in its corresponding position.
Because of this we will have to make three different 2d arrays, the first 2d array will be to represent our A matrix, second 2d array would be to represent B matrix.
Along with that our third 2d array which will be there, it will be used to store the sum of both the matrix.
That’s why we are making here sum named 2d array, which will store both the matrix addition in itself.
So, you can see that we have defined its rows and columns.
There will be two rows and two columns.
And along with that, we are starting i and j named two variables and definitely we will be using it here for our for loops, about which we will get to know in some time.
So, here we have defined our sum metrics, but you will ask that we have not made our A and B matrix.
So, let's make even that and to make that you can see that we have made two 2d arrays named A and B and we have stored the respective data inside it.
So, first matrix elements are 2, 3, 4 and 7 and along with that our second matrix B, its elements are 3, 1, 2 and 4.
So, these are our two matrices and between these matrices they will be addition and to do the addition we know that this 2 is going to be added with whom? With this 3.
Along with that this 3 that is there with us, that will be added with this 1.
Why? Because its position in both the matrices is the same.
And now we had discussed that the corresponding.
position elements add each other and this stores in some third matrix.
So, here we spoke about we have made A matrix, B matrix and the 3rd matrix which is called as
sum, we even defined that.
Here in which we will be storing the addition of matrices.
After making all these things, now comes the time of what? To define our logic.
So, what is the logic addition of corresponding elements of the matrices.
And you all know that suppose if I want to access this particular element and my element is on the 0th row and column.
That's why its index position which will be there, that will be 0,0.
Along with that if we talk about this 3, its index position would be 0,1 because it's on 0th row, but its column number has become 1.
In this way we will call this as 1,0 index position.
And we will call this 1,1 index position.
In this way it will be for B matrix and whatever is there on 0,0 index position that will add into B’s 0,0 index position’s element and it will be stored in our sum 2d array.
Now we will talk about the logic, to talk about the logic.
We will make our for loop over here.
Now how to make this for loop we will understand that quickly.
Here we will be considering two things one is the rows of the 2d array and second is the columns of 2d array.
First, one loop will go for the row and the second loop will be used to traverse the column.
So, for each row, the inside for loop which will be there, that will go on for each column.
And as soon as the row will be changed, again for each column our loop will go on and it will be doing the addition between them and give us.
That's why we have started the first from zero because the index position starts with zero and we have taken it till less than 2.
Till the time the i’s value is less than 2.
Till that time our for loop will go, in the same way inside this also we have made one for loop, this other for loop is to traverse the columns and if we talk about here, here we have the square matrix.
Which means the number of rows and columns are equal, they are two and two, that's why the inside loop will go on till “j<2” condition.
Now we will come into logic.
Now the logic is very easy.
And how is it we will understand it over here, you will see that inside sum, in the index position we have put i and j, it means that the corresponding value of i and j at that time, at that particular location element of I and j’s next position.
We will add that with the corresponding b’s I and j’s index position element and it will be stored inside what? Sum’s 2D array’s I, j’s index position, in the same way for each value of I, our J will keep increasing.
There on those locations.
Sum’s 2D Array’s location the addition of a and b’s elements, that will be stored.
So, in this way our addition’s logic will go on.
What we have to do is we have to put two loops, first loop will go on for our rows and the second loop will go on for our columns and inside that we will write our addition’s logic which is a’s corresponding element plus b’s corresponding elements at certain index position of I, j, that will go and get stored in the sum’s I, j index position place.
So, this was about it, now we have added it Now the sum matrix which is the, the sum’s 2D array, that is also made, it is completely ready.
Now we have to print it and see.
To print it.
we will have to access the elements one by one.
To access the elements one by one, we all know that we require for loops.
Here since there is 2d array and there is concept of rows and columns, that's why we will be making use of nested loops again over here, which means that inside one for loop, we are going to write another for loop.
So, you can see that first of all we are writing sum of the two matrices equal to, this is the magic of our printf statement, you can see that the first for loop has run for row, second for loop has run for our column.
And for each value of i and j, I'm printing my particular element.
How? With the help of this printf statement, where I have put the format specifier as %d.
And whatever value was there in my sum’s I, j index position.
That I am printing over here.
Below that, you will see that we have written one more if statement.
And this if statement is related to which thing.
In this we're checking the condition that the j’s value was zero, and then it became one.
And when it became equal to one, after that we have to print our values in the next line.
Because we know that our matrix are there in a tabular format.
So, we don't write them in one line.
Usually what we do is the first row’s element we write in first row.
After that, we come in the below line and write the elements of the second row, that's why when the j’s value will strike on 1, what we will do is, with the help of printf statement, we will change the line with the help of \n, \n we have used twice over here, which means that our cursor will come two lines below.
and then it will print our next value.
This might look a little complex to you.
But we are going to take the output and see and for that we will have to go in our terminal.
So, now we have come into our terminal, what we will do quickly is we will open our common prompt over here.
And after going into the command prompt, I will write my command which is “matrix addition.c”.
And as soon as I press enter, you will see that it has got completely compiled.
Now we just have to run it.
Now as soon as I run it, you will see that I'm getting my matrix sum printed over here.
So, this was about how to add two matrix.
How it's additions comes out, that we have seen over here.
Our first element was two and the b’s first element was three.
When we do 2+3.
Definitely we get 5 over here.
In the same way.
If we talk about this a’s element, 3+1 is over here, 3+1 is 4 and correspondingly, our 6 and 11 values that have come, it has come out as 4+2 and 7+4.
So, in this way our matrix addition is done.
We don't have to do anything in the matrix addition logic.
We just have to put two for loops.
One for loop is for row end second for loop is for column and the logic of the sum of the corresponding index position, that we have to put and after that, we just have to print it and show it on the terminal.
So, you can execute this program in your computer line by line and check it, how our programs flow is going on.
And then you can also do some minor tweaks in it as per your requirement.
We have taken 2 dimension array over here.
Then we can increase the size of matrix, we can take 3 by 3 or we can take some other matrix to see this particular example.
So, you practise it till that time.
This particular matrix addition program and we will meet you in the next video with one new program, till that time, keep practising.
Thank you so much.
If you think that there is any part of this video or this topic, you might have not understood then you can ask us any query or question related to that topic without any inhibition.
You just have to go on forums.learnvern.com, you have to type your query there in your own
language and we will come up
with the solution or the answer as
soon as possible in your own language.
Apart from this if you wish to have a discussion on any topic.
Even that you will be able to do on forums.learnvern.com.
Share a personalized message with your friends.