MatteB_01

sottomatrici

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