Skip to content

Instantly share code, notes, and snippets.

@skhozinova
Last active May 4, 2017 13:31
Show Gist options
  • Save skhozinova/23108d7a80e931c2c9888009c1f56b37 to your computer and use it in GitHub Desktop.
Save skhozinova/23108d7a80e931c2c9888009c1f56b37 to your computer and use it in GitHub Desktop.
3
15 76 34
34 56 67
9 13 18
#include<stdio.h>
#include<stdlib.h>
#include<locale.h>
#include<string.h>
void insertion_sort(int *a,int t)
{
int i,j,temp;
for (i=1;i<t;i++)
{
temp=a[i];
j=i-1;
while(j>=0 && a[j]>temp)
{
a[j+1] =a[j];
j--;
}
a[j+1]=temp;
}
}
void print_matrix(int matrix_size, int matrix[100][100])
{
int i, j;
for (i = 0; i < matrix_size; i++)
{
for (j = 0; j < matrix_size; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main (void)//с массивом
{ setlocale(LC_ALL, "Russian");
int i,j,a[100][100],s[100],t=0;
char c;
int matrix_size = 0;
/*FILE *f;
if((f=fopen("in.txt","r"))==NULL)
{
printf("Невозможно открыть файл '%s'\n", "in.txt");
exit(1);
}
while((c=getc(f))!=EOF)
{
ungetc(c,f);
fscanf(f,"%d",&matrix_size);
for(i=0;i<matrix_size;i++)
{
for(j=0;j<matrix_size;j++)
{
fscanf(f,"%d",&a[i][j]);
}
}
}
fclose(f); */
printf("Размерность матрицы:[%d*%d]\nНеотсортированный массив:\n", matrix_size);
print_matrix(matrix_size, a);
for (j = 1; j < matrix_size - 1; j++)
{
// read diagonal row
for (i = 0; i < matrix_size - j; i++)
{
s[t] = a[i][i + j];
printf("s[t]:%d , t:%d\n", s[t],t);
t++;
}
// sort diagonal
insertion_sort(s, t);
t = 0;
// save sorted diagonal into the original matrix
for (i = 0; i < matrix_size - j; i++)
{
a[i][j + i] = s[t];
printf("a[t]:%d , t:%d\n", a[t],t);
t++;
}
t = 0;
}
printf("Oтсортированный массив: %d \n", matrix_size);
print_matrix(matrix_size, a);
FILE *fp;
if ((fp = fopen("out.txt", "w")) != NULL)
{
fprintf(fp, "%d\n", marix_size);
for (i = 0; i < matrix_size; i++)
{
for (j = 0; j < matrix_size; j++)
{
fprintf(fp, "%d ", a[i][j]);
}
fprintf(fp, "\n");
}
fclose(fp);
}
else
{
printf("Can't open the file '%s'\n", "out.txt");
exit(1);
}
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment