Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define FILENAME "input.txt"
- #define MAXLEN 20
- void leggiMatrice(FILE *fin, int nr, int nc, int matrice[nr][nc]);
- void sottomatrici(int nr, int nc, int matrice[nr][nc], int dim);
- void copiaMatrice(int dim, int matrice0[dim][dim],int matrice1[dim][dim]);
- int main(void){
- FILE *fin;
- int numRighe, numColonne;
- unsigned int dimensione;
- char separatore;
- if ((fin = fopen(FILENAME, "r")) == NULL) {
- printf("Errore nell' apertura del file.\n");
- return 1;
- }
- fscanf(fin, "%d", &numRighe);
- separatore = fgetc(fin);
- fscanf(fin, "%d", &numColonne);
- separatore = fgetc(fin);
- if (numColonne > MAXLEN || numRighe > MAXLEN) {
- printf("le dimensioni eccedono il massimo di 20");
- return 2;
- }
- int matrice[numRighe][numColonne];
- leggiMatrice(fin, numRighe, numColonne, matrice);
- printf("inserire le dimensioni delle sottomatrici\n");
- scanf("%d", &dimensione);
- while (dimensione <= numColonne && dimensione <= numRighe){
- sottomatrici(numRighe, numColonne, matrice, dimensione);
- printf("inserire le dimensioni delle sottomatrici\n");
- scanf("%d", dimensione);
- }
- printf("dimensione inserita non valida");
- return 0;
- }
- void leggiMatrice(FILE *fin, int nr, int nc, int matrice[nr][nc]){
- int i, j;
- char separatore;
- for (i=0;i<nr;i++){
- for (j=0;j<nc;j++){
- fscanf(fin, "%d", &matrice[i][j]);
- separatore = fgetc(fin);
- }
- }
- }
- void sottomatrici(int nr, int nc, int matrice[nr][nc], int dim){
- int maxSum = 0, sum, i, j, x , y, subMatrix[dim][dim], maxMatrix[dim][dim];
- for (i=0;i<nr;i++){
- for (j=0;j<nc;j++){
- //per ogni posizione nella matrice, verifico che vi sia una sottomatrice controllando la distanza dai bordi
- if ((nr-i)>(dim-1) && (nc-j)>(dim-1)){
- sum = 0;
- //ciclo sulla sottomatrice per stamparla e verifico che sia la più grande
- for (y=i;y<(i+dim);y++){
- for (x=j;x<(j+dim);x++){
- sum += matrice[y][x];
- printf("%d ", matrice[y][x]);
- subMatrix[y-i][x-j] = matrice[y][x];
- printf("ciao");
- }
- printf('\n');
- }
- printf('\n');
- if (sum >= maxSum){
- maxSum = sum;
- copiaMatrice(dim,subMatrix,maxMatrix);
- }
- }
- }
- }
- printf("La sottomatrice con somma degli elementi massima(%d) è: \n", maxSum);
- for (i=0;i<dim;i++){
- for (j=0;j<dim;j++){
- printf("%d ",maxMatrix[i][j]);
- ;
- }
- printf('\n');
- }
- }
- void copiaMatrice(int dim, int matrice0[dim][dim],int matrice1[dim][dim]){
- int i, j;
- for (i=0;i<dim;i++){
- for (j=0;j<dim;j++){
- matrice1[i][j] = matrice0[i][j];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment