#include <iostream>
<span>#include <ctime>
</span><span>using namespace std;
</span>int main() {
<span> int n,m;
</span><span> FILE *fpt;
</span><span> cout<<"n = "; cin>>n;
</span><span> fpt=fopen("input.dat","wb");
</span><span> srand(time(NULL));
</span><span>// запись файла
</span><span> for (int i=0; i<n; i++) {
</span><span> m=rand()%8000-3000;
</span><span> cout<<m<<" ";
</span><span> fwrite(&m,sizeof(int),1,fpt);
</span><span> }
</span><span> cout<<endl;
</span><span> fclose(fpt);
</span><span> fpt=fopen("input.dat","rb+");
</span><span> int indf=0,indl=0,vf=0,vl=0,k=0;
</span><span>// чтение файла
</span><span> while (fread(&m,sizeof(int),1,fpt)!=0) {
</span><span> k++;
</span><span> if (!(m%2==0) && (indf==0)) { indf=k-1; vf=m; }
</span><span> if ((!m==0) && (m%2==0)) { indl=k-1; vl=m; }
</span><span> }
</span><span> cout<<"first odd = "<<vf<<" index = "<<indf<<endl;
</span><span> cout<<"last even = "<<vl<<" index = "<<indl<<endl;
</span><span>// обмен первого нечетного и последнего четного
</span><span> if (indf>0) {
</span><span> fseek(fpt,sizeof(int)*indf,SEEK_SET);
</span><span> fwrite(&vl,sizeof(int),1,fpt);
</span><span> }
</span><span> if (indl>0) {
</span><span> fseek(fpt,sizeof(int)*indl,SEEK_SET);
</span><span> fwrite(&vf,sizeof(int),1,fpt);
</span><span> }
</span><span> fclose(fpt);
</span><span> system("pause");
</span><span> return 0;
</span><span>}
</span>n = 6
<span>4368 2733 1112 2620 1941 753
</span><span>first odd = 2733 index = 1
</span><span>last even = 2620 index = 3
</span>
1) function MaxArr(a: array of integer): integer;
var i,l,ma: integer;
begin
l:=high(a);
ma:=a[0];
for i:=1 to l do
if a[i]>ma then ma:=a[i];
MaxArr:=ma;
end;
var a: array of array of integer;
i,j,l: integer;
begin
write('Введите длину матрицы: ');
readln(l);
setlength(a,l);
for i:=0 to l-1 do setlength(a[i],l);
for i:=0 to l-1 do
begin
writeln;
for j:=0 to l-1 do
begin
a[i,j]:=random(50)+1;
write(a[i,j],' ');
end;
end;
a[0,0]:=MaxArr(a[0]);
a[1,1]:=MaxArr(a[1]);
a[2,2]:=MaxArr(a[2]);
a[3,3]:=MaxArr(a[3]);
a[4,4]:=MaxArr(a[4]);
writeln;
writeln('Преобразованный массив');
for i:=0 to l-1 do
begin
writeln;
for j:=0 to l-1 do
begin
write(a[i,j],' ');
end;
end;
end.
2) function SumArr(a: array of integer): integer;
var i,l,s: integer;
begin
l:=high(a);
s:=0;
for i:=0 to l do
s:=s+a[i];
SumArr:=s;
end;
var a: array of array of integer;
i,j,n,m,s: integer;
begin
write('Введите длину матрицы N: ');
readln(n);
write('Введите длину матрицы M: ');
readln(m);
setlength(a,n);
for i:=0 to n-1 do setlength(a[i],m);
for i:=0 to n-1 do
begin
writeln;
for j:=0 to m-1 do
begin
a[i,j]:=random(50)+1;
write(a[i,j],' ');
end;
end;
s:=0;
for i:=0 to n-1 do s:=s+SumArr(a[i]);
writeln;
writeln('Сумма всех элементов матрицы равна: ',s);
end.
Var
i, s: integer;
begin
s := 0;
for i := 10 to 99 do
if i mod 2 = 0 then
s := s + i;
writeln(s);
end.