quinta-feira, 12 de fevereiro de 2015

Matriz transposta


Matriz transposta é uma matriz invertida, 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.
Na verdade, aproveitei o último código que postei e fiz poucas alterações
para chegar nestes ótimos resutados.

Veja abaixo imagens do programa em execução:




Veja abaixo o código do programa:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define tam 10
void Janela5 ( ) {
     int lin, col;
     col = 0;
     for ( lin = 2; lin <= 24; lin++ )
         for ( col = 2; col <= 78; col++ ) {
              gotoxy ( col, lin );
              textattr ( 3 );
              printf ( " " );
         }
}
int main ( ) {
     system ( "title MATRIZ TRANSPOSTA" );
     int a = 0, i, j, n, m, t = 0, r = 0, y = 0, temp, M [ tam ] [ tam ],
              N [ tam ] [ tam ];
     do {
         system ( "Color 80" );
         Janela5 ( );
         textcolor ( LIGHTRED );
         gotoxy ( 34, 3 );
         printf ( "MATRIZ TRANSPOSTA" );
         textcolor ( YELLOW );
         gotoxy ( 24, 5 );
         printf ( "Programa desenvolvido por:" );
         textcolor ( LIGHTCYAN );
         gotoxy ( 52, 5 );
         printf ( "Samuel Lima" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 35, 7 );
         printf ( "sa_sp10@hotmail.com" );
         textcolor ( WHITE );
         gotoxy ( 30, 9 );
         printf ( "Digite o total de linhas  " );
         textcolor ( LIGHTRED );
         scanf ( "%d", &n );
         fflush ( stdin );
         if ( n < 0 || n > tam ) {
              textcolor ( LIGHTBLUE );
              gotoxy ( 25, 9 );
              printf ( "\aO Número de linhas não Pode Ser Negativo" );
              textcolor ( LIGHTBLUE );
              gotoxy ( 25, 10 );
              printf ( "Nem Maior que %d", tam );
              Sleep ( 1000 );
              gotoxy ( 25, 12 );
              textcolor ( AMARELO );
              printf ( "PRESSIONE QUALQUER TECLA" );
              getche ( );
              system ( "cls" );
              main ( );
         }
     } while ( ( n < 0 ) || ( n > tam ) );
     do {
         textcolor ( WHITE );
         gotoxy ( 30, 11 );
         printf ( "Digite o total de colunas  " );
         textcolor ( LIGHTRED );
         scanf ( "%d", &m );
         fflush ( stdin );
         if ( m < 0 || m > tam ) {
              textcolor ( LIGHTBLUE );
              gotoxy ( 25, 13 );
              printf ( "\aO Número de colunas não Pode Ser Negativo" );
              textcolor ( LIGHTBLUE );
              gotoxy ( 25, 14 );
              printf ( "Nem Maior que %d", tam );
              Sleep ( 1000 );
              gotoxy ( 25, 16 );
              textcolor ( AMARELO );
              printf ( "PRESSIONE QUALQUER TECLA" );
              getche ( );
              gotoxy ( 25, 13 );
              clreol ( );
              gotoxy ( 25, 14 );
              clreol ( );
              gotoxy ( 25, 16 );
              clreol ( );
              gotoxy ( 57, 11 );
              clreol ( );
         }
     } while ( ( m < 0 ) || ( m > tam ) );
     Sleep ( 1000 );
     gotoxy ( 32, 21 );
     textcolor ( LIGHTGREEN );
     printf ( "PRESSIONE QUALQUER TECLA" );
     getche ( );
     system ( "cls" );
     system ( "Color 40" );
     Janela5 ( );
     textcolor ( LIGHTRED );
     gotoxy ( 34, 3 );
     printf ( "MATRIZ TRANSPOSTA" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 28, 5 );
     printf ( "Mostrando abaixo a matriz " );
     textcolor ( LIGHTRED );
     printf ( "%d", n );
     textcolor ( LIGHTBLUE );
     printf ( " por " );
     textcolor ( LIGHTRED );
     printf ( "%d", m );
     textcolor ( WHITE );
     for ( i = 0; i < n; i++ ) {
         for ( j = 0; j < m; j++ ) {
              t++;
              M [ i ] [ j ] = t;
              a = M [ i ] [ j ];
         }
     }
     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;
         }
     }
     for ( r = 0; r < n; r++ ) {
         gotoxy ( 18, r + 8 );
         for ( y = 0; y < m; y++ ) {
              printf ( " %3d ", M [ r ] [ y ] );
         }
     }
     textcolor ( LIGHTBLUE );
     gotoxy ( 18, 21 );
     printf ( "Foram gerados " );
     textcolor ( LIGHTRED );
     printf ( "%d", a );
     textcolor ( LIGHTBLUE );
     printf ( " elementos na matriz sem repetições" );
     getche ( );
     system ( "cls" );
     system ( "Color 60" );
     Janela5 ( );
     textcolor ( LIGHTRED );
     gotoxy ( 34, 3 );
     printf ( "MATRIZ TRANSPOSTA" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 20, 5 );
     printf ( "Mostrando abaixo a matriz " );
     textcolor ( LIGHTRED );
     printf ( "%d", m );
     textcolor ( LIGHTBLUE );
     printf ( " por " );
     textcolor ( LIGHTRED );
     printf ( "%d", n );
     textcolor ( LIGHTBLUE );
     printf ( " transposta" );
     textcolor ( LIGHTGREEN );
     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++ ) {
              printf ( " %3d ", N [ r ] [ y ] );
         }
     }
     textcolor ( LIGHTBLUE );
     gotoxy ( 18, 21 );
     printf ( "Foram passados " );
     textcolor ( LIGHTRED );
     printf ( "%d", a );
     textcolor ( LIGHTBLUE );
     printf ( " elementos para matriz tranposta" );
     Sleep ( 1800 );
     textcolor ( LIGHTRED );
     gotoxy ( 37, 23 );
     printf ( "MUITO OBRIGADO" );
     getche ( );
     exit ( 0 );
}

Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.