How to print all square submatrices of square matrix in C? - c

Please, help me to find and print all square submatrices of square matrix from big to small square matrices in C programming language
I wrote code that works wrong:
int main() {
int mtrx_size = 8;
int mat[8][8] = {
{ 1, 2, 3, 4, 5, 6, 7, 8},
{ 9,10,11,12,13,14,15,16},
{17,18,19,20,21,22,23,24},
{25,26,27,28,29,30,31,32},
{33,34,35,36,37,38,39,40},
{41,42,43,44,45,46,47,48},
{49,50,51,52,53,54,55,56},
{57,58,59,60,61,62,63,64}
};
int i,j;
int sub_mtrx_size;
for(sub_mtrx_size = mtrx_size; sub_mtrx_size > 1 ; sub_mtrx_size--)
{
for(i = 0; i < sub_mtrx_size; i++)
{
for(j = 0; j < sub_mtrx_size; j++)
{
printf("%3d ", mat[i][j]);
}
printf("\n");
}
printf("\n");
}
return 0;
Here I need to find all 8x8, 7x7, 6x6, 5x5, 4x4, 3x3 and 2x2 submatrices.

Your code was just printing a single sub-matrix for each size, positioned in the upper-left corner of the matrix. You need to add i and j offsets to get the sub-matrices at all positions:
#include <stdio.h>
int main() {
int mtrx_size = 8;
int mat[8][8] = {
{ 1, 2, 3, 4, 5, 6, 7, 8},
{ 9,10,11,12,13,14,15,16},
{17,18,19,20,21,22,23,24},
{25,26,27,28,29,30,31,32},
{33,34,35,36,37,38,39,40},
{41,42,43,44,45,46,47,48},
{49,50,51,52,53,54,55,56},
{57,58,59,60,61,62,63,64}
};
int i, j, ioff, joff, off_cnt;
int sub_mtrx_size;
for(sub_mtrx_size = mtrx_size; sub_mtrx_size > 1 ; sub_mtrx_size--) {
off_cnt = mtrx_size - sub_mtrx_size + 1;
for (ioff = 0; ioff < off_cnt; ioff++) {
for (joff = 0; joff < off_cnt; joff++) {
for (i = 0; i < sub_mtrx_size; i++) {
for (j = 0; j < sub_mtrx_size; j++) {
printf("%3d ", mat[i+ioff][j+joff]);
}
printf("\n");
}
printf("\n");
}
}
}
return 0;
}

Java implementation for a general nxm matrix:
private static void printSubMatrix(int[][] mat) {
int rows=mat.length;
int cols=mat[0].length;
//prints all submatrix greater than or equal to 2x2
for (int subRow = rows; subRow >= 2; subRow--) {
int rowLimit = rows - subRow + 1;
for (int subCol = cols; subCol >= 2; subCol--) {
int colLimit = cols - subCol + 1;
for (int startRow = 0; startRow < rowLimit; startRow++) {
for (int startCol = 0; startCol < colLimit; startCol++) {
for (int i = 0; i < subRow; i++) {
for (int j = 0; j < subCol; j++) {
System.out.print(mat[i + startRow][j + startCol] + " ");
}
System.out.print("\n");
}
System.out.print("\n");
}
}
}
}
}

#include <stdio.h>
int main() {
int mtrx_size = 8;
int mat[8][8] = {
{ 1, 2, 3, 4, 5, 6, 7, 8},
{ 9,10,11,12,13,14,15,16},
{17,18,19,20,21,22,23,24},
{25,26,27,28,29,30,31,32},
{33,34,35,36,37,38,39,40},
{41,42,43,44,45,46,47,48},
{49,50,51,52,53,54,55,56},
{57,58,59,60,61,62,63,64}
};
int i, j, ioff, joff, off_cnt;
int sub_mtrx_size;
/* if we make terminating condition sub_mtrx_size>=1 then we will have all
possible square sub matrices */
for(sub_mtrx_size = mtrx_size; sub_mtrx_size >= 1 ; sub_mtrx_size--) {
off_cnt = mtrx_size - sub_mtrx_size + 1;
for (ioff = 0; ioff < off_cnt; ioff++) {
for (joff = 0; joff < off_cnt; joff++) {
for (i = 0; i < sub_mtrx_size; i++) {
for (j = 0; j < sub_mtrx_size; j++) {
printf("%3d ", mat[i+ioff][j+joff]);
}
printf("\n");
}
printf("\n");
}
}
}
return 0;
}

Related

In C, I'm having trouble getting an Index sorted High to Low and vice versa

I'm working on a project involving a preset array:
primaryArray[8] = {8, 4, 2, 16, 32, 124, 64, 256};
Im calling a function before the main, its not quite working. Here is the code.
void lowhighSort(int primaryArray[], int arrayLength) {
int i;
int a;
int indexMin;
for (i = 0; i < arrayLength - 1; ++i){
indexMin = i;
for (a = (i + 1) + 1; a < arrayLength; ++a) {
if (primaryArray[a] < primaryArray[indexMin]){
indexMin = a;
}
simpleSwap(&primaryArray[indexMin], &primaryArray[i]);
}
}
}
void highlowSort(int primaryArray[], int arrayLength) {
int i;
int a;
int indexMin;
for (i = 0; i < arrayLength - 1; ++i){
indexMin = i;
for (a = i + 1; a < arrayLength; ++a) {
if(primaryArray[a] > primaryArray[indexMin])
indexMin = a;
simpleSwap(&primaryArray[indexMin], &primaryArray[i]);
}
}
}
My "simpleSwap" might be the mistake, when learning I didn't quite understand entirely what the * did. Regardless, here it is as well:
void simpleSwap(int* x, int* y) {
int temp = *x;
*x = *y;
*y = temp;
}
*edit
This is part of a "larger" code so I'm not entirely sure if perhaps the error is elsewhere.
#include <stdio.h>
#include <stdlib.h>
void simpleSwap(double* x, double* y) {
double temp = *x;
*x = *y;
*y = temp;
}
double largest(double Array[], double Length) {
double largestNum = Array[0];
for (int i = 1; i < Length; ++i) {
if(largestNum < Array[i]) {
largestNum = Array[i];
}
}
return largestNum;
}
double smallest(double Array[], double Length) {
double smallestNum = Array[0];
for (int i = 1; i < Length; ++i) {
if(smallestNum > Array[i]) {
smallestNum = Array[i];
}
}
return smallestNum;
}
void lowhighSort(double Array[], double Length) {
int a;
int indexMin;
for (int i = 0; i < Length; ++i){
indexMin = i;
for (a = (i + 1); a < Length; ++a) {
if (Array[a] < Array[indexMin]){
indexMin = a;
simpleSwap(&Array[indexMin], &Array[i]);
}
}
}
}
void highlowSort(double Array[], double Length) {
int a;
int indexMin;
for (int i = 0; i < Length; ++i){
indexMin = i;
for (a = (i + 1); a < Length; ++a) {
if(Array[a] > Array[indexMin]){
indexMin = a;
simpleSwap(&Array[indexMin], &Array[i]);
}
}
}
}
float arrayAverage (double Array[], double Length) {
int i;
double arrayAverage = 0;
for (i = 0; i < Array[i]; ++i) {
arrayAverage = Array[i] + arrayAverage;
}
return arrayAverage;
}
int main()
{
double primaryArray[8] = {8, 4, 2, 16, 32, 124, 64, 256};
double arrayLength = sizeof(primaryArray)/sizeof(primaryArray[0]);
double ancillaryArray[10] = {8, 4, 2, 16, 32, 124, 64, 256};
printf("Primary Array:\n");
printf("\nLargest Array Element: %.1lf\n", largest(primaryArray, arrayLength));
printf("Smallest Array Element: %.1lf\n\n", smallest(primaryArray, arrayLength));
lowhighSort(primaryArray, arrayLength);
printf("Array Ascending Order: ");
for(int i = 0; i < 8; ++i){
printf("%.1lf, ", primaryArray[i]);
}
printf("\n");
highlowSort(primaryArray, arrayLength);
printf("Array Descending Order: ");
for(int i = 0; i < 8; ++i){
printf("%.1lf, ", primaryArray[i]);
}
printf("\n");
printf("\nArray Element Average: %.3lf", (arrayAverage(primaryArray, arrayLength))/8);
int ancillaryarrayLength = sizeof(ancillaryArray)/sizeof(ancillaryArray[0]);
printf("\n\nPlease Finish Ancillary Array (Max 10 Elements)\nCurrent Elements: ");
for(int i = 0; i < arrayLength; ++i) {
printf("%.1lf, ", ancillaryArray[i]);
}
printf("\nEnter Elements: ");
for (int i = 8; i < 10; ++i) {
scanf("%lf", &ancillaryArray[i]);
}
printf("\nLargest Array Element: %.1lf\n", largest(ancillaryArray, ancillaryarrayLength));
printf("Smallest Array Element: %.03lf\n", smallest(ancillaryArray, ancillaryarrayLength));
lowhighSort(ancillaryArray, ancillaryarrayLength);
printf("Array Ascending Order: ");
for(int i = 0; i < 10; ++i){
printf("%.3lf, ", ancillaryArray[i]);
}
printf("\n");
highlowSort(ancillaryArray, ancillaryarrayLength);
printf("Array Descending Order: ");
for(int i = 0; i < 10; ++i){
printf("%.3lf, ", ancillaryArray[i]);
}
printf("\n");
printf("Array Element Average: %.4lf", (arrayAverage(ancillaryArray, ancillaryarrayLength))/10);
return 0;
}
Here is the final code, it works as intended! Thank you all for your help.

I want to find all sub matrix of a matrix. I made a code in C, but it only finds square ones. How can I improve my code

The following code finds all square sub matrix of a square matrix:
#include <stdio.h>
int main() {
int mtrx_size = 3;
int mat[3][3] = {
{ 1, 2, 3},
{ 9,10,11},
{17,18,19},
};
//I took 3*3 matrix for example though It works for any square matrix
int i, j, k, l, m;
int sub_mtrx_size;
for(sub_mtrx_size = mtrx_size; sub_mtrx_size > 1 ; sub_mtrx_size--) {
m = mtrx_size - sub_mtrx_size + 1;
for (k = 0; k <m; k++) {
for (l = 0; l <m; l++) {
for (i = 0; i < sub_mtrx_size; i++) {
for (j = 0; j < sub_mtrx_size; j++) {
printf("%3d ", mat[i+k][j+k]);
}
printf("\n");
}
printf("\n");
}
}
}
return 0;
}
But I want to find all of the sub matrix of a matrix.
I've tried this way to get all my sub matrix:
#include <stdio.h>
int main() {
int mtrx_size = 3;
int mtrx_size1=3;
int mat[3][3] = {
{ 1, 2,3},
{ 9,10,4},
{ 5,6,7},
};
//Here I took example for 3*3 matrix though it should work for others
int i, j, ioff, joff, off_cnt,off_cnt1;
int sub_mtrx_size,sub_mtrx_size1;
for(sub_mtrx_size = mtrx_size; sub_mtrx_size > 0 ; sub_mtrx_size--)
for(sub_mtrx_size1 = mtrx_size1; sub_mtrx_size1 > 0 ; sub_mtrx_size1--){
off_cnt = mtrx_size - sub_mtrx_size + 1;
off_cnt1 = mtrx_size1 - sub_mtrx_size1 + 1;
for (ioff = 0; ioff < off_cnt; ioff++) {
for (joff = 0; joff < off_cnt1; joff++) {
for (i = 0; i < sub_mtrx_size; i++) {
for (j = 0; j < sub_mtrx_size1; j++) {
printf("%3d ", mat[i+ioff][j+joff]);
}
printf("\n");
}
printf("\n");
}
}
}
return 0;
}

Find maximum value from 2 dimensional array & addition of all the values before the maximum value & multiply the all values after the maximum value

Here is my code
#include<stdio.h>
void main() {
int a[4][4] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 15, 6, 5 },
{ 4, 3, 2, 1 } };
int max = a[0][0];
int mIndexF, mIndexE, addition = 0, multiplication = 1, i, j, status = 0, k,
l;
// this is for find out maximum value
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if (max < a[i][j]) {
max = a[i][j];
mIndexF = i;
mIndexE = j;
}
}
}
for (k = 0; k < 4; k++) {
for (l = 0; l < 4; l++) {
if ((a[k][l] < max) && (status == 0)) {
addition += a[k][l];
} else {
status++;
if (a[k][l] != max) {
multiplication *= a[k][l];
}
}
}
}
printf("Addition is %d\n", addition);
printf("Multiplication is %d", multiplication);
return 0;
}
I want to find the maximum value. Also want to print addition of the values which are in before of the maximum value and want to print multiply value of the values which are in after the maximum value.
The following should do the trick:
#define MAX_INT (((unsigned int)(-1))>>1)
#define MIN_INT (~(MAX_INT))
void minmax(int a[4][4])
{
int i, j, maxi=0, maxj=0, max=MIN_INT, sum=0, mul=1;
// this is for find out maximum value
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if (max < a[i][j]) {
max = a[i][j];
maxi = i;
maxj = j;
}
}
}
// this is to add and multiply
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if (i< maxi || (i==maxi && j<maxj)) // this is "before"
sum += a[i][j];
else if (i==maxi && j==maxj) // this is "same"
; //..nothing to do
else mul *= a[i][j];
}
}
printf("i,j=%d,%d; sum= %d, mul= %d\n", maxi, maxj, sum, mul);
}
EDIT: Added definitions of MAX_INT and MIN_INT
Your code seems to be correct: just initialize
max=INT_MIN using include<limits.h>
Your second loops seems to slight inappropriate you could use:
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(status==0)
add+=disp[i][j];
else if(status==1)
mul*=disp[i][j];
if(i==loc_i && j==loc_j)
status=1;
}
Afterwards just subtract
add-=disp[mIndexF][mIndexE];
#include<stdio.h>
void main(){
int a[4][4]={
{10,11,12,13},
{14,15,16,17},
{18,19,20,21},
{22,2,3,3}
};
int max = a[0][0],mIndexF,mIndexE,addition = 0,multiplication = 1,i,j,status=0,k,l;
// this is for find out maximum value
for(i=0;i<4;i++){
for(j=0;j<4;j++){
if(max<a[i][j]){
max = a[i][j];
mIndexF=i;
mIndexE=j;
}
}
}
printf("The maximum value is %d\n", max);
for(k=0;k<4;k++){
for(l=0;l<4;l++){
if((a[k][l]<max) &&(status==0)){
addition+=a[k][l];
}else{
status++;
if(a[k][l]!=max){
multiplication*=a[k][l];
}
}
}
}
printf("Addition is %d\n",addition);
printf("Multiplication is %d",multiplication);
return 0;
}

