SHARE
TWEET

Untitled

a guest May 23rd, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5.  
  6.  
  7.  
  8. int printf_Matrix(double* M, int n, int m, FILE *fp);//fp == NULL -> into console
  9. int printf_Matrix(double* M, int n, int m, FILE *fp)
  10. {
  11.  
  12.     for (int i = 0; i < n; i++)
  13.     {
  14.         for(int j = 0; j < m; j++)
  15.         {
  16.             fprintf(fp, "%lf ", M[i*m + j]);
  17.         }
  18.         fprintf(fp, "\n");
  19.     }
  20.     return 0;
  21.  
  22. }
  23.  
  24. int scanf_Matrix(double* M, int n, int m, FILE *fp);//n - strings, m - elements in one string
  25. int scanf_Matrix(double* M, int n, int m, FILE *fp)//n - strings, m - elements in one string
  26. {
  27.     int st;
  28.     for (int i = 0; i < n; i++)
  29.     {
  30.         for(int j = 0; j < m; j++)
  31.         {
  32.             st = fscanf(fp, "%lf", &M[i*m + j]);
  33.             if(st != 1)
  34.             {
  35.                 if (st == 0)
  36.                 {
  37.                     printf("Error: Not correct data!\n");
  38.                     return -1;
  39.                 }
  40.                 else if(st == -1)
  41.                 {
  42.                     printf("Error: Not enough data!\n");
  43.                     return -1;
  44.                 }
  45.                 else
  46.                 {
  47.                     printf("Error: Unexepted data input error!\n");
  48.                     return -1;
  49.                 }
  50.             }
  51.         }
  52.     }
  53.     return 0;
  54. }
  55.  
  56.  
  57. int main(void)
  58. {
  59.     FILE *fin, *fout;
  60.     int n, m;
  61.     double* M;
  62.     fin = fopen ("input.txt", "r");
  63.     fout = fopen ("output.txt", "w");
  64.     if(!fin)
  65.     {
  66.         printf ("Error: cannot open file!\n");
  67.         return(-1);
  68.     }
  69.     if(!fscanf(fin, "%d", &n) || !fscanf(fin, "%d", &m))
  70.     {
  71.         printf("Error: not correct \'n\'!");
  72.         return -1;
  73.     }
  74.     M = (double *)malloc(n*m*sizeof(double));
  75.     if(scanf_Matrix(M, n, m, fin))
  76.     {
  77.         return -1;
  78.     }
  79.     printf_Matrix(M, n, m, fout);
  80.  
  81. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top