C_61 C program for Matrix Multiplication part2 | C Language Tutorials

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so in the stage of learning programming see we are discussing some programs on metrics in the previous video we have discussed the theory of this program how to do matrix multiplication like how to multiply two metrics how the logic behind multiplication the rules and the conditions everything we have discussed in the previous video in this video we will write a program first of all i will write a program here we'll write in that program step by step and then i'll show you output on my laptop practically you will also see right before that just want to tell you one thing whether you are preparing or will be preparing for software development roles at top product companies an academy can be a perfect companion and guide and here you get access to lot of classes which cost nothing to you but why you should attend these free classes because you get best in class educators working as software developers in companies like amazon google linkedin and other such top product companies and you also get not just the concept clearing classes by these amazing educators but also some guidance sessions on how to prepare for sde interviews how to build your resume as a programmer and many many uh such more sustainable sessions which help you in understanding perspective from experienced people and here you get access to maximum variety of classes and topics that you will ever find especially curated by subject matter exports working in top companies so when you come to the platform you will definitely always find something relevant to your current requirement for clearing your concepts you get to the point topics and subjects being covered in the classes so that you know you are being taught what matters the most and if you have missed any live class because of your busy schedule it's okay you have option to watch recording of the classes and you can also access these classes on the go on an academy app so wherever you are you can keep your preparation going on but these special classes can be thought of as a test drive or a trailer to your actual subscription so watch as much as you need to decide your favorite educator and what course you'll be following as a paid subscriber and use my code jkl10 to get 10 discount on your paid subscription so now back to the topic so now i am taking these two metrics we will be reading these metrics first and then we will print the final matrix so the dimension would be size would be number of rows of first matrix number of columns of second matrix so that would be three into two matrix that is for sure right can you multiply this check columns of first matrix three should be equal to number of rows of second matrix number of rows three yes equal yes you can multiply these so i'm just writing writing down the logic here just for multiplication for reading purpose like nested loops and scanf i guess you can do we have read metrics multiple times so that i'll show you directly on my laptop right here i will just discuss the code to multiply these matrixes right i am assuming that you have written the code to read these two metrics and to print these two metrics on the screen right now see the logic here i is 0 see second this matrix resultant matrix is suppose c this is a this is b so dimension for c is 3 into 2 means number of rows 3 2 number of columns so outer loop would be for number of rows we know so that will be from 0 to 3 in that loop would be for number of columns so that should run from 0 to 2 because here we are we will have we will be having 2 columns so j plus plus right now here what logic we have to implement c index is 0 1 2 0 1 2 0 1 2 please try to understand this one clearly first of all here if you get this logic you can directly you can easily write down the program at your own so first we'll take this complete multiply with this column first column of i mean column 0 of second matrix row 0 first matrix and we will access this and this and add this and this and add multiply this and this and add and whatever the value will get will store here the sum we will store here right so now how you are getting c here i am multiplying what i am accessing and multiplying what a of i am accessing these elements means index would be 0 0 0 1 0 2 means first index is 0 right and that is i for first iteration i would be 0 for all the iterations then it would be 1 right so we are accessing this row so for this entire row i value 0 because row for handling row we are taking a variable i i this loop is for row right so a of i now second subscript would be what i'm accessing second subscript is changing every time 0 1 and 2 first time 0 then 1 then 2 right but we cannot write down here j because j would always be run from 0 and 1 2 times only once j becomes 2 out from the loop and i want this value 0 1 and 2 and we cannot write i here because we have written i here we cannot write j so now what you will write we have only two variables i and j so here we will take another variable k and that value would run from 0 1 2 right now into b of b of here in 2 subscript what you will write we are accessing this one right so index would be 0 1 for this 1 0 2 0 means the second index is same that is 0 every time right and if you will access this one the second index would be 1 every time 0 and 1 we are we are not having any other column we are having only two columns zero and one so here we will write directly j zero and one j would run from uh would run two times zero and one so here you will write j but here what you will write because it's changing 0 0 1 0 to 0 so here we will write k obviously we cannot write i here because i value is what for every iteration it's 0. and this value is changing 0 1 2 we are simultaneously accessing this and this then multiply this and this multiply this and this multiply and then add also so we are going to multiply this right and also we are going to add in this one so we have to take another variable k and we will add in this we will take another variable like sum and whatever the value here we are going to store that into sum again right so before this another for loop that is k k should be 0 now upper bound of k would be 1 it is basically for the multiplication purpose right so k would be would run three times one two three and here also k is handling this or this three times so zero to two it should run three times so k less than three or here what you can write upper bound must be either number of columns of first matrix or number of rows of second matrix here both are 3 and 3 so that is why it is 3 and k plus plus and this you have to write down this line you will write down in this loop okay and whatever the value in sum obviously we are going to store that value in this matrix some so at c of i j here we will store sum fine that's it right now for again what we will do this row but now this column right so some you have to initialize again with 0 otherwise it will take whatever the value in sum and it will add in that some value this and this so whatever you will get here that should not be correct so after this loop again we will initialize sum with 0 so where we should initialize the sum 0 outside of this for loop so here we will initialize sum is equal to 0 within this j loop sum is equal to 0 we can initialize here and before initializing obviously we will declare this variable after main function like wherever we declare this ijk variable there only we can declare this sum right so it's okay we have stored here right now we can close this for loop and then this eye loop and now if you want to print this this i j sorry c matrix nested two loops would be required i would be 0 to 3 j would be 0 to 2 and directly you can print what print f percentage d c o 5 j that's it that is very simple here the main logic is this loop the kth loop and what you will write here that is very important that's it otherwise there is nothing in this multiplication program so let me dry run this logic and then you will get it right see we have i variable we have j variable as well as we have k and also we have sum right so i is 0 condition tree enter j 0 condition tree enter now some would be initialized with 0 here now here we have one for loop k k would benefit the 0 condition true enter now see the statement sum is equal to sum plus this sum plus sum is what 0 0 plus now what we are accessing i value 0 k value 0 k value 0 i value 0 so a of 0 0 means 0 b of 0 0 means 1 we are multiplying 0 into 1 that is 0 right so 0 plus here we have again the value is 0 and whatever the value that would be what stored in this sum so now sum is still 0 right so now we haven't you know got the exact output whatever you want here because obviously you want to add this and this multiplication of this and this and this so now rather than storing some hair better to store this in this kth loop we will only write this one line sum is equal to this and after closing this k loop then we will store the sum into co phi j right so now see now the working is what see we have done this sum is equal to sum is still 0 now k plus plus now k becomes 1 condition 2 yes again enter here 0 1 1 0 so now sum is equal to some whatever value in sum still we have 0 0 plus a of 0 1 we are going to excess a of 0 1 a of 0 0 1 that is 5 so 5 into b of 1 0 b of 1 and 0 that is 0 so that is what 0 right 5 into 0 0 and sum is also 0 so now still sum is 0 again k plus plus k becomes 2 yes condition 2 again enter here now we will access 0 2 and 2 0 so sum is still 0 plus 0 to a of 0 and 2 this one 2 into b of 2 0 b of 2 0 that is 3 3 into 2 6 now that would be stored in sum so sum becomes now 6. now k becomes 3 condition true no condition is not true exit from this kth loop and now we will we will store this in at co phi j i value 0 k value is 0 sum sum is 6 so we'll store this at 0 0 so here we will store this six so first element we have got offs the resultant matrix that is 6 right and i hope it's correct fine now see after this line what now still we are in this jth loop so now what will be done j plus plus now j becomes 1 now condition 2 again enter here again some would be initialized with 0 right if you will not initialize this sum with 0 then by default what the value in sum is 6 so it will take 6 plus it will do what multiplication of this and this and it will store here so that would not be correct so now again we have to initialize some with zero i hope you are getting my point right now again k would be zero condition true yes enter here now see i value still zero k value 0 k value 0 and i will sorry j value is now see j value is now 1 right and that's exactly 1 2 x s now sum is 0 0 plus 0 0 means 0 into 0 1 0 1 exactly now we want to access this row but this column and we are going to multiply and add so 0 into 2 but that is 0 only so that would be stored in sum sum is still 0. now k plus plus k becomes 1 condition 2 again enter here now 0 1 and we will access 1 1 so 0 1 is 5 5 into 1 1 that is 4 this is what 20 so now 20 would be stored here right but k is what 2 condition true again enter here now k is 2 here k is 2 so will x 0 2 0 2 means 2 so 20 plus 2 into 2 1 2 and 1 that is 5 10 that is 30 so now sum becomes 30 right now k plus plus k becomes 3 but condition is not true exit from the kth loop and now whatever the value in sum we are going to initialize that value we are going to store that value in c of 5 j i is 0 but j is 1 so 0 1 this index zero throw and one column so here we will store what 30 so we got this value second one 30 right so now again j plus plus now j becomes 2 but condition is not true this time jth condition right so exit from this jth loop and now still we are in ith loop now i becomes 1 right condition true again enter here and again j would be initialized with zero obviously condition true and again the same process would be repeated right so now i is one now so we will get this row and when i becomes 2 you will get this row and when i becomes 3 condition is not true exit from the loop so here we are getting 3 rows and 2 columns only right so i hope you got the working of this one right it's not confusing right so let me show you this thing practically so now let me show you practically see here i have already done this program matrix multiplication dot c because if i'll write the complete coding here then it would be very lengthy the video would be very lengthy so i'll just explain the coding i'll show you the output here i'm using macro see hash define n 50. i have just taken a size macro size 50 and here i am declaring array like what i am not giving any size i am just writing n right so this is also another way to define to write to declare size of an array using macro that's exactly i want to show here right so that is fine now here i want that user should enter number of rows and columns for these metrics for first and second matrix right so that is why i am asking here rather than you know a static declaration three into three and three into two metrics i last from user right so printf enter number of rows and columns for first matrix and for number of rows and columns for storing the value i am taking what m and n so this you need to take care m and n m number of rows for first matrix and number of columns for first matrix p number of rows for second matrix q number of columns for second matrix this you need to take care so address of m n i am just taking now i am going to enter the first matrix see two for loops outer loop would be for number of rows that is why i am writing here the upper bound is less than m rather than writing any 3 or 2 or any value i am writing m inner loop would be till n because n would be number of columns so scanf address of a of ij right same second matrix enter number of rows and columns for second matrix we are taking variable p and q and the second matrix the loop would be from 0 to p inner loop would be from 0 to q and address of a of i j right now i'm printing first and second matrix so printf first matrix is here i'm just going to print whatever you have entered in matrix form so obviously two for loops outer would be from zero to m inner would be from 0 to n and printf a 5 j that's it and same we are going to print second matrix i hope this is clear to you because we have discussed this thing many times so now here also one more thing i am adding i am going to check if we can multiply the matrix or no so what is the condition i have told you for multiplication number of columns of first matrix must be equal to number of rows of second matrix right if not equal you cannot multiply so here if n number of columns of first matrix and not equal to p p is what number of rows of second matrix if these are not equal means you cannot multiply means it should print you cannot multiply else we can multiply so in else part we will write down the coding of multiplication this right see so the resultant matrix would be of size m into q i hope you got y i am saying m into q m number of rows of first matrix and q number of columns of second matrix so the resultant size would be m into q so that is why loop would be outer loop would be from 0 to m inner loop would be from 0 to q and in this loop we are initializing sum is equal to 0 and one more loop that is k that is responsible for actual you know accessing uh the simultaneously the values and multiplying those values and adding right so same i am writing sum plus a of i k into b of k j and after this k loop i am going to store sum is equal to c of i j that's it and here i am printing the multiplication printf printing is simple two for loops only outer loop would be m 0 to m and inner loop would be from 0 to q right because you know number of rows and columns and i am just printing c of i j right now let me just run this program for you see it is asking enter rows and columns of first matrix so i am taking 3 and 3 enter first matrix so i am entering the value now 0 5 2 -1 1 0 3 7 and 5 now enter number of rows and columns for second matrix so i'm taking 3 into 2 matrix so enter second matrix that is 1 2 0 1 5 4 c this is first matrix right this is second matrix and this is the multiplication of these matrix and see let me just run this again again and show you uh one more output like if you cannot multiply the matrix that kind of thing so number of rows i am taking 3 and 3 and matrix and number of rows and columns of second matrix i am taking what it is 5 into 4. so second matrix would be good see i just want to show you this output cannot multiply why because here number of columns of first matrix is not equal to number of rows of second matrix number of columns in first matrix i have entered that as 3 but number of rows in second matrix are i guess 1 2 3 4 5 and 3 is not equal to 5 so that is why it is showing we cannot multiply right so maybe this condition you can put where here after just you know asking enter rows and columns of second matrix at that only you can check if the condition is true then only it will ask that enter second matrix otherwise it will not ask it will just directly print it we cannot multiply these metrics please enter the correct size right so i hope you got the program now so now i'll see in the next video till then bye take care
Info
Channel: Jenny's lectures CS/IT NET&JRF
Views: 16,764
Rating: 4.9302325 out of 5
Keywords: data structure tutorials, operating system, data structure and algorithms, jayanti khatri lamba, jennys lectures, jenny data structures, jennys lectures DS, jenny lamba, jennys baby, jennys lectures baby, data structures, c programming turorials for beginners, arrays in c, c program for matrix multiplication, pointers in c, functions in c, gate cs lectures, ugc net computer science preparation material, c jennys lectures, ingroduction to c programming
Id: 1TrgKl8DRk8
Channel Id: undefined
Length: 23min 7sec (1387 seconds)
Published: Sat Jul 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.