![]() |
Program to find the sum of each row & column of a matrix of size n x m and if matrix is square, find the sum of the diagonals 1 Attachment(s) Program to find the sum of each row & column of a matrix of size n x m and if matrix is square, find the sum of the diagonals also. #include #include int main() { clrscr(); int A[10][10],m,n,x,y,sum=0; //Create a Matrix A cout << "Enter number of rows and columns in Matrix A : \n"; cin>>n>>m; cout << "Enter elements of Matrix A : \n"; for(x=1;x //Find sum of each row for(x=1;x A[x][m+1]=0; for(y=1;y } //Find sum of each column for(y=1;y A[n+1][y]=0; for(x=1;x } cout << "\nMatrix A, Row Sum (Last Column)" << " and Column Sum (Last Row) : \n"; for(x=1;x for(y=1;y cout << "\n"; } //Print sum of each column x=n+1; for(y=1;y cout << "\n"; if(m==n) { for(x=1;x sum+=A[x][y]; else if(y==m-(x+1)) sum+=A[x][y]; } cout << "Sum of diagonal elements is : " << sum << endl; getch(); return 0; } It then calculates the sum of each row and each column and outputs it using the 'cout' command. Also, if it is a square matrix, it calculates the sum of diagonal elements and prints it out. Sample Input 3 3 9 8 7 6 5 4 3 2 1 Sample Output Matrix A, Row Sum(Last Column) and Column Sum(Last Row) : 9 8 7 24 6 5 4 15 3 2 1 6 18 15 12 Sum of diagonal elements is : 15 |
| All times are GMT +5. The time now is 10:08 PM. |
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.