Upload
atara
View
32
Download
0
Embed Size (px)
DESCRIPTION
Un Programa para Crear un Archivo con una Matríz mXn de enteros dados. * #include #include /******************************************************************************/ intmain(int argc, char *argv[]) {inti, j; intm,n; FILE*fp; int*Astorage; - PowerPoint PPT Presentation Copyright Complaint Adult Content Flag as Inappropriate Report This Download Presentation Un Programa para Crear un Archivo con una Matríz mXn de enteros dados An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - Presentation Transcript Un Programa para Crear un Archivo con una Matríz mXn de enteros dados * #include #include /******************************************************************************/ intmain(int argc, char *argv[]) {inti, j; intm,n; FILE*fp; int*Astorage; int**A; int x; if(argc != 4) {printf("\nDebe ser: generar "); printf("\ndonde la matriz es mxn"); printf("\n"); exit(1); }
Citation preview
Un Programa para Crear un Archivo con una Matríz mXn de
enteros dados• *
#include <stdio.h>#include <stdlib.h>/******************************************************************************/int main(int argc, char *argv[]){ int i, j;
int m,n;FILE *fp;int *Astorage;int **A;int x;if(argc != 4){ printf("\nDebe ser: generar <m> <n> <archivo> ");
printf("\ndonde la matriz es mxn");printf("\n");exit(1);
}
Programa generar(cont) m=atoi(argv[1]); n=atoi(argv[2]); //Abrir archivo para escribir
if((fp = fopen(argv[2], "w")) == NULL){ printf("\n*** no se puede escribir en archivo %s ***\n", argv[2]);
exit();}
/* escribir las dimensiones n y n en el archivo */fwrite(&m, sizeof(int), 1, fp);fwrite(&n, sizeof(int), 1, fp);
// Asignar memoria para almacenar el arregloif((Astorage = (int *)malloc(m * n * sizeof(int))) == NULL){ printf( "\n*** no hay memoria ***\n");
exit();}
//Asignar memoria para los apuntadores a las filasif((A = (int **)malloc(m * sizeof(int *))) == NULL){ printf("\n*** no hay memoria ***\n");
exit();}
•
Program generar (cont)/* inicializar arreglo de apuntadores */for(i = 0; i <m; ++i)
A[i] = &Astorage[i * n];/* Entrar la matriz desde el teclado*/
/* set all values */for(i = 0; i < m; ++i)
for(j = 0; j < n; ++j){
printf("A[%d][%d]=",i,j); scanf("%d",&A[i][j]);
}
/* escribir el arreglo en el archivo */fwrite(Astorage, sizeof(int), m * n, fp);
fclose(fp); free(Astorage); free(A);
return(0); }• }