top of page
Search

Difference between diagonal elements in a matrix

  • Ben
  • Feb 13, 2018
  • 1 min read

#include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <limits.h> #include <stdbool.h>

int main()

{ int n; scanf("%d",&n); int c=n; int result1=0; int result2=0; int result3; int a[n][n]; // Getting input matrix for(int a_i = 0; a_i < n; a_i++){ for(int a_j = 0; a_j < n; a_j++){ scanf("%d",&a[a_i][a_j]); } } // Getting one diagonal elements for(int a_i = 0; a_i < n; a_i++){ for(int a_j = 0; a_j < n; a_j++){ if(a_i == a_j) { result1= result1+ a[a_i][a_j]; } } } // Getting other diagonal elements for(int a_i = 0; a_i < n; a_i++) { result2 = result2+ a[a_i][c-1]; c=c-1 ; } // Difference of diagonal elements result3 = abs(result2 - result1); //printf("%d\t", result1); //printf("%d", result2); printf("%d",result3); return 0; }

 
 
 

Recent Posts

See All
Swap two words in a byte

#include <iostream> using namespace std; int main() { unsigned int data=0x1234; cout<<"data before swapping :"<<hex<<data<<endl; ...

 
 
 
Fibonacci series

#include <iostream> using namespace std; int main() { // Getting the input of fibonacci series long int n; cout<<"Enter the size of...

 
 
 

Kommentare


bottom of page