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]);
}
}
Related
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;
}
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.
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.
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;
}
}
For example an array: {1, 2, 3}
How do I make a program that loops this array to get an output of {1, -1, 2, -2, 3, -3}?
I tried doing something like:
#include <stdio.h>
int main()
{
int n = 3;
int arr[3] = {1, 2, 3};
int result[6];
int i, j, k;
for(i = 0; i < n*2; i++)
{
for(j = 0; j < n; j++)
{
for(k = 0; k < 2; k++)
{
if((i+1) % 2 != 0)
{
result[i] = arr[j];
}
else if((i+1) % 2 == 0)
{
result[i] = -arr[j];
}
}
}
}
for(i = 0; i < n*2; i++)
{
printf("%d ", result[i]);
}
}
But it only outputs {3, -3, 3, -3, 3, -3}
I thought my logic was perfect xD . Can anyone help?
Simple implementation: just store positive and negative values.
#include <stdio.h>
int main(void)
{
int n = 3;
int arr[3] = {1, 2, 3};
int result[6];
int i;
for (i = 0; i < n; i++)
{
result[i * 2] = arr[i];
result[i * 2 + 1] = -arr[i];
}
for(i = 0; i < n * 2; i++)
{
printf("%d\n", result[i]);
}
return 0;
}
This block seems to work:
int src[] = { 1, 2, 3};
int dest[(sizeof(src) / sizeof(int)) * 2];
int destCount = 0;
for (int i = 0; i < sizeof(src) / sizeof(int); i++) {
dest[destCount++] = abs(src[i]);
dest[destCount++] = -(abs(src[i]));
}
for (int i = 0; i < (sizeof(src) / sizeof(int)) * 2; i++) {
printf("%d\n", dest[i]);
}
I put in abs() calls to make sure you get a positive number followed by a negative number. If this isn't what you want, then just get rid of them.
Or if you just want output you could do this:
#include <stdio.h>
int main(void)
{
int n = 3;
int arr[3] = {1, 2, 3};
int i;
for (i = 0; i < n; i++)
{
printf("%d ",arr[i]);
printf("%d ",-1*arr[i]);
}
return 0;
}