A matrix pointer into sub-matrices in C - c

I have this matrix in C.
[1,2,3,4]
[5,6,7,8]
[9,10,11,12]
[13,14,15,16]
n x n, squared matrix.
I need to split this into four matrices:
[1,2] [3,4] [9,10] [11,12]
[5,6] [7,8] [13,14] [15,16]
This will be represented inside an array like this:
array[16] = [1,2,5,6,3,4,7,8,9,10,13,14,11,12,15,16]
So far, I've done this:
int i,j;
int k = 0;
for(i = 0; i < 2; i++ )
{
for(j = 0; j < 2; j++)
{
array[k] = matrix[i][j];
k++;
}
}
for(i = 0; i < 2; i++ )
{
for(j = 2; j < 4; j++)
{
array[k] = matrix[i][j];
k++;
}
}
for(i = 2; i < 4; i++ )
{
for(j = 0; j < 2; j++)
{
array[k] = matrix[i][j];
k++;
}
}
for(i=2;i<4;i++)
{
for(j=2;j<4;j++)
{
array[k] = matrix[i][j];
k++;
}
}
As you can see, I've done 4 double for this, but, is there a dynamic way to do this? If I got a bigger matrix, like 8 x 8, how to do this? The split if bigger is the same as the example.

You should see the pattern of the routines you are duplicating.
Basically you do the same thing, except that the start and end of i and j are different. So create a sub routine and pass them as parameter.
For example:
void get_sub_matrix(int input[][N], int start_row, int end_row, int start_col, int end_col, int[] result, int* result_offset)
{
int offset = *result_offset;
for (int i = start_row; i < end_row; i++)
{
for (int j = start_col; j < end_col; j++)
{
result[offset++] = input[i][j];
}
}
*result_offset = offset;
}
Notice how the result-offset is increased inside the routine as more elements are added to the result array.
Now you can do:
int matrix[N][N];
int array[N*N];
int k = 0;
get_sub_matrix(matrix, 0, 2, 0, 2, array, &k);
get_sub_matrix(matrix, 0, 2, 2, 4, array, &k);
get_sub_matrix(matrix, 2, 4, 0, 2, array, &k);
get_sub_matrix(matrix, 2, 4, 2, 4, array, &k);
P.S: Haven't compiled it.

Related

Changing the position of a 2D array in a matrix in C language

