1 / 3

Un Programa para Crear un Archivo con una Matríz mXn de enteros dados

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;

atara
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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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); }

  2. 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 arreglo if((Astorage = (int *)malloc(m * n * sizeof(int))) == NULL) { printf( "\n*** no hay memoria ***\n"); exit(); } //Asignar memoria para los apuntadores a las filas if((A = (int **)malloc(m * sizeof(int *))) == NULL) { printf("\n*** no hay memoria ***\n"); exit(); }

  3. 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); } • }

More Related