typedef struct _PQ {
short cost;
} PQ;
void Dijkstra(short** arr, short row, short col) {
//initialize DIST
PQ **table;
table = malloc(sizeof(*table) * (row));
for (int i = 0; i < row; i++) {
table[i] = malloc(sizeof(table) * col);
}
// 1. graph
for (short i = 0; i < row; i++) {
for (short j = 0; j < col; j++) {
table[i][j].cost = arr[i][j];
}
if (i == 0) {
printf("First run: %hd %hd\n", arr[0][4], table[0][4].cost);
printf("%hd ", table[1][0].cost);
}
}
}
row is 5 and column is 4.
I'm currently inserting 5 (arr[0][4]) at table[0][4].cost
However, as soon as I insert the value to table[0][4].cost, table[1][0].cost updates with the same value.
Also after finish inserting the second row, my first row value changes.
After inserting the first row
4 2 1 0 5
5
After inserting the second row
4 2 1 0 2
2 0 4 1 0
and this happens every row. Last element on the row and first element on the next row are same.
I'm really not sure why this happens.
Also, I checked if my arr[i][j] value, but the arr is correct.
after inserting the second row,
I should have
4 2 1 0 5
2 0 4 1 0
instead of
4 2 1 0 2
2 0 4 1 0
I feel like it's related to pointer, but I'm not sure what to do
Disclaimer: I'm not very good with words, I have included a sample of how will it look like below.
So I'm trying to reduce this 4x4 array into a bunch of 2x2 arrays. (See sample below)
int disp[4][4] = {{12,4,32,9}, {19,24,3,4},{1,26,3,8},{3,24,7,5} };
/*
12 4 32 9 12 4 | 32 9
19 24 3 4 into something like 19 24 | 3 4
1 26 3 8 =============
3 24 7 5 1 26 | 3 8
3 24 | 7 5
*/
Take note: it is splitting it into smaller sizes (bunch of 2D arrays)
I have tried splitting it using a for loop code but only getting the 2nd quadrant.
code below:
int i,j;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
printf("%d\n",disp[i][j]);
see that this only gives me 12,4,19 and 24.
Is there a way to get the other quadrants? How would this work for bigger sizes? (Lets say 28x28 to 14x14) I would really appreciate the help. Thank you.
Let's consider a NXN matrix where N and quadrant_size be the required size of sub_matrix to be printed.
We first traverse to find the starting element to be printed in each sub_matrix.
i.e: From 0 to N incrementing by quadrant_size for row and 0 to N incrementing by quadrant_size for column.(First two loops in the below given code)
Let's say (x,y) index at any position. Now we print the matrix elements from (x,y) to (x+2,y+2)
The below code is generalized for any size of matrix NxN of required quadrant_size where N should be divisible by quadrant_size.
int quadrant_size=2;
//outer loop to traverse the starting elements of the sub array
for(int x=0 ; x<N ; x+=quadrant_size){
for(int y=0 ; y<N ; y+=quadrant_size){
//inner loop to print matrix
for(int i=x ; i<x+quadrant_size ; i++){
for(int j=y ; j<y+quadrant_size ; j++){
printf("%d ",disp[i][j]);
}
printf("\n");
}
printf("\n");
}
}
You need to add an offset to i and j depending on which quadrant you want to print.
For your specific case it could be:
int main() {
int disp[4][4] = {{12,4,32,9},
{19,24,3,4},
{1,26,3,8},
{3,24,7,5} };
// calculate quadrant size (it will be 2 in this case)
int qsize = sizeof disp[0] /sizeof disp[0][0] / 2;
int q = 3; // <--- The quadrant to print
int offset_i = 0; // Extra offsets for
int offset_j = 0; // the loop that prints
if (q == 1)
{
offset_j = qsize;
}
else if (q == 3)
{
offset_i = qsize;
}
else if (q == 4)
{
offset_i = qsize;
offset_j = qsize;
}
for(int i=0; i<qsize; i++)
{
for(int j=0; j<qsize; j++)
{
printf("%d ",disp[i + offset_i][j + offset_j]);
// Notice: ^^^^^^^^^^ ^^^^^^^^^^
}
printf("\n");
}
return 0;
}
I need to check if I can find inside of given matrix size of 5*8
a matrix that has a transpose and if there is more than one I must find the biggest one.
example of a given matrix
1 2 0 3 2 1 0 7
2 3 4 1 2 3 4 5
3 4 6 2 5 6 7 6
4 5 7 3 6 8 9 8
6 7 1 4 7 9 0 9
in this matrix we can find a matrix 4x4
that has transpose and its the biggest matrix in the main matrix
1 2 3 4
2 5 6 7
3 6 8 9
4 7 9 0
#include <stdio.h>
#define M 4
#define column 5
#define row 8
int main()
{
int matrixA[5][8];
printf("please enter a matrix to check if there is a transpose matrix\n");
for (int i = 0; i < column; i++)
{
for (int j = 0; j < row; j++)
{
printf("please enter %d row and %d column: ", i + 1, j + 1);
scanf("%d", &matrixA[i][j]);
}
}
transpose(matrixA, column, row);
}
void transpose(int A[][row], int c, int r)
{
int matrixAT[M][M];
for (int size = r; size > 0; size--)
{
for (int j = 0; j < c - size + 1; j++)
{
for (int b = 0; b <= r - size; b++)
{
printf("Checking Matrix at row: %d , column: %d ,size: %dx%d", j, b, size, size);
for (int k = j, w = 0; k < size + j; k++, w++)
{
for (int l = b, z = 0; l < size + b; l++, z++)
{
matrixAT[w][z] = A[k][l];
}
printf("/n");
}
if (IsSymmetric(matrixAT, size))
printf("Matrix found");
}
}
}
}
int IsSymmetric(int mat[M][M], int size)
{
int flag = 0;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (mat[i][j] == mat[j][i]) flag++;
}
}
return flag == size * size ? 1 : 0;
}
this is my code i dont know what im doing wrong
Your IsSymmetric is slow as it always check all elements why not stop on first inequality instead? Also copying it to temp array again and again ...
The main problem is You are not checking every position and size as you call transpose(matrixA, column, row); only once outside the loops ...
Also your main does not return anything and its declared as int ...
I would start with brute force like this:
#define column 5
#define row 8
int IsSymmetric(int mat[column][row], int i0,int j0,int size) // check n*n sub matrix at i0,j0 no need to copy again and again to temp array
{
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
if (mat[i0+i][j0+j] != mat[i0+j][j0+i]) return 0;
return 1;
}
int min(int a,int b){ return (a<b)?a:b; } // not sure if min is present in your environment if is comment this line out
int main()
{
int matrixA[5][8];
...
for (int i = 0; i < column; i++)
for (int j = 0; j < row; j++)
for (int n = 1; n <= min(column-i,row-j); n++)
if (IsSymmetric(matrixA,i,j,n))
{
// here do what you want with the i,j,n*n sub matrix
// like remember position and size for the biggest n
}
...
return 0; // return value as you declared int main
}
Hope I did not make any typo in here as I just wrote this into answer editor from your original code.
How ever as you can see its O(n^4) complexity (on average O(n^3)) which is really slow. However for your small matrix its not a problem.
In case you need something faster then we need to know more about the data ... for example what is the range of the values? Some hints:
on positive IsSymmetric test one cell bigger submatrix without testing the previous elements again (recursively increasing diagonal).
use histogram to detect values that might be only on diagonals (appear once globally or odd times locally)
Using the incremental symmetry test results in O(n^3) solution:
//---------------------------------------------------------------------------
#define column 5
#define row 8
//---------------------------------------------------------------------------
void submatrix_print(int mat[column][row], int i0,int j0,int n,int m)
{
int i,j;
printf("%i*%i at %i,%i\r\n",n,m,i0,j0);
for (i=0;i<n;i++,printf("\r\n"))
for (j=0;j<m;j++)
printf("%1i ",mat[i0+i][j0+j]);
}
//---------------------------------------------------------------------------
void submatrix_print_transposed(int mat[column][row], int i0,int j0,int n,int m)
{
int i,j;
printf("%i*%i at %i,%i\r\n",n,m,i0,j0);
for (i=0;i<m;i++,printf("\r\n"))
for (j=0;j<n;j++)
printf("%1i ",mat[i0+j][j0+i]);
}
//---------------------------------------------------------------------------
int min(int a,int b){ return (a<b)?a:b; }
int submatrix_symmetric(int mat[column][row], int i0,int j0) // returns biggest symetric submatrix size >=1 found at i0,j0
{
int i,n,N;
N=min(column-i0,row-j0); // max size that still fits into matrix
for (n=2;n<N;n++) // test all sizes above 1
for(i=0;i<n-1;i++) // only test newly added cells to last sub matrix
if (mat[i0+n-1][j0+i]!=mat[i0+i][j0+n-1])
return n-1; // first non match means last tested size i svalid
return n; // no mismatches mean full size is valid
}
//---------------------------------------------------------------------------
int main()
{
int mat[5][8]=
{
1,2,0,3,2,1,0,7,
2,3,4,1,2,3,4,5,
3,4,6,2,5,6,7,6,
4,5,7,3,6,8,9,8,
6,7,1,4,7,9,0,9,
};
submatrix_print(mat,0,0,5,8);
// submatrix_print_transposed(mat,0,0,5,8);
int i,j,n,i0=0,j0=0,n0=0;
for(i=0;i<column;i++)
for(j=0;j<row;j++)
{
n=submatrix_symmetric(mat,i,j);
if (n0<n){ n0=n; i0=i; j0=j; }
}
submatrix_print(mat,i0,j0,n0,n0);
return 0;
}
//-------------------------------------------------------------------------
The result of the code is:
5*8 at 0,0 // input matrix
1 2 0 3 2 1 0 7
2 3 4 1 2 3 4 5
3 4 6 2 5 6 7 6
4 5 7 3 6 8 9 8
6 7 1 4 7 9 0 9
4*4 at 1,3 // biggest symmetric sub matrix found
1 2 3 4
2 5 6 7
3 6 8 9
4 7 9 0
you can make a function that check if the matrix ican be transposed or no
and another function that take evry time a part from the main matrix and you move it everytime and check it with 1st function
example :
1st matrix :m[1][1] starting from zero
1 2
2 3
2 matrix :m[2][2] starting from one
2 0
3 4
then when you finish with 2 demension matrix you go to 3
till the end
i hope you understand me and sorry for my bad english
I want to generate all possible increasing subsequences of numbers (repetition allowed) from 1 to n, but of length k.
Ex. n=3, k=2
Output:
1 1
1 2
1 3
2 2
2 3
3 3
This is my code:
#include <stdio.h>
int s[100];
int n=6;
int k=4;
void subk(int prev,int index)
{
int i;
if (index==k)
{
for(int i=0; i<k; i++)
printf("%d ",s[i]);
printf("\n");
return;
}
s[index]=prev;
for (i=prev; i<=n; ++i)
{
subk(i,index+1);//,s,n,k);
}
}
int main()
{
int j;
for (j = 1; j<=n ; ++j)
{
subk(j,0);
}
return 0;
}
But this generates some unwanted repetitions. How do I eliminate those?
I have tested your code with n = 3 and k = 2 and got the following result:
1 1
1 1
1 1
1 2
1 2
1 3
2 2
2 2
2 3
3 3
This is obviously incorrect, as there are several identical numbers like 1 1 or 1 2.
But what exactly went wrong?
Let's write down the right results if n = 3 and k = 3. Now compare those to the result we got from the program when n = 3 and k = 2.
correct program (incorrect)
k = 3 k = 2
1 1 1 1 1
1 1 2 1 1
1 1 3 1 1
1 2 2 1 2
1 2 3 1 2
1 3 3 1 3
2 2 2 2 2
2 2 3 2 2
2 3 3 2 3
3 3 3 3 3
Now we can see that the incorrect output of the program is the same as the first two columns of the correct answer when we set k = 3. This means that the program solves the problem for 3 columns if we set k = 2, but only displays the first two columns.
You need to stop the program from writing the same number several times.
Solution 1
One way to do this is to execute the for-loop in the subk-function only once when it writes the last number (index == (k - 1)) into the buffer s.
In order to achieve this, you need to add the following two lines to the end of your for-loop.
if (index == (k - 1))
break;
(Instead of the break you could also use return)
After you added these two lines the function should look like this:
void subk(int prev, int index)
{
int i;
if (index == k)
{
for (int i = 0; i<k; i++)
printf("%d ", s[i]);
printf("\n");
return;
}
s[index] = prev;
for (i = prev; i <= n; ++i)
{
subk(i, index + 1);//,s,n,k);
if (index + 1 == k)
break;
}
}
Solution 2
Another way to solve the problem is to move the line s[index] = prev; to the beginning of the function and change the k in the if-statement to k - 1.
Now the function should look like this:
void subk(int prev, int index)
{
int i;
s[index] = prev;
if (index == k - 1)
{
for (int i = 0; i<k; i++)
printf("%d ", s[i]);
printf("\n");
return;
}
for (i = prev; i <= n; ++i)
{
subk(i, index + 1);//,s,n,k);
}
}
With this solution, the for-loop is never executed when the index shows that the program is at the last 'sub-number'. It just displays the number and exits the function because of the return.
You get the right result with both solutions, but I personally like the second solution better, because there is no additional if-statement that is executed every iteration of the for-loop and the program is (slightly) faster.