Bubble sort an Array of pointers of another arrays in a function (C)

I want to make a bubble sort function of an array of pointers that each of the pointers point to another arrays - inside of function and i'm getting a error that i violated a writing location (Visual Studio)
P.S, I do (*parr)++ because the first value of each array shows the length of the array without the first value so i need to start bubble sorting from the second box (arr[1] and not arr[0] for example ).
can someone write to me how can i fix it?
Thanks for help
(I need to sort the values of the original arrays not the pointer of the arrays).
int main(void){
int i = 0;
int arr0[4] = { 3, 9, 6, 7 };
int arr1[3] = { 2, 5, 5 };
int arr2[1] = { 0 };
int arr3[2] = { 1, 6 };
int arr4[5] = { 4, 5, 6, 2, 1 };
int* parr[5] = { arr0, arr1, arr2, arr3, arr4 };
func1(parr);
system("PAUSE");
return (0);
}
void func1(int** parr)
{
int i;
int temp;
int j;
int k;
int length;
for (i = 0; i < 5; i++, (parr)++)
{
length = **parr;
(*parr)++;
for (j = 0; j < length-1; j++)
{
for (k = 0; k < length - j - 1; k++, (*parr)++)
{
if ((**parr)>(*(*parr + 1)))
{
temp = **(parr);
**(parr) = (*(*parr + 1));
(*(*parr + 1)) = temp;
}
}
}
}
}
This seems to work. It is easier in func1 to use dereferenceing as parr[i][k] rather than moving the pointer.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void func1(int** parr);
int main(void){
int j;
int arr0[4] = { 3, 9, 6, 7 };
int arr1[3] = { 2, 5, 5 };
int arr2[1] = { 0 };
int arr3[2] = { 1, 6 };
int arr4[5] = { 4, 5, 6, 2, 1 };
int* parr[5] = { arr0, arr1, arr2, arr3, arr4 };
func1(parr);
for (j = 1; j <= arr0[0]; j++)
{
printf ( "arr0[%d] %d\n", j, arr0[j]);
}
for (j = 1; j <= arr4[0]; j++)
{
printf ( "arr4[%d] %d\n", j, arr4[j]);
}
return (0);
}
void func1(int** parr)
{
int i;
int temp;
int j;
int k;
int length;
for (i = 0; i < 5; i++)
{
length = **parr;
for (j = 0; j < length; j++)
{
for (k = 1; k < length - j; k++)
{
temp = *((*parr)+k);
if (*((*parr)+k)>*((*parr)+k+1))
{
temp = *((*parr)+k);
*((*parr)+k) = *((*parr)+k+1);
*((*parr)+k+1) = temp;
}
}
}
*parr++;// next array
}
}