here is the question :
have a two 2D array
int matrix1[4][5]={ {8,5,2,4,3}, {9,6,4,0,4}, {0,1,2,3,4}, {9,9,9,9,9}};
Without changing the elements in the row in the matrix1 The sum of the rows will be ordered from smallest to largest. finally I want to transfer it to matrix 2 . cant use copy or functions just the basic programming skills like ( for,array,if, )
matrix2 should look like this:
int matrix2[4][5]={{0,1,2,3,4}, {9,6,4,0,4}, {8,5,2,4,3}, {9,9,9,9,9}};
here is my code
int i, j, temp, plus = 0;
int matris1[4][5] = {
{8, 5, 2, 4, 3},
{9, 6, 4, 0, 4},
{0, 1, 2, 3, 4},
{9, 9, 9, 9, 9}};
int matris2[4][5];
int topla[4];
/*here I found sum of the rows*/
for (i = 0; i < 4; i++)
{
for (j = 0; j < 5; j++)
{
plus = plus + matris1[i][j];
}
topla[i] = plus;
plus = 0;
}
/*here I did the smallest to largest and copy to the array topla */
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
if (topla[i] < topla[j])
{
temp = topla[i];
topla[i] = topla[j];
topla[j] = temp;
}
}
}
/* cant transfer to the matrix2 HELP PLEASE :'( */
int k;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 5; j++)
{
plus = plus + matris1[i][j];
}
for (k = 0; k < 4; k++)
{
if (plus == topla[k])
{
matris2[k] = matris1[i];
}
}
plus = 0;
}
for (i = 0; i < 4; i++)
{
for (j = 0; j < 5; j++)
{
printf("%d - ", matris2[i][j]);
}
printf("\n");
}
You can create a struct remembering the sum and a pointer to the corresponding data that gave that sum:
typedef struct
{
int* data;
int sum;
} sum_t;
Then it goes like this:
int matrix1[4][5] = { {8,5,2,4,3}, {9,6,4,0,4}, {0,1,2,3,4}, {9,9,9,9,9} };
sum_t sum[4] = {0}; // set everything to zero
for(size_t i=0; i<4; i++)
{
sum[i].data = matrix1[i]; // remember which sum this is for later
for(size_t j=0; j<5; j++) // calculate sum
{
sum[i].sum += matrix1[i][j];
}
}
Next you'd call a sort function like qsort. But since you can't do that because of artificial requirements, you could just spew out some inefficient, manual version:
// school book example of a bad sort function:
void shitty_bubble_sort (size_t n, sum_t arr[n])
{
for(size_t i=0; i<n-1; i++)
{
for(size_t j=0; j<n-i-1; j++)
{
if(arr[j].sum > arr[j+1].sum)
{
sum_t tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
}
}
Usage: shitty_bubble_sort(4,sum);
That bubble sort function is the same thing you did when sorting topla, the only real difference is the loop indices and that I'm moving structs instead of integers.
Then create the next matrix based on the sorted sums:
int matrix2[4][5];
for(size_t i=0; i<4; i++)
{
shitty_memcpy(&matrix2[i], sum[i].data, sizeof(matrix2[i]));
printf("{ ");
for(size_t j=0; j<5; j++)
{
printf("%d ", matrix2[i][j]);
}
printf("}\n");
}
Where shitty_memcpy is just like ordinary memcpy but slower, for the benefit of your artificial requirments:
//school book example of a naive memcpy
void shitty_memcpy (void* dst, const void* src, size_t n)
{
unsigned char* d = dst;
const unsigned char* s = src;
for(size_t i=0; i<n; i++)
{
d[i] = s[i];
}
}
Complete example here: https://godbolt.org/z/ra8jenWWM. If you aren't allowed to use functions at all, then inlining those two functions manually is left as an exercise.

Sorting matrix by the sum of columns in C

I need help with code for sorting matrix by sum of columns.
On this site there is already a similar program with sorting by sum of rows.I copied that code and tried to adapt it for columns and it doesn't work.
And one more question:I don't understand the operation in function sum.
If someone could help me with this.
Thank you.
#include<stdio.h>
int sum(int *row, int size) {
int s = 0;
for (size--; size >= 0; size--) s += row[size];
return s;
}
int main() {
int matrix[3][4] = {{1, 2, 3, 4},
{9, 10, 11, 12},
{5, 6, 7, 8}};
int tmp;
printf("Before:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
// Bubble sort.
for (int i = 0; i < 2; i++) {
for (int j = i + 1; j < 3; j++) {
// Comparing row value.
if (sum(matrix[i], 4) > sum(matrix[j], 4)) {
// Swapping.
for (int k = 0; k < 4; k++) {
tmp = matrix[i][k];
matrix[i][k] = matrix[j][k];
matrix[j][k] = tmp;
}
}
}
}
printf("After:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}

C Find sub-symmetric matrix in one big matrix

I try to find all the sub-symmetric matrix in one big matrix but I only get some of the sub-symmetric matrix and some of them are incorrect.
In my function I found all the sub-matrix and try to send all the square sub-matrix to function that check if the sub-matrix is symmetric, If the sub-matrix is symmetric I print the matrix.
main -
int main() {
int matrix[][8] = {{1, 2, 0, 3, 2, 1, 0, 9},
{2, 3, 4, 1, 2, 3, 4, 5},
{3, 4, 6, 2, 5, 6, 7, 86},
{9, 5, 8, 3, 6, 8, 9, 8},
{6, 7, 1, 4, 7, 9, 1, 9}};
findSubMatrix(matrix, 5, 8);
return 0;
}
findSubMatrix -
void findSubMatrix(int matrix[][8], int rows, int cols) {
for (int i = 0; i <= cols; i++) {
for (int j = i; j <= cols; j++) {
for (int m = 0; m <= rows; m++) {
for (int n = m; n <= rows; n++) {
if (n - m == j - i && n - m > 0) {
if (isSymmetric(matrix, i, j, m, n) == 1) {
for (int k = m; k <= n; k++) {
for (int l = i; l <= j; l++) {
printf("%d ", matrix[k][l]);
}
printf("\n");
}
printf("\n");
}
}
}
}
}
}
}
isSymmetric -
int isSymmetric(int matrix[][8], int rowStart, int rowEnd, int colStart, int colEnd) {
for (int i = colStart; i < colEnd; i++) {
for (int j = rowStart; j < rowEnd; j++) {
if (matrix[j][i] != matrix[i][j]) {
return -1;
}
}
}
return 1;
}
I appreciate any help Thanks.
All the sub-matrices, whether are they beeing tested or printed, should be square, so I'd use a different signature than OP's. Take for example this helper function which prints out a sub-matrix, given a bigger matrix passed with VLA notation, a starting point (indices of the top left corner) and a size:
void print_submatrix(int cols, int m[][cols],
int first_row, int first_col, int n)
{
for (int r = 0; r < n; ++r) {
for (int c = 0; c < n; ++c) {
printf("%3d", m[first_row + r][first_col + c]);
}
putchar('\n');
}
putchar('\n');
}
This should help us finding the correct formulas to evaluate the indices in the function that checks symmetry.
int is_symmetric(int cols, int matrix[][cols],
int first_row, int first_col, int n)
{
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
// ^^^^^^^^^ I'll loop only through half the sub-matrix
if ( matrix[first_row + i][first_col + j]
!= matrix[first_row + j][first_col + i] ) {
// ^^^^^^^^^^^^^ ^^^^^^^^^^^^^
// Note that the starting point (top left corner) is fixed,
// while the offset is changing
return 0;
}
}
}
return 1;
}
The outer loops could be rewritten too
void find_sym_submatrices(int rows, int cols, int matrix[][cols])
{
// For every possible top left corner...
for (int i = 0, mi = rows; i < rows; ++i, --mi) {
for (int j = 0, mj = cols; j < cols; j++, --mj) {
// For every possible square matrix (which fits in size).
const int m = mj < mi ? mj : mi;
for (int n = 2; n <= m; ++n) {
if (is_symmetric(cols, matrix, i, j, n) == 1) {
print_submatrix(cols, matrix, i, j, n);
}
else {
break; // Yeap, there's no need to check the bigger ones.
}
}
}
}
}
Live, here.

C simple sorting algorithm to change

How to change the following sorting algorithm to operate on a single array arr[]? For now, I have to transfer elements from one array arr[] to res[]. How to preform all operations on arr[]?
Here is my code:
void main(){
int arr [] = {1, 14, 5, 18, 3, 1};
int res [] = {0, 0, 0, 0, 0, 0};
int x = 0;
int y = 0;
for(int i = 0; i < 6; i++) {
x = 0;
for(int k = 0; k < 6; k++){
if(arr[k] > x){
x = arr[k];
y = k;
}
}
arr[y] = 0;
res[5 - i] = x;
}
for (int z = 0; z < 6; z++){
printf("%d ", res[z]);
}
}
Just swap the elements in array that mistakenly positioned thoroughly by checking one by one. This called as bubble sort. There's also another sorting algorithms u can search online or check visualgo for animated process for understanding.
void main(){
int arr [] = {1, 14, 5, 18, 3, 1};
// This applies bubble sort
for(int i = 0; i < 6; i++) {
for(int k = 0; k < 6; k++){
// Swap elements in an array
if(arr[i] > arr[k]){
int x = arr[k];
arr[k] = arr[i];
arr[i] = x;
}
}
}
for (int z = 0; z < 6; z++){
printf("%d ", res[z]);
}
}

How to multiply a 4x4 matrix with a 1x3 matrix in C?

If you already have a set 4x4 matrix.
Ex.
Matrix A = [1 2 3 4;
5 6 7 8;`
9 10 11 12;
13 14 15 16]
Matrix B = [1, 2, 3]
How would you convert Matrix A into C coding? Also what would there positions be in code? For position I mean: if I'm trying to multiply the first row into matrix B, can I do this?
A[1][0]*B[0]+A[1][1]*B[1]+A[1][2]*B[2]
Outline code:
main(){
int matrixA[4][4] = [{"1","2","3","4"};
{"5","6","7","8"};
{"9","10","11","12"};
{"13","14","15","16"}];
printf(matrix A);
return 0;
}
First of all you cannot multiply a 1×3 matrix with 4×4 matrix.
You should have matrices like m×n and n×p to get them multiplied (the result will be an m×p matrix).
Also for having a 4×4 matrix in C you should implement it like this:
int main()
{
int mat[4][4];
for(int i=0;i<=3;i++)
{
for(int j=0;j<=3;j++)
{
scanf("%d", &mat[i][j]);
}
}
return 0;
}
As far i can understand, you want to make a program to execute mathematical fractions related to matrices. Example in linear algebra. Matrix sizes are not checked, but you get the idea. In division you have to make the calculation of Array2^-1 to find it. Hope i helped. After you find it, multiply the result ,to Array1. In division however, things are more complicated. You need to approach your coding in different ways, depending to the array(matrix) dimensions you have. Exceptions must be included and need to read a bit of theory on how to divide those matrices. See https://en.wikipedia.org/wiki/Division_(mathematics) for more details.
#include <stdio.h>
#include <math.h>
int main(){
//counters
int i=0,j=0,k=0,sum=0;
//first array
int Array1[4][4] ={
{87,96,70,22},
{18,65,77,78},
{76,72,84,65},
{87,93,73,77}};
//second array
int Array2[4][4]={
{14,45,66,88},
{45,32,97,44},
{34,64,23,66},
{39,98,55,32}};
//result array.
int ResultArray[4][4];
// Add
for (i = 0; i < 4; ++i) {
for (j = 0; j < 4; ++j) {
ResultArray[i][j]= Array1[i][j]+Array2[i][j];
}
}
//result
printf("\tAdd Array1 and Array2\n");
for (i = 0; i < 4; ++i) {
for (j = 0; j < 4; ++j) {
printf(" %10d \t",ResultArray[i][j]);
}
printf("\n");
}
//subtract
for (i = 0; i < 4; ++i) {
for (j = 0; j < 4; ++j) {
ResultArray[i][j]= Array1[i][j]-Array2[i][j];
}
}
//result
printf("\tSubtract Array1 and Array2\n");
for (i = 0; i < 4; ++i) {
for (j = 0; j < 4; ++j) {
printf(" %10d \t",ResultArray[i][j]);
}
printf("\n");
}
//multiply
for (i = 0; i < 4; ++i) {
for (j = 0; j < 4; ++j) {
for (k = 0; k < 4; ++k) {
sum=sum+Array1[i][k]*Array2[k][j];
}
ResultArray[i][j]=sum;
sum=0;
}
}
//result
printf("\tMultiplication Array1 and Array2\n");
for (i = 0; i < 4; ++i) {
for (j = 0; j < 4; ++j) {
printf(" %10d \t",ResultArray[i][j]);
}
printf("\n");
}
return 0;
}
you can write like this:
int matrixA[4][4] = {
{ 1, 2, 3, 4},//matrixA[0][0] is 1
{ 5, 6, 7, 8},//matrixA[1][1] is 6
{ 9, 10, 11, 12},
{13, 14, 15, 16}
};
int matrixB[] = { 1, 2, 3};
for(int r = 0; r < 4; ++r){
for(int c = 0; c < 4; ++c){
printf("%3d", matrixA[r][c]);
}
printf("\n");
}
Let me help you a bit, but you in return help me understand the question. :)
Representation
Ok Matrices are represented using 2-d array.
Like
int a[][]= { {1,2,3,4}, {5,6,7,8},{9,10,11,12},{13,14,15,16}};
Matrix is ready. :)
If you want to create it on the fly..malloc() will be your friend.
Here I meant to say that you have to dynamically allocate the array.
For example:
int **a;
a= malloc(sizeof(int*)*m);
for(int i=0;i<m;i++)
a[i]=malloc(sizeof(int)*n);
Forming mxn matrix where m and n are got as input maybe.
How to multiply 2 matrices [ ofcourse compatible]?
Here in your case A[4x4] B[1x4] so p=4,m=1,q=4.
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}

Resources