Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #define MAX_LEN 32
- #define Begin {
- #define End }
- #define a aarray[i][j]
- #define b aarray[j][i]
- void file_to_matrix(FILE *, int (*)[MAX_LEN], int *, int *);
- void matrix_to_file(int (*)[MAX_LEN], int, int, FILE *);
- void transpose_matrix(int (*)[MAX_LEN], int, int);
- main()
- Begin
- int array[MAX_LEN][MAX_LEN], rows=0, columns=0;
- FILE * fi, *fo;
- fi=fopen("matrix.dat", "r");
- fo=fopen("matrix_output.dat", "w");
- file_to_matrix(fi, array, &rows, &columns);
- transpose_matrix(array, rows, columns);
- matrix_to_file(array, columns, rows, fo);
- fclose(fi);
- fclose(fo);
- return 0;
- End
- void file_to_matrix(FILE * fp, int (*aarray)[MAX_LEN], int *m, int *n)
- Begin
- int j=0;
- char c;
- while(feof(fp)==0)
- Begin
- c=fgetc(fp);
- if(isdigit(c))
- Begin
- aarray[*m][j++]=c-'0';
- ++*n;
- End
- else if(c=='\n')
- Begin
- ++*m;
- j=0;
- End
- End
- ++*m;
- (*n)/=(*m);
- End
- void transpose_matrix(int (*aarray)[MAX_LEN], int m, int n)
- Begin
- int i, j;
- for(i=0;i<m;i++)
- for(j=0;j<n;j++)
- (i>=j)?:(a^=b^=a^=b);
- End
- void matrix_to_file(int (*aarray)[MAX_LEN], int m, int n, FILE * fp)
- Begin
- int i, j;
- for(i=0;i<m;i++)
- Begin
- for(j=0;j<n;j++)
- fprintf(fp, "%1d\t", aarray[i][j]);
- fprintf(fp, "\n");
- End
- End
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement