#include
#include
using namespace std;
int main() {
int n,m;
FILE *fpt;
cout<<"n = "; cin>>n;
fpt=fopen("input.dat","wb");
srand(time(NULL));
// запись файла
for (int i=0; i m=rand()%8000-3000;
cout<<m<<" ";<br> fwrite(&m,sizeof(int),1,fpt);
}
cout<<endl;<br> fclose(fpt);
fpt=fopen("input.dat","rb+");
int indf=0,indl=0,vf=0,vl=0,k=0;
// чтение файла
while (fread(&m,sizeof(int),1,fpt)!=0) {
k++;
if (!(m%2==0) && (indf==0)) { indf=k-1; vf=m; }
if ((!m==0) && (m%2==0)) { indl=k-1; vl=m; }
}
cout<<"first odd = "<<vf<<" index = "<<indf<<endl;<br> cout<<"last even = "<<vl<<" index = "<<indl<<endl; <br>// обмен первого нечетного и последнего четного
if (indf>0) {
fseek(fpt,sizeof(int)*indf,SEEK_SET);
fwrite(&vl,sizeof(int),1,fpt);
}
if (indl>0) {
fseek(fpt,sizeof(int)*indl,SEEK_SET);
fwrite(&vf,sizeof(int),1,fpt);
}
fclose(fpt);
system("pause");
return 0;
}
n = 6
4368 2733 1112 2620 1941 753
first odd = 2733 index = 1
last even = 2620 index = 3