How to display the elements of a 5*5 matrix in the following way?

I want to display the output of 5*5 matrix in the following way:
Input:
1 2 3 4 5
6 7 8 9 0
1 3 5 7 9
2 4 6 8 0
1 4 3 7 0
Output:
1 2 3 4 5 0 9 0 0 7 3 4 1 2 1 6 7 8 9 7 8 6 4 3 5
I have written the below program, but I can't get output like the above. What is the mistake?
#include<stdio.h>
int main()
{
int i, j, m, n, a[5][5];
scanf("%d%d", & m, & n);
if (m >= 1 && m <= 5 && n >= 1 && n <= 5)
{
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", a[i][j]);
}
}
if (m == 5 && n == 5)
{
for (i = 0; i == 0; i++)
{
for (j = 0; j < n; j++)
{
printf("%d", a[i][j]);
}
}
for (i = 0; i < n; i++)
{
for (j = n - 1; j == n - 1; j++)
{
printf("%d", a[i][j]);
}
}
for (i = n - 1; i == n - 1; i++)
{
for (j = n - 2; j >= 0; j--)
{
printf("%d", a[i][j]);
}
}
for (i = n - 2; i >= n - 4; i--)
{
for (j = 0; j == 0; j++)
{
printf("%d", a[i][j]);
}
}
for (i = n - 4; i == n - 4; i++)
{
for (j = n - 4; j <= n - 2; j++)
{
printf("%d", a[i][j]);
}
}
for (i = n - 3; i == n - 3; i++)
{
for (j = n - 2; j >= n - 4; j--)
{
printf("%d", a[i][j]);
}
}
}
}
return 0;
}
use
if(m>=1 && m<=5 && n>=1 && n<=5)
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
you will get output displayed on screen and use
printf("%d\t",a[i][j]);
to display output clearly
make this changes to the code, you are not looping through the matrix properly,
#include<stdio.h>
int main()
{
int i,j,m,n,a[5][5];
scanf("%d%d",&m,&n);
if(m>=1 && m<=5 && n>=1 && n<=5)
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
if(m==5 && n==5)
{
for(i=0;i==0;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
}
for(i=1;i<n;i++)
{
for(j=n-1;j==n-1;j++)
{
printf("%d\t",a[i][j]);
}
}
for(i=n-1;i==n-1;i++)
{
for(j=n-2;j>=0;j--)
{
printf("%d\t",a[i][j]);
}
}
for(i=n-2;i>=n-4;i--)
{
for(j=0;j==0;j++)
{
printf("%d\t",a[i][j]);
}
}
for(i=n-4;i==n-4;i++)
{
for(j=n-4;j<=n-2;j++)
{
printf("%d\t",a[i][j]);
}
}
for(i=n-3;i<=n-2;i++)
{
for(j=n-2;j==n-2;j++)
{
printf("%d\t",a[i][j]);
}
}
for(i=n-2;i==n-2;i++)
{
for(j=n-3;j>=n-4;j--)
{
printf("%d\t",a[i][j]);
}
}
for(i=n-3;i==n-3;i++)
{
for(j=n-4;j<=n-3;j++)
{
printf("%d\t",a[i][j]);
}
}
}
}
return 0;
}
this loop is for 5*5 only as you coded.
5*5 sample
#include<stdio.h>
int main(){
int i,j,m,n,a[5][5];
//scanf("%d %d",&m,&n);
//if(m>=1 && m<=5 && n>=1 && n<=5){
m = n = 5;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d", &a[i][j]);
}
}
int side_size = m;
int row = 0, col = 0;//start point
int side = side_size -1;
while(side > 1){
for(i = 0; i<side ;++i)
printf("%d ", a[row][col++]);//MOVE RIGHT
for(i = 0; i<side ;++i)
printf("%d ", a[row++][col]);//MOVE DOWN
for(i = 0; i<side ;++i)
printf("%d ", a[row][col--]);//MOVE LEFT
for(i = 0; i<side ;++i)
printf("%d ", a[row--][col]);//MOVE UP
++row;++col;
side -= 2;
}
printf("%d\n", a[row][col]);
return 0;
}
I would use go forward and turn right approach
int max_x = 4;
int min_x = 0;
int max_y = 4;
int min_y = 0;
int x = 0;
int y = 0;
int direction = 0; // 0-RIGHT, 1-DOWN, 2-LEFT, 3-UP
int count = 0;
int go_forward(){
switch(direction){
case 0:
if( x+1 > max_x ){ min_y++; return 1; }
else x++;
break;
case 1:
if( y+1 > max_y ){ max_x--; return 1; }
else y++;
break;
case 2:
if( x-1 < min_x ){ max_y--; return 1; }
else x--;
break;
case 3:
if( y-1 < min_y ){ min_x++; return 1; }
y--;
break;
}
return 0;
}
int turn_right(){
direction = (direction+1)%4; // 0-RIGHT, 1-DOWN, 2-LEFT, 3-UP
}
int main(){
/* int matrix[5][5]={ { 1, 2, 3, 4, 5},
{ 6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
{21, 22, 23, 24, 25}}; //TEST */
//get matrix from input
int cango;
do{
printf("%d ", matrix[y][x]);
count++;
cango = go_forward();
if( cango!=0 ){
turn_right();
cango = go_forward();
}
}while(cango == 0 && count < 25);
return 0;
}

Resources