Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <math.h>
  5. #define N 4
  6.  
  7. int main ()
  8. {
  9.     FILE *F1;
  10.     FILE *F2;
  11.     int matr[N][N], a, b, s1 = 0, s2 = 0;
  12.  
  13.     F1 = fopen("F1.txt", "w");
  14.     for(int i = 0; i < N; i++)
  15.     {
  16.         for(int j = 0; j < N; j++)
  17.         {
  18.             matr[i][j] = rand() % 20 + (-10);
  19.             printf("%4d", matr[i][j]);
  20.             fprintf(F1, "%d ", matr[i][j]);
  21.         }
  22.         printf("\n");
  23.         fprintf(F1, "\n");
  24.     }
  25.     fclose(F1);
  26.  
  27.     F1 = fopen("F1.txt", "r");
  28.     for(int i = 0; i < N; i++)
  29.     {
  30.         for(int j = 0; j < N; j++)
  31.             fscanf (F1, "%d", &matr[i][j]);
  32.     }
  33.     fclose(F1);
  34.  
  35.     F2 = fopen("F2.txt", "w");
  36.     for(int i = 0; i < N; i++)
  37.     {  
  38.         for(a = 0; a < i; a++)
  39.         {
  40.             if(matr[i][a] > 0)
  41.                 s1 += matr[i][a];
  42.         }
  43.     }
  44.     fprintf(F2, "Suma > 0 pid: %d\n", s1);
  45.     printf("Suma > 0 pid: %d\n", s1);
  46.  
  47.     for(int j = 0; j < N; j++)
  48.     {  
  49.         for(b = 0; b < j; b++)
  50.         {
  51.             if(matr[b][j] < 0)
  52.                 s2 += matr[b][j];
  53.         }
  54.     }
  55.     fprintf(F2, "Suma < 0 nad: %d\n", s2);
  56.     printf("Suma < 0 nad: %d \n", s2);
  57.     fclose(F2);
  58.     getch();
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement