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;
}
Related
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.
Here we are given a martix and we have to find out whether the matrix is symmetrix or not. I want to optimise it using array pointer or passsing by reference or you can suggest better approach , which uses multiple concepts, and please provide an explanation , it would be helpful.
I am learning arrays so, that why I am asking you to use multiple concepts , I want to see if there is a better approach
#include <stdio.h>
#include <stdlib.h>
void swap(int arr[3][3], int i, int j)
{
int temp;
temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}
void check(int arr[3][3], int i, int j)
{
static int count = 0;
if (arr[i][j] == arr[j][i])
{
count++;
if (count == 9)
{
printf("matrix is symmetric");
}
}
}
int main()
{
int arr[3][3] = {1, 3, 3,
3, 1, 5,
3, 5, 5};
int i, j;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d\t", arr[i][j]);
}
printf("\n");
}
for (int i = 0; i < 3; i++)
{
for (int j = i + 1; j < 3; j++)
{
swap(arr, i, j);
}
printf("\n");
}
printf("THE TRANSPOSE MATRIX IS \n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d\t", arr[i][j]);
}
printf("\n");
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
check(arr, i, j);
if (arr[i][j] != arr[j][i])
{
printf("matrix is non-symmetric");
exit(0);
}
}
}
}
To find symmetric matrix simply use the following code:
#include <stdio.h>
#define dim 4
int main()
{
int i=0;
int arr[dim][dim]= {1, 3, 2, 3,
3, 1, 5, 4,
2, 5, 5, 16,
3, 4, 16, 7};
int isSymmetric = 1;
while (isSymmetric && i<dim)
{
for (int j = i; j < dim; j++)
{
if(arr[i][j]!=arr[j][i]) {
isSymmetric = 0;
break;
}
}
i++;
}
isSymmetric==1?printf("Symmetric"):printf("notSymmetric");
return 0;
}
The complexity of this algorithm at most is O(N/2) when N is the number of elements.
well, i need to store the fibonacci sequence in a 2x5 matrix but what im doing its not working. here's my attempt
int main()
{
int i,j,k;
int mat[2][5];
mat[0][0]=0;
mat[0][1]=1;
k=2;
for (i=0; i<2; i++)
{
for(j=0; j<5; j++)
{
mat[i][k]=mat[i][j]+mat[i][j+1];
k++;
}
}
for(i=0; i<2; i++)
{
for(j=0; j<5; j++)
{
printf("%d\t",mat[i][j]);
}
printf("\n");
}
return 0;
}
How about using the row and column of the matrix to calculate the "position" of that element at the fibonacci sequence?
#include <stdio.h>
#define ROWS 2
#define COLS 5
int fibonacci(int position)
{
if (position == 0)
return 0;
if (position == 1)
return 1;
return fibonacci(position - 1) + fibonacci(position - 2);
}
int main()
{
int mat[ROWS][COLS];
for(int row = 0; row < ROWS; row++)
{
for(int col = 0; col < COLS; col++)
{
/*
[0,0] = 0 [0,1] = 1, [0,2] = 2, [0,3] = 3, [0,4] = 4
[1,0] = 5 [1,1] = 6, [1,2] = 7, [1,3] = 8, [1,4] = 9
*/
mat[row][col] = fibonacci(COLS * row + col);
printf("%3d, ", mat[row][col]);
}
printf("\n");
}
return 0;
}
Would you please try the following:
#include <stdio.h>
int main()
{
int i, j, k;
int mat[2][5] = {{0, 1}};
for (k = 2; k < 10; k++) {
mat[k / 5][k % 5] = mat[(k - 1) / 5][(k - 1) % 5] + mat[(k - 2) / 5][(k - 2) % 5];
}
for (i = 0; i < 2; i++) {
for (j = 0; j < 5; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
return 0;
}
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.
#include <stdio.h>
int main()
{
int array[8] = {4, 1, 5, 4, 2, 7, 4, 2};
int i, j, k;
int len = 8;
for(i = 0; i < len; i++)
{
for(j = i + 1; j < len;)
{
if(array[j] == array[i])
{
for(k = j; k < len - 1; k++)
{
array[k] = array[k + 1];
}
len--;
}
else
{
j++;
}
}
for(i = 0; i < len; i++)
{
printf("%d ", array[i]);
}
printf("\n");
}
return 0;
}
Hi,
Above is the code to remove duplicated numbers in an array, but when compiles and executes, I get 4 1 5 2 7 2--which isn't right because I'm supposed to get 4 1 5 2 7.
It seems that I have a problem with len but couldn't figure out where and what in the code specifically needs to be fixed.
Don't know if it is a mistake in indentation, or you miss-copied the code from somewhere ?
anyways to get it run, make this minor changes and it runs (Don't expect an explanation I doubt you don't need it)
#include <stdio.h>
int main()
{
int array[8] = {4, 1, 5, 4, 2, 7, 4, 2};
int i, j, k;
int len = 8;
for(i = 0; i < len; i++)
{
for(j = i + 1; j < len;)
{
if(array[j] == array[i])
{
for(k = j; k < len - 1; k++)
{
array[k] = array[k + 1];
}
len--;
}
else
{
j++;
}
}
} // close the main `for` loop
// you used to print out after each iteration!
for(i = 0; i < len; i++)
{
printf("%d ", array[i]);
}
printf("\n");
return 0;
}