// Input a 3x3 matrix, and output all 2x2 square matrices in it. The rows and // columns of the 2x2 matrices must be adjacent in the original matrix. For each // such matrix, also output the sum of its elements // // Author:rahule@cse.iitk.ac.in #include main() { int a[3][3],i,j,sum=0; printf("enter a 3x3 matrix\n"); for(i=0;i<3;i++) //inputs the 3x3 matrix for(j=0;j<3;j++) scanf("%d",&a[i][j]); printf("All possible 2x2 matrices that can be generated are,\n"); for(i=0;i<2;i++) for(j=0;j<2;j++) //for each element a(i,j),do the following { sum=a[i][j]+a[i][j+1]+a[i+1][j]+a[i+1][j+1]; printf("%d %d\n%d %d\nSUM:%d\n\n",a[i][j],a[i][j+1],a[i+1][j],a[i+1][j+1], sum ); //takes 2x2 matrix starting from (i,j),ie (i,j),(i,j+1),(i+1,j),(i+1,j+1) and prints it togather with its SUM printf("%d %d\n%d %d\nSUM:%d\n\n",a[i+1][j],a[i+1][j+1],a[i][j],a[i][j+1], sum ); //swap the rows of 2x2 matrix starting from (i,j) and prints it printf("%d %d\n%d %d\nSUM:%d\n\n",a[i][j+1],a[i][j],a[i+1][j+1],a[i+1][j], sum ); //swap the columns of 2x2 matrix starting from (i,j) and prints it } }