domingo, 9 de março de 2014

Matriz Dinamica Para Matriz Estática

Esta Matriz é carregada por um contador, com seus limites entre 1 e 100, (Números Inteiros)
Ela recebe alocação dinâmica de memória, e passa os mesmos valores para uma Matriz Estática.
No final, salva os números num arquivo de texto.


Veja o código abaixo:

#include <stdio.h>
#include <conio.h>
#define tam 10
void Janela5(){
    int lin, col;
    for ( lin = 0; lin <= 25; lin++ ){
        for ( col = 0; col <= 80; col++ ){
            gotoxy( col, lin );
            if ( lin == 2 ){textbackground(LIGHTBLUE);printf( " ");}
            if ( col == 1 ){textbackground(LIGHTBLUE);printf(" ");}
            if ( lin == 25 ){textbackground(LIGHTBLUE);}
            if ( col == 80 ){textbackground(LIGHTBLUE);printf(" ");}
        }
    }textbackground( BLACK );
}
int main(){Janela5();int matriz [tam] [tam];
system ("title MATRIZ DINAMICA PARA MATRIZ ESTATICA");
int **Vet;
Vet = ( int** ) malloc ( tam * sizeof ( int** ) );
int i, j, f = 0, cont = 0;
textcolor(LIGHTRED);gotoxy(26,3);printf("MATRIZ DINAMICA PARA MATRIZ ESTATICA ");
textcolor(LIGHTBLUE);gotoxy(29,5);printf("IMPRIMINDO A MATRIZ DINAMICA");
textcolor(YELLOW);
for( i = 0; i < tam; i++ ){
    Vet [ i ] = ( int* ) malloc ( tam * sizeof ( int* ) );
    gotoxy(18, i + 8);
    for( j = 0; j < tam; j++ ){
        cont++;
        Vet [ i ] [ j ]  = cont;
        printf(" %3d ",  Vet [ i ] [ j ] );
        matriz [ i ] [ j ] = Vet [ i ] [ j ] ;
        f = Vet [ i ] [ j ];
    }
    printf("\n");
}
textcolor(LIGHTBLUE);gotoxy(24,19);
printf ("Quantidade de elementos na Matriz:  %d", f );
getche();clrscr();Janela5();
textcolor(LIGHTRED);gotoxy(26,3);printf("MATRIZ DINAMICA PARA MATRIZ ESTATICA");
textcolor(LIGHTBLUE);gotoxy(29,5);printf("IMPRIMINDO A MATRIZ ESTATICA");
textcolor(LIGHTGREEN);
FILE *arq = fopen ("matriz.txt", "w");
for( i = 0; i < tam; i++ ){
    gotoxy(18, i + 8);
    for( j = 0; j < tam; j++ ){
        printf(" %3d ",     matriz [ i ] [ j ] );
        f = Vet [ i ] [ j ];
        fprintf(arq," %d ",  matriz [ i ] [ j ] );
    }
}
textcolor(LIGHTBLUE);gotoxy(24,19);
printf ("Quantidade de elementos na Matriz:  %d", f );
fclose( arq );
getche();
}

Nenhum comentário:

Postar um comentário

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