Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.79 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main() {
  4.  
  5.     int mat[10][10];
  6.     int mat2[10][10];
  7.     int summa[10][10];
  8.  
  9.     int x, y;
  10.  
  11.     FILE *luku;
  12.     FILE *luku2;
  13.     FILE *kirj_tied;
  14.  
  15.     if ((luku = fopen("mata.txt", "r")) == NULL) {
  16.         printf("Tiedoston avaus epäonnistui!");
  17.     } else {
  18.         for (y = 0; y < 10; y++) {
  19.             fscanf(luku, "\n");
  20.             for (x = 0; x < 10; x++) {
  21.                 mat[y][x] = 0;
  22.                 if (x == 9) {
  23.                     fscanf(luku, "%d", &mat[y][x]);
  24.                 } else {
  25.                     fscanf(luku, "%d ", &mat[y][x]);
  26.                 }
  27.             }
  28.         }
  29.     }
  30.  
  31.     fclose(luku);
  32.  
  33.  
  34.     if ((luku2 = fopen("matb.txt", "r")) == NULL) {
  35.         printf("Tiedoston avaus epäonnistui!");
  36.     } else {
  37.         for (y = 0; y < 10; y++) {
  38.             for (x = 0; x < 10; x++) {
  39.                 mat2[y][x] = 0;
  40.                 if (x == 9) {
  41.                     fscanf(luku2, "%d", &mat2[y][x]);
  42.                 } else {
  43.                     fscanf(luku2, "%d ", &mat2[y][x]);
  44.                 }
  45.             }
  46.         }
  47.     }
  48.  
  49.     fclose(luku2);
  50.  
  51.  
  52.     for (y = 0; y < 10; y++) {
  53.  
  54.         for (x = 0; x < 10; x++) {
  55.  
  56.             summa[y][x] = mat[y][x] + mat2[y][x];
  57.  
  58.         }
  59.     }
  60.  
  61.  
  62.     if ((kirj_tied = fopen("summa.usr", "w")) == NULL) {
  63.         printf("Tiedoston luku epäonnistui!");
  64.     } else {
  65.         for (y = 0; y < 10; y++) {
  66.             for (x = 0; x < 10; x++) {
  67.                 if (x == 9) {
  68.                     fprintf(kirj_tied, "%d\n", summa[y][x]);
  69.                 } else {
  70.                     fprintf(kirj_tied, "%d ", summa[y][x]);
  71.                 }
  72.             }
  73.  
  74.         }
  75.  
  76.         printf("Matriisien summa on laskettu tiedostoon summa.usr.\n");
  77.     }
  78.  
  79.  
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement