DSA/Lab_5/code.c

110 lines
2.1 KiB
C

#include <stdio.h>
#include <stdlib.h>
void line_pr(short int len)
{
for (short int i = 0; i < len; i++) {
printf("-");
}
}
int main ()
{
short int rows, columns, indent, c_indent, line_len;
int temp;
printf("Input the number of rows and columns separated with a space: ");
scanf("%hd %hd", &rows, &columns);
int array[rows][columns];
for (short int i = 0; i < rows; i++) {
for (short int j = 0; j < columns; j++) {
printf("Input a value for %hd %hd: ", i + 1, j + 1);
scanf("%d", &array[i][j]);
}
}
printf("Matrix before sorting:\n");
indent = 6;
if (rows >= columns) {
c_indent = floor(log10(rows));
} else {
c_indent = floor(log10(columns));
}
c_indent +=2;
line_len = c_indent + indent * columns + columns * 2 + 1;
printf("%*c|", c_indent, *" ");
for (short int i = 1; i <= columns; i++) {
printf("%*d |", indent, i);
}
printf("\n");
line_pr(line_len);
printf("\n");
for (short int i = 0; i < rows; i++) {
printf("%*d|", c_indent, i+1);
for (short int j = 0; j < columns; j++) {
printf("%*d |", indent, array[i][j]);
}
printf("\n");
line_pr(line_len);
printf("\n");
}
for (short int i = 2; i < columns; i+=2) {
temp = array[0][i];
short int j = 0;
while (array[0][j] < temp) {
j+=2;
}
for (short int k = i - 2; k >= j; k-=2) {
array[0][k+2] = array[0][k];
}
array[0][j] = temp;
}
printf("Matrix after sorting:\n");
printf("%*c|", c_indent, *" ");
for (short int i = 1; i <= columns; i++) {
printf("%*d |", indent, i);
}
printf("\n");
line_pr(line_len);
printf("\n");
for (short int i = 0; i < rows; i++) {
printf("%*d|", c_indent, i+1);
for (short int j = 0; j < columns; j++) {
printf("%*d |", indent, array[i][j]);
}
printf("\n");
line_pr(line_len);
printf("\n");
}
return 0;
}