var x: array[1..n] of integer; i, min, max: integer;
begin Randomize; Writeln('Элементы массива'); min := 32767; max := -32768; for i := 1 to n do begin x[i] := Random(101) - 50; Write(x[i]:4); if (x[i] > 0) and (min > x[i]) then min := x[i] else if (x[i] < 0) and (max < x[i]) then max := x[i] end; Writeln; Writeln('Min=', min, ', max=', max) end.
var x: array[1..n] of integer; i, max, max1: integer;
begin Randomize; Writeln('Элементы массива'); max := -32768; max1 := max; for i := 1 to n do begin x[i] := Random(100); Write(x[i]:3); if max < x[i] then begin max1 := max; max := x[i] end else if max1 < x[i] then max1 := x[i] end; Writeln; Writeln('Второй максимум равен ', max1) end.
float average(int* a, int first, int last) { int s = 0; for(int i = first; i <= last; ++i) s += a[i]; return s * 1. / (last - first + 1); } int min_i(int* a, int n) { int m = a[0]; int numb = 0; for(int i = 1; i < n; ++i) if(m > a[i]) { m = a[i]; numb = i; } return numb; } int max_i(int* a, int n) { int m = a[0]; int numb = 0; for(int i = 1; i < n; ++i) if(m < a[i]) { m = a[i]; numb = i; } return numb; } int main() { int N; std::cin >> N; int X[N]; for(int i = 0; i < N; ++i) std::cin >> X[i]; std::cout << "Average of whole array: " << average(X, 0, N - 1) << std::endl; int first = min_i(X, N); int last = max_i(X, N); if(first > last) { int temp = first; first = last; last = temp; } if(last - first == 0 || last - first == 1) std::cout << "Average of shorter array: " << average(X, 0, N - 1) << std::endl; else { int delta = last - first; for(int i = last; i < N; ++i) X[i - delta + 1] = X[i]; std::cout << "Average of shorter array: " << average(X, 0, N - delta) << std::endl; } }
n = 15;
var
x: array[1..n] of integer;
i, min, max: integer;
begin
Randomize;
Writeln('Элементы массива');
min := 32767; max := -32768;
for i := 1 to n do
begin
x[i] := Random(101) - 50;
Write(x[i]:4);
if (x[i] > 0) and (min > x[i]) then min := x[i]
else
if (x[i] < 0) and (max < x[i]) then max := x[i]
end;
Writeln;
Writeln('Min=', min, ', max=', max)
end.
Тестовое решение:
Элементы массива
29 -28 -14 -46 -22 13 -6 16 24 -47 -1 39 49 47 -23
Min=13, max=-1
const
n = 15;
var
x: array[1..n] of integer;
i, max, max1: integer;
begin
Randomize;
Writeln('Элементы массива');
max := -32768; max1 := max;
for i := 1 to n do
begin
x[i] := Random(100);
Write(x[i]:3);
if max < x[i] then begin max1 := max; max := x[i] end
else
if max1 < x[i] then max1 := x[i]
end;
Writeln;
Writeln('Второй максимум равен ', max1)
end.
Тестовое решение:
Элементы массива
37 66 92 11 39 32 70 17 28 80 51 37 1 56 41
Второй максимум равен 80
float average(int* a, int first, int last)
{
int s = 0;
for(int i = first; i <= last; ++i)
s += a[i];
return s * 1. / (last - first + 1);
}
int min_i(int* a, int n)
{
int m = a[0];
int numb = 0;
for(int i = 1; i < n; ++i)
if(m > a[i])
{
m = a[i];
numb = i;
}
return numb;
}
int max_i(int* a, int n)
{
int m = a[0];
int numb = 0;
for(int i = 1; i < n; ++i)
if(m < a[i])
{
m = a[i];
numb = i;
}
return numb;
}
int main()
{
int N;
std::cin >> N;
int X[N];
for(int i = 0; i < N; ++i)
std::cin >> X[i];
std::cout << "Average of whole array: " << average(X, 0, N - 1) << std::endl;
int first = min_i(X, N);
int last = max_i(X, N);
if(first > last)
{
int temp = first;
first = last;
last = temp;
}
if(last - first == 0 || last - first == 1)
std::cout << "Average of shorter array: " << average(X, 0, N - 1) << std::endl;
else
{
int delta = last - first;
for(int i = last; i < N; ++i)
X[i - delta + 1] = X[i];
std::cout << "Average of shorter array: " << average(X, 0, N - delta) << std::endl;
}
}