Last active
May 3, 2017 15:34
-
-
Save skhozinova/219772a7d7deb08445cf91ad4fa3f39d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
#include<locale.h> | |
#include<string.h> | |
#include<windows.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; | |
} | |
} | |
int main (void)//с массивом | |
{ | |
FILE *f; | |
if((f=fopen("in.txt","r"))==NULL)//обязательное условие открытия файла | |
{ | |
printf("Невозможно открыть файл\n"); | |
return 0; | |
} | |
int i,j,m,n,a[100][100],s[100],t=0; | |
char c; | |
while((c=getc(f))!=EOF) //тоже обязательно(до конца файла) | |
{ | |
ungetc(c,f); | |
fscanf(f,"%d",&n); | |
fscanf(f,"%d",&m); | |
for(i=0;i<n;i++) | |
{ | |
for(j=0;j<m;j++) | |
{ | |
fscanf(f,"%d",&a[i][j]); | |
} | |
} | |
break; | |
} | |
fclose(f); //закрытие файла "in.txt" | |
printf("Размерность матрицы:[%d*%d]\n Неотсортированный массив:\n",n,m); | |
for(i=0;i<n;i++) | |
{ | |
for(j=0;j<m;j++) | |
{ | |
printf("%d ",a[i][j]); | |
} | |
printf("\n"); | |
} | |
for(j=1;j<n-1;j++) | |
{ | |
for(i=0;i<n-j;i++) | |
{ | |
s[t]=a[i][i+j]; | |
t++; | |
} | |
insertion_sort(s,t); //использование функции сортировки | |
t=0; | |
for(i=0;i<n-j;i++) | |
{ | |
a[i][j+i]=s[t]; | |
t++; | |
} | |
t=0; | |
} | |
printf("Oтсортированный массив:\n",n,m); | |
for(i=0;i<n;i++) | |
{ | |
for(j=0;j<m;j++) | |
{ | |
printf("%d ",a[i][j]); | |
} | |
printf("\n"); | |
} | |
FILE *fp; | |
if((fp=fopen("out.txt","w"))!=NULL) //условие открытия файла уже "out.txt" | |
{ | |
fprintf(fp,"%d ",n); | |
fprintf(fp,"%d\n",m); | |
for(i=0;i<n;i++) | |
{ | |
for(j=0;j<m;j++) | |
{ | |
fprintf(fp,"%d ",a[i][j]); | |
} | |
fprintf(fp,"\n"); | |
} | |
fclose(fp);//закрытие файла "out.txt" | |
} | |
else | |
{ | |
printf("Невозможно открыть файл\n"); | |
return 0; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 76 89 | |
34 56 3 | |
23 46 97 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment