Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to Interchange Diagonals of Matrix in C program
In this tutorial, we will be discussing a program to interchange the diagonals of a given matrix.
For this, we will be given with a square matrix of the order n*n. Our task is to interchange the elements in the two diagonals of the matrix and then return the new matrix.
Example
#include<bits/stdc++.h>
using namespace std;
#define N 3
//interchanging the two diagonals
void int_diag(int array[][N]){
for (int i = 0; i < N; ++i)
if (i != N / 2)
swap(array[i][i], array[i][N - i - 1]);
for (int i = 0; i < N; ++i){
for (int j = 0; j < N; ++j)
printf(" %d", array[i][j]);
printf("
");
}
}
int main(){
int array[N][N] = {24, 45, 64,
17, 21, 34,
75, 38, 98};
int_diag(array);
return 0;
}
Output
64 45 24 17 21 34 98 38 75
Advertisements