Write a program to accept a two dimensional array with M rows and N columns.
i) Find the sum of the elements.
ii) Find the sum of left diagonal elements.
iii) Find the sum of right diagonal elements.
iv) Find the transpose of the original matrix.
v) Display all the above details along with the original matrix.
Answers
Answer:
Note: I am using C++ programming language.
Code
#include<iostream>
using namespace std;
int main () {
int ar[10][10] ,trans[10][10] ;
int m,n ,sum=0, sum_right =0 , sum_left = 0;
cout<<"Enter no of rows and columns :\n" ;
cin>>m>>n;
cout<<"Enter your array elements: \n" ;
for(int i =0 ; i<m ;i++ ){
for(int j=0;j<n ; j++) {
cin>>ar[i][j];
}
}
for(int i =0 ; i<m ;i++ ){
for(int j=0;j<n ; j++) {
sum += ar[i][j] ;
}
}
cout<<"\n\n* Sum of all elements in the array is " <<sum <<endl;
for(int i =0 ; i<m ;i++ ){
for(int j=0;j<n ; j++) {
if( i == j )
sum_left += ar[i][j] ;
}
}
cout<<"\n\n* Sum of left diagonal the array is " <<sum_left <<endl;
int k = n ;
for(int i = 0 ; i<m ;i++ ){
for(int j=0;j<n ; j++) {
if( j == (k-1) ) {
sum_right += ar[i][j] ;
}
}
k--;
}
cout<<"\n\n* Sum of right diagonal the array is " <<sum_right <<endl;
for(int i =0; i<m ;i++){
for(int j=0;j<n;j++){
trans[i][j] = ar[j][i] ;
}
}
cout<<"\n* Transpose matrix is : \n" ;
for(int i =0; i<m ;i++){
for(int j=0;j<n;j++){
cout<<trans[i][j] <<" " ;
}
cout<<"\n" ;
}
return 0;
}
** Mark this ans as Brainliest Ans. Thank you!
Explanation:
// Java program for implementation of Selection Sort
class SelectionSort
{
void sort(int arr[])
{
int n = arr.length;
// One by one move boundary of unsorted subarray
for (int i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
// Swap the found minimum element with the first
// element
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}