Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- int printf_Matrix(double* M, int n, int m, FILE *fp);//fp == NULL -> into console
- int printf_Matrix(double* M, int n, int m, FILE *fp)
- {
- for (int i = 0; i < n; i++)
- {
- for(int j = 0; j < m; j++)
- {
- fprintf(fp, "%lf ", M[i*m + j]);
- }
- fprintf(fp, "\n");
- }
- return 0;
- }
- int scanf_Matrix(double* M, int n, int m, FILE *fp);//n - strings, m - elements in one string
- int scanf_Matrix(double* M, int n, int m, FILE *fp)//n - strings, m - elements in one string
- {
- int st;
- for (int i = 0; i < n; i++)
- {
- for(int j = 0; j < m; j++)
- {
- st = fscanf(fp, "%lf", &M[i*m + j]);
- if(st != 1)
- {
- if (st == 0)
- {
- printf("Error: Not correct data!\n");
- return -1;
- }
- else if(st == -1)
- {
- printf("Error: Not enough data!\n");
- return -1;
- }
- else
- {
- printf("Error: Unexepted data input error!\n");
- return -1;
- }
- }
- }
- }
- return 0;
- }
- int main(void)
- {
- FILE *fin, *fout;
- int n, m;
- double* M;
- fin = fopen ("input.txt", "r");
- fout = fopen ("output.txt", "w");
- if(!fin)
- {
- printf ("Error: cannot open file!\n");
- return(-1);
- }
- if(!fscanf(fin, "%d", &n) || !fscanf(fin, "%d", &m))
- {
- printf("Error: not correct \'n\'!");
- return -1;
- }
- M = (double *)malloc(n*m*sizeof(double));
- if(scanf_Matrix(M, n, m, fin))
- {
- return -1;
- }
- printf_Matrix(M, n, m, fout);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement