MatteB_01

sottomatrici

May 13th, 2021
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.06 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[nr][nc], 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[nr][nc], 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);y++){
  74.  
  75.                     for (x=j;x<(j+dim);x++){
  76.                         sum += matrice[y][x];
  77.  
  78.                         printf("%d ", matrice[y][x]);
  79.  
  80.                         subMatrix[y-i][x-j] = matrice[y][x];
  81.                         printf("ciao");
  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