em relação a matriz original,
as linhas da matriz original,
passarão a ser as colunas da matriz transposta,
e as colunas da matriz original passarão a ser
as linhas da matriz transposta.
Se você já domina matrizes não terá dificuldades
em entender o que é matriz transposta.
Acompanhe agora mesmo este exemplo:
Abaixo está o código do programa:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define tam 10
#define ENTER 13
bool x = false, k = false;
int m, n, o = 0;
int a = 0, b = 0, i, j, t = 0, v = 0, r = 0, y = 0, temp;
int main ( );
/*============================================================================*/
//Criando uma moldura na tela do dos
int Moldura ( int tam_lin_ini, int tam_lin_fim,
int tam_ini_col, int tam_fim_col ) {
int i, c;
for ( i = tam_lin_ini; i < tam_lin_fim; i++ ) {
for ( c = tam_ini_col; c < tam_fim_col; c++ ) {
gotoxy ( c, i );
textbackground ( WHITE );
printf ( " " );
}
}
return 0;
}
/*============================================================================*/
void Informe ( ) {
if ( x == false ) {
gotoxy ( 38, 23 );
textcolor ( LIGHTRED );
printf ( "TECLE ENTER" );
} if ( x == true ) {
textcolor ( LIGHTBLUE );
gotoxy ( 26, 20 );
printf ( "Criado por:
" );
textcolor ( LIGHTMAGENTA );
gotoxy ( 126, 20 );
printf ( "Samuel Lima" );
textcolor ( BLACK );
gotoxy ( 23, 23 );
printf ( " " );
gotoxy ( 26, 21 );
printf ( "sa_sp10@hotmail.com" );
textcolor ( LIGHTRED );
gotoxy ( 36 , 23 );
printf ( "MUITO
OBRIGADO" );
}
}
/*============================================================================*/
void matriz_transposta (
) {
do {
Moldura ( 2, 25, 3, 79 );
Informe ( );
textbackground ( WHITE );
textcolor ( LIGHTRED );
gotoxy ( 32, 3 );
printf ( "MATRIZ
TRANSPOSTA II" );
textcolor ( LIGHTBLUE );
gotoxy ( 30, 9 );
printf ( "Digite o
total de linhas: " );
textcolor ( LIGHTRED );
scanf ( "%d", &n );
fflush ( stdin );
if ( n < 3 || n > tam ) {
textcolor ( LIGHTBLUE );
gotoxy ( 30, 13 );
printf ( "\aO Número não
Pode Ser Negativo" );
textcolor ( LIGHTBLUE );
gotoxy ( 30, 14 );
printf ( "e tem que ser
maior ou igual a %d", tam - 7 );
Sleep ( 1800 );
main ( );
}
} while ( ( n < 3 ) || ( n > tam ) );
/*=========================================================*/
do {
Informe ( );
textcolor ( LIGHTBLUE );
gotoxy ( 30, 11 );
printf ( "Digite o
total de colunas: " );
textcolor ( LIGHTRED );
scanf ( "%d", &m );
fflush ( stdin );
if ( m < 3 || m > tam ) {
textcolor ( LIGHTBLUE );
gotoxy ( 30, 13 );
printf ( "\aO Número não
Pode Ser Negativo" );
textcolor ( LIGHTBLUE );
gotoxy ( 30, 14 );
printf ( "e tem que ser
maior ou igual a %d", tam - 7 );
Sleep ( 1800 );
gotoxy ( 25, 13 );
clreol ( );
gotoxy ( 25, 14 );
clreol ( );
gotoxy ( 25, 23 );
clreol ( );
gotoxy ( 57, 11 );
clreol ( );
}
} while ( ( m < 3 ) || ( m > tam ) );
/*=========================================================*/
do {
Informe ( );
textcolor ( LIGHTBLUE );
gotoxy ( 30, 13 );
printf ( "Multiplicar a
diagonal por: " );
textcolor ( LIGHTRED );
scanf ( "%d", &b );
fflush ( stdin );
if ( b < 2 || b > tam ) {
textcolor ( LIGHTBLUE );
gotoxy ( 30, 15 );
printf ( "\aO Número não
Pode Ser Negativo" );
textcolor ( LIGHTBLUE );
gotoxy ( 30, 16 );
printf ( "e tem que ser
maior ou igual a %d", tam - 8 );
Sleep ( 1800 );
gotoxy ( 25, 15 );
clreol ( );
gotoxy ( 25, 16 );
clreol ( );
gotoxy ( 57, 13 );
clreol ( );
}
} while ( ( b < 2 ) || ( b > tam ) );
}
/*============================================================================*/
int Cria_Matriz ( int M [ tam ] [ tam ] ) {
for ( i = 0; i < n; i++ ) {
for ( j = 0; j < m; j++ ) {
t++;
M [ i ] [ j ] = t;
a = M [ i ] [ j ];
}
}
srand ( time ( NULL ) );
for ( i = 0; i < n; i++ ) {
for ( j = 0; j < m; j++ ) {
r = rand ( ) % n;
y = rand ( ) % m;
temp = M [ i ] [ j ];
M [ i ] [ j ] = M [ r ] [ y ];
M [ r ] [ y ] = temp;
}
}
return 0;
}
/*============================================================================*/
void Imprime_Matriz ( int M [ tam ] [ tam ], int N [ tam ] [ tam ] ) {
if ( k == false ) {
for ( r = 0; r < n; r++ ) {
gotoxy ( 18, r + 8 );
for ( y = 0; y < m; y++ ) {
printf ( " %3d ", M [ r ] [ y ] );
}
}
}
if ( k == true ) {
for ( r = 0; r < m; r++ ) {
for ( y = 0; y < n; y++ ) {
N [ r ] [ y ] = M [ y ] [ r ];
}
}
for ( r = 0; r < m; r++ ) {
gotoxy ( 18, r + 8 );
for ( y = 0; y < n; y++ ) {
if ( r == y ) {
textcolor ( LIGHTRED );
N [ r ] [ y ] = N [ r ] [ y ] * b;
printf ( " %3d ", N [ r ] [ y ] );
} else {
textcolor ( LIGHTBLUE );
printf ( " %3d ", N [ r ] [ y ] );
x = true;
}
}
}
}
}
/*============================================================================*/
int main ( ) {
int M [ tam ] [ tam ], N [ tam ] [ tam ];
char letra = '\0';
system ( "title MATRIZ
TRANSPOSTA" );
matriz_transposta ( );
Moldura ( 2, 25, 3, 79 );
textbackground ( WHITE );
textcolor ( LIGHTRED );
gotoxy ( 32, 3 );
printf ( "MATRIZ
TRANSPOSTA II" );
textcolor ( LIGHTBLUE );
gotoxy ( 26, 5 );
printf ( "Mostrando
abaixo a matriz " );
textcolor ( LIGHTRED );
printf ( "%d", n );
textcolor ( LIGHTBLUE );
printf ( " por " );
textcolor ( LIGHTRED );
printf ( "%d", m );
textcolor ( LIGHTBLUE );
Cria_Matriz ( M );
Imprime_Matriz ( M, N );
//Este bloco só permite a passagem pela tecla
ENTER
//Se alum caractere for digitado, um beep é
acionado
do {
textcolor ( LIGHTBLUE );
gotoxy ( 18, 21 );
printf ( "Foram gerados
" );
textcolor ( LIGHTRED );
printf ( "%d", a );
textcolor ( LIGHTBLUE );
printf ( " elementos na
matriz sem repetições" );
Informe ( );
letra = getch ( );
fflush ( stdin );
if ( letra == ENTER ) {
k = true;
}
if ( letra != ENTER ) {
Beep ( 1000, 500 );
}
continue;
}while ( letra != ENTER );
Moldura ( 2, 25, 3, 79 );
textbackground ( WHITE );
textcolor ( LIGHTRED );
gotoxy ( 32, 3 );
printf ( "MATRIZ
TRANSPOSTA II" );
textcolor ( LIGHTBLUE );
gotoxy ( 27, 5 );
printf ( "A diagonal da
matriz " );
textcolor ( LIGHTRED );
printf ( "%d", m );
textcolor ( LIGHTBLUE );
printf ( " por " );
textcolor ( LIGHTRED );
printf ( "%d", n );
gotoxy ( 27, 6 );
textcolor ( LIGHTBLUE );
printf ( "transposta
foi multiplicada por " );
textcolor ( LIGHTRED );
printf ( "%d", b );
Imprime_Matriz ( M, N );
Informe ( );
getche ( );
exit ( 0 );
}
/*============================================================================*/