A[ i ][ j ] aparține matricei A,
0 <= i,j <= N-1.
Elementele diagonalei principale respectă relația i = j, altfel spus au forma
A[ i ][ i ]. ( Cele colorate cu verde )
Elementele diagonalei secundare sunt de forma
A[ i ][ N-i-1 ]. ( Cele colorate cu albastru )
INTERSCHIMBARE TRIUNGHI SUPERIOR/INFERIOR: for( i = 0 ; i < N/2 ; ++i )
for( j = i+1 ; j < N-i-1 ; ++j )
{
aux = Mat[i][j];
Mat[i][j] = Mat[N-i-1][j];
Mat[N-i-1][j] = aux;
}
INTERSCHIMBARE TRIUNGHI STANGA/DREAPTA: for( j = 0 ; j < N/2 ; ++j )
for( i = j+1 ; i < N-j-1 ; ++i )
{
aux = Mat[i][j];
Mat[i][j] = Mat[i][N-j-1];
Mat[i][N-j-1] = aux;
}
Sursă completă:# include <stdio.h>
# define InFile "input.txt"
# define OutFile "output.txt"
# define NMax 1000
int N,Mat[NMax][NMax];
void Citire()
{
int i,j;
scanf("%d",&N);
for( i = 0 ; i < N ; ++i )
for( j = 0 ; j < N ; ++j )
scanf("%d",&Mat[i][j]);
}
void Rezolvare()
{
int i,j,aux;
// Triunghi superior - Triunghi inferior
for( i = 0 ; i < N/2 ; ++i )
for( j = i+1 ; j < N-i-1 ; ++j )
{
aux = Mat[i][j];
Mat[i][j] = Mat[N-i-1][j];
Mat[N-i-1][j] = aux;
}
// Triunghi stanga - Triunghi dreapta
for( j = 0 ; j < N/2 ; ++j )
for( i = j+1 ; i < N-j-1 ; ++i )
{
aux = Mat[i][j];
Mat[i][j] = Mat[i][N-j-1];
Mat[i][N-j-1] = aux;
}
}
void Tipar()
{
int i,j;
for( i = 0 ; i < N ; ++i, putchar('\n') )
for( j = 0 ; j < N ; ++j )
printf("%d ",Mat[i][j]);
}
int main()
{
freopen(InFile,"r",stdin);
freopen(OutFile,"w",stdout);
Citire();
Rezolvare();
Tipar();
return 0;
}