MatteB_01

sottomatrici

May 13th, 2021
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #define FILENAME "input.txt"
  3. #define MAXLEN 20
  4. void leggiMatrice(FILE *fin, int nr, int nc, int matrice[nr][nc]);
  5. void sottomatrici(int nr, int nc, int matrice[nc][nr], int dim);
  6. void copiaMatrice(int dim, int matrice0[dim][dim],int  matrice1[dim][dim]);
  7.  
  8. int main(void){
  9.  
  10.     FILE *fin;
  11.     int numRighe, numColonne;
  12.     unsigned int dimensione;
  13.     char separatore;
  14.  
  15.     if ((fin = fopen(FILENAME, "r")) == NULL) {
  16.         printf("Errore nell' apertura del file.\n");
  17.         return 1;
  18.     }
  19.  
  20.     fscanf(fin, "%d", &numRighe);
  21.     separatore = fgetc(fin);
  22.     fscanf(fin, "%d", &numColonne);
  23.     separatore = fgetc(fin);
  24.  
  25.     if (numColonne > MAXLEN || numRighe > MAXLEN) {
  26.         printf("le dimensioni eccedono il massimo di 20");
  27.         return 2;
  28.     }
  29.  
  30.     int matrice[numRighe][numColonne];
  31.     leggiMatrice(fin, numRighe, numColonne, matrice);
  32.  
  33.  
  34.     printf("inserire le dimensioni delle sottomatrici\n");
  35.     scanf("%d", &dimensione);
  36.  
  37.     while (dimensione <= numColonne && dimensione <= numRighe){
  38.         sottomatrici(numRighe, numColonne, matrice, dimensione);
  39.  
  40.         printf("inserire le dimensioni delle sottomatrici\n");
  41.         scanf("%d", dimensione);
  42.  
  43.     }
  44.     printf("dimensione inserita non valida");
  45.     return 0;
  46.  
  47. }
  48.  
  49.  
  50. void leggiMatrice(FILE *fin, int nr, int nc, int matrice[nr][nc]){
  51.     int i, j;
  52.     char separatore;
  53.     for (i=0;i<nr;i++){
  54.         for (j=0;j<nc;j++){
  55.             fscanf(fin, "%d", &matrice[i][j]);
  56.             separatore = fgetc(fin);
  57.         }
  58.     }
  59.  
  60. }
  61.  
  62. void sottomatrici(int nr, int nc, int matrice[nc][nr], int dim){
  63.     int maxSum = 0, sum, i, j, x , y, subMatrix[dim][dim], maxMatrix[dim][dim];
  64.  
  65.     for (i=0;i<nr;i++){
  66.         for (j=0;j<nc;j++){
  67.  
  68.             //per ogni posizione nella matrice, verifico che vi sia una sottomatrice controllando la distanza dai bordi
  69.             if ((nr-i)>(dim-1) && (nc-j)>(dim-1)){
  70.                 sum = 0;
  71.  
  72.                 //ciclo sulla sottomatrice per stamparla e verifico che sia la più grande
  73.                 for (y=i;y<(i+dim-1);y++){
  74.                     for (x=j;x<(j+dim-1);x++){
  75.  
  76.                         sum += matrice[y][x];
  77.                         printf("%d ", matrice[y][x]);
  78.  
  79.                         subMatrix[y-i][x-j] = matrice[y][x];
  80.  
  81.                     }
  82.                     printf('\n');
  83.                 }
  84.                 printf('\n');
  85.  
  86.                 if (sum >= maxSum){
  87.                     maxSum = sum;
  88.                     copiaMatrice(dim,subMatrix,maxMatrix);
  89.                 }
  90.             }
  91.         }
  92.     }
Advertisement
Add Comment
Please, Sign In to add comment