 Hello friends, I am Sanjay Gupta. In this video I am going to demonstrate you how you can calculate some of major and minor diagonal of a matrix by passing 2D array into a function. Before starting, you can note my information, you can follow or subscribe my YouTube channel through the URL youtube.com slash sanjaygupta underscore tech school. You can download my programming app, Tachymiz, which is available on Google Play. Now I am going to implement the solution of this problem with the help of C programming. So first of all, I am including a header file that is stdio.h. Now I will be declaring two functions, one for major diagonal sum and one for minor diagonal sum. So first one is major sum. So this function return type is int name is major sum and it will receive an argument in form of 2D array whose dimension will be 3 by 3. So this statement is function declaration. Now second function int minor sum int 3 by 3. So again return type is integer, name of function is minor sum and it will also receive a 2D array of dimension 3 by 3. So this statement is function declaration. Now I am implementing main function definition. So inside main function, I am declaring a 2D array, variables i, j and then s. Now with the help of printf, I am going to display this message enter elements on console to receive elements of 2D array. I am applying nested loops. So for that purpose, first I have implemented i loop which will provide row indexes. After that, inner loop is implemented which is maintained by j which will provide column indexes. Now to receive elements from user, I am implementing scanf. So scanf will read a particular element and that element will be assigned into a matrix. So this is the reading process. After completion of reading process, I can call major sum and minor sum functions. So first I am calling major sum function. So this statement is function calling. So major sum function is passing an argument in form of 2D array which is a and the result will be stored inside s variable. Now I can print sum of major diagonal equals to percent d and then s. So through this printf statement, sum of major diagonal will be displayed on console. Now I am calling minor sum function and again I am passing same 2D array. So this statement is also a function call. Again I am using printf statement which will display the message sum of minor diagonal equals to percent d comma s. So this is the complete implementation of main function which is calling 2 functions major sum and minor sum. So first I am defining major sum function. So this block is function, sorry, definition. Inside this function definition I am declaring some variables like i, j and s equals to zero. Now I am implementing nested roots again for calculation purpose. So here you can see I have implemented i loop which will provide row indexes and then inside this i loop I am implementing j loop which will provide column indexes. For major sum I am applying a condition i double equals to j. So major sum indexes are, sorry, major diagonal indexes are zero zero one one two two and so on. So if row and column indexes are same then I can apply this condition if i double equals to j and I can apply the addition process s equals to s plus aij. So if row and column indexes are same then that element is major diagonal element. So I have added that element using this s equals to s plus aij statement. So after completion of this loop all the elements will be added into s variable. So I can return that variable and that value will be received inside main function at function calling statement. So this is the complete definition of major sum function. Now I am applying, sorry, I am copying this code and I am pasting it. So you can see total two definitions are available here. First one is major sum and second one is now minor sum. So minor sum indexes condition is i plus j double equals to 2. So if we add minor diagonal indexes that is i and j. So the sum will be equals to 2 if dimension is 3 by 3. If dimension is 4 by 4 then that diagonal elements indexes will be sorry their sum will be 3. So you can say the row size or column size minus 1 is equals to the sum of indexes available at a minor diagonal position. So I am adding ij if its sum is equals to 2 then only I can add aij into s variable. So this is the logic for calculating sum of minor diagonal. So this way I have implemented the complete code in front of you. So right now I have implemented sum with two loops. After execution I will be demonstrating you how you can calculate sum of major and minor diagonals using single loops. So first time executing this code you can see I am entering 3 by 3 matrix. Now you can see the output sum of major diagonal and sum of minor diagonal. So major diagonal elements are 1, 5 and 9. So sum is 15 minor diagonal elements are 7, 5 and 3. So these elements are also equals to sum 15. So this way program is working properly. Now I am going to convert this logic with the help of one loop. So I am removing this j loop and I am applying logic with only i loop. So this is the logic for calculating sum of major diagonal. So major diagonal indexes are same. So that's why I am applying ii here. So i first time is 0. So 00 index will be added into s. Then 11 index will be added into s. So similarly all the elements which are available on major diagonal position will be added into s variable. Now in case of minor diagonal I am applying s equals to s plus a i and 2 minus i. So this is the logic for adding elements available on minor diagonal. So if we see minor diagonal positions. So minor diagonal suppose matrix is 3 by 3. So first element of minor diagonal will be at 02 position. So you can see i is providing 0 and 2 minus 0 is 2. So first element 02 will be available here that will be added into s. Then second position of minor diagonal will be 11. So after increment operation I will become 1. So first index will be 1 and 2 minus i 2 minus 1 will be 1. So 11 index element will be added into s. Then third location of minor diagonal position will be 20. So i will be incremented to 2. So 2 and 2 minus 2 0 will be available here. So this way addition of minor diagonal diagonal will be calculated using single loop. So now I am going to execute this code. I am entering the values. You can see the same result is displayed here again. Some of major diagonal is 15 and some of minor diagonal is also 15. So this way I have implemented two different logics with major sum and minor sum functions for calculating some of major and minor diagonals and I have passed 2d array into function. So I hope you have understood all the concepts which I have demonstrated you in this video. So in this video we have calculated some of major and minor diagonals with two different ways and with the help of passing 2d array into function. If you want to watch more programming related videos you can follow or subscribe my YouTube channel through the URL youtube.com. You can download my programming app Takimis which is available on Google Play. Thank you for watching this video.