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; } }
//Pascal var A: array [1..5] of integer; min, max, minInd, MaxInd: integer; i: byte; begin writeln('Заполнение массива.'); for i := 1 to 5 do begin write('А[', i, '] = '); readln(A[i]); end; writeln; Max := A[1]; Min := A[1]; for i := 1 to 5 do begin if Max < A[i] then begin Max := A[i]; MaxInd := i; end; if Min > A[i] then begin Min := A[i]; MinInd := i; end; end; writeln('Номер минимального элемента = ', MinInd); writeln('Номера минимального и максимального = ', MinInd, ' и ',MaxInd); 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;
}
}
var
A: array [1..5] of integer;
min, max, minInd, MaxInd: integer;
i: byte;
begin
writeln('Заполнение массива.');
for i := 1 to 5 do
begin
write('А[', i, '] = '); readln(A[i]);
end;
writeln;
Max := A[1]; Min := A[1];
for i := 1 to 5 do
begin
if Max < A[i] then begin Max := A[i]; MaxInd := i; end;
if Min > A[i] then begin Min := A[i]; MinInd := i; end;
end;
writeln('Номер минимального элемента = ', MinInd);
writeln('Номера минимального и максимального = ', MinInd, ' и ',MaxInd);
end.