sexta-feira, 10 de outubro de 2014

Trocando linhas e colunas de uma Matriz

Neste código mostro como trocar linhas e colunas de uma matriz,
 na verdade é muito fácil, tudo o que fiz, foi criar dois vetores
que trabalham em modo temporários recebendo as linhas e colunas específicas.
A matriz de inteiros é quadrada e seus números contam de 1 a 25,
assim pré-definidos em todas as suas posições.

Veja abaixo, imagens do programa em execução:


 

Veja abaixo, o código do programa:

#include <stdio.h>
#include <conio.h>
#define tam 5
void Janela5(){
    int lin, col;system("color eF");
    for ( lin = 0; lin <= 36; lin++ ){
        for ( col = 0; col <= 80; col++ ){
            gotoxy( col, lin );
            if ( lin == 2 ){textbackground(BLACK);printf( " ");}
            if ( col == 1 ){textbackground(BLACK);printf(" ");}
            if ( lin == 36 ){textbackground(BLACK);}
            if ( col == 80 ){textbackground(BLACK);printf(" ");}
        }
    }textbackground( BLACK );
}
void inicio( ){system ("title TROCANDO LINHAS E COLUNAS DE UMA MATRIZ");
int i;do{Janela5();
textcolor(LIGHTRED);gotoxy(23,3); printf("TROCANDO LINHAS E COLUNAS DE UMA MATRIZ");
textcolor(YELLOW);gotoxy(25,10);printf("Programa desenvolvido por:");
textcolor(LIGHTCYAN);gotoxy(52,10);printf("Samuel Lima");
textcolor(LIGHTGREEN);gotoxy(34,12);printf("sa_sp10@hotmail.com");
textcolor(LIGHTBLUE);gotoxy(24,14);printf("DIGITE    PARA SAIR OU   PARA CONTINUAR  ");
textcolor(LIGHTRED);gotoxy(32,14);printf("1");textcolor(LIGHTRED);gotoxy(47,14);printf("2");
gotoxy(41,16);
scanf("%d",&i);fflush(stdin);
if( i == 1 ){textcolor(LIGHTRED);gotoxy(35,20);printf ("MUITO OBRIGADO");Sleep(1800);exit(0);}
if( i == 2 ){system("cls"); return;}
else {textcolor(LIGHTRED);gotoxy(37,20);printf ("\aOPÇÃO ERRADA");Sleep(1800);system("cls");}
}
while(i);
}
int main(){
    inicio( );
    int i, j;int Vet_temp [ tam ], Vet_emp [ tam ];
    int numeros [ tam ] [ tam ] = {
            { 1, 2, 3, 4, 5 },
            { 6, 7, 8, 9, 10 },
            { 11, 12, 13, 14, 15 },
            { 16, 17, 18, 19, 20 },
            { 21, 22, 23, 24, 25 }};Janela5();
    textcolor(LIGHTRED);gotoxy(23,3); printf("TROCANDO LINHAS E COLUNAS DE UMA MATRIZ");
    textcolor(LIGHTBLUE);gotoxy(7, 9);printf("Abaixo a Matriz original");
    textcolor(LIGHTGRAY);
    for( i = 0; i < tam; i++ ){
        gotoxy( 6, i + 11 );
        for( j = 0; j < tam; j++ ){
            printf(" %3d ", numeros [ i ] [ j ] );
        }
    }
    for ( i = 0; i < tam; i++ ){
        Vet_temp [ i ] = numeros [ 0 ] [ i ];
        Vet_emp [ i ] = numeros [ 2 ] [ i ];
    }
    textcolor(LIGHTBLUE);gotoxy(5, 5);printf("Abaixo a linha [ 0 ] da Matriz");
    gotoxy(7, 7);textcolor(LIGHTRED);
    for ( i = 0; i < tam; i++ ){
        printf(" %3d ", Vet_temp [ i ] );
        numeros [ 2 ] [ i ] = Vet_temp [ i ];
    }
    getche();
    textcolor(LIGHTBLUE);gotoxy(43, 5);printf("Abaixo a linha [ 2 ] da Matriz");
    gotoxy(45, 7);textcolor(LIGHTRED);
    for ( i = 0; i < tam; i++ ){
        printf(" %3d ", Vet_emp [ i ] );
        numeros [ 0 ] [ i ] = Vet_emp [ i ];
    }
    getche();
    textcolor(LIGHTBLUE);gotoxy(45, 9);printf("Abaixo a Matriz modificada");
    textcolor(LIGHTGRAY);
    for( i = 0; i < tam; i++ ){
        gotoxy( 46, i + 11 );
        for( j = 0; j < tam; j++ ){
            printf(" %3d ", numeros [ i ] [ j ] );
        }
    }
    Sleep(1000);textcolor(LIGHTGREEN);
    gotoxy(32,22);printf("PRESSIONE QUALQUER TECLA");getche();
    system("cls");Janela5();
    textcolor(LIGHTRED);gotoxy(23,3); printf("TROCANDO LINHAS E COLUNAS DE UMA MATRIZ");
    textcolor(LIGHTBLUE);gotoxy(7, 9);printf("Abaixo a Matriz modificada");
    textcolor(LIGHTGRAY);
    for( i = 0; i < tam; i++ ){
        gotoxy( 7, i + 11 );
        for( j = 0; j < tam; j++ ){
            printf(" %3d ", numeros [ i ] [ j ] );
        }
    }

    for ( i = 0; i < tam; i++ ){
        Vet_temp [ i ] = numeros [ i ] [ 0 ];
        Vet_emp [ i ] = numeros [ i ] [ 2 ];
    }
    textcolor(LIGHTBLUE);gotoxy(5, 5);printf("Abaixo a Coluna [ 0 ] da Matriz");
    gotoxy(8, 7);textcolor(LIGHTRED);
    for ( i = 0; i < tam; i++ ){
        printf(" %3d ", Vet_temp [ i ] );
        numeros [ i ] [ 2 ] = Vet_temp [ i ];
    }
    getche();
    textcolor(LIGHTBLUE);gotoxy(43, 5);printf("Abaixo a Coluna [ 2 ] da Matriz");
    gotoxy(46, 7);textcolor(LIGHTRED);
    for ( i = 0; i < tam; i++ ){
        printf(" %3d ", Vet_emp [ i ] );
        numeros [ i ] [ 0 ] = Vet_emp [ i ];
    }
    getche();
    textcolor(LIGHTBLUE);gotoxy(45, 9);printf("Abaixo a Matriz modificada");
    textcolor(LIGHTGRAY);
    for( i = 0; i < tam; i++ ){
        gotoxy( 46, i + 11 );
        for( j = 0; j < tam; j++ ){
            printf(" %3d ", numeros [ i ] [ j ] );
        }
    }
    Sleep(1000);textcolor(LIGHTGREEN);
    gotoxy(36,22);printf("MUITO OBRIGADO");
    getche();
}

Nenhum comentário:

Postar um comentário

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