/* * Сумма элементов массива * [ 1, 2, 3] => 6 * [-5, 8, 2] => 5 */ int arraySum(int a[], int s) { int ret = 0; for (int i = 0; i < s; i++) { ret += a[i]; } return ret; }
/* * Определение, каких чисел больше в массиве. * [-1, 2, 3] => "Положительных" * [ 1, -2, -3] => "Отрицательных" */ const char* plusMinGreater(int a[], int s) { int plus = 0, minus = 0; for (int i = 0; i < s; i++) { if (a[i] > 0) plus++; if (a[i] < 0) minus++; } return (plus > minus ? "Положительных чисел больше" : (plus < minus ? "Отрицательных чисел больше" : "Положителных и отрицательных поровну")); }
/* * Разница между максимальным и минимальным элементами. * [5, 3, 2] => 5 - 2 = 3 */ int maxMinDiff(int a[], int s) { int min = a[0], max = a[0]; for (int i = 1; i < s; i++) { if (a[i] > max) max = a[i]; if (a[i] < min) min = a[i]; } return max - min; }
int main() { int n; cout << "n = "; cin >> n;
int a[n]; for (int i = 0; i < n; i++) { cout << "Число " << i << ": "; cin >> a[i]; }
Program one;uses crt; const k = 10; var S: array[1..k] of integer; i, min: integer; begin randomize; writeln('Array:'); for i := 1 to k do begin S[i] := random(20); write(S[i], ' '); end; writeln; min := S[1]; for i := 2 to k do if S[i] < min then min := S[i]; writeln('Min: ', min); writeln('Result:'); for i := 1 to k do begin S[i] := S[i] - min; write(S[i], ' '); end; end.
program two; uses crt; const m = 5; k = 5; var A: array[1..m] of array[1..k] of integer; i, j, min: integer; begin randomize; writeln('Matrix:'); for i := 1 to m do begin for j := 1 to k do begin A[i][j] := random(20) - 10; write(A[i][j], ' '); end; writeln; end; writeln('Result:'); for i := 1 to m do begin for j := 1 to k do begin if A[i][j] > 0 then begin write(A[i][j], ' '); break; end; end; end; end.
using namespace std;
/*
* Сумма элементов массива
* [ 1, 2, 3] => 6
* [-5, 8, 2] => 5
*/
int arraySum(int a[], int s)
{
int ret = 0;
for (int i = 0; i < s; i++) {
ret += a[i];
}
return ret;
}
/*
* Определение, каких чисел больше в массиве.
* [-1, 2, 3] => "Положительных"
* [ 1, -2, -3] => "Отрицательных"
*/
const char* plusMinGreater(int a[], int s)
{
int plus = 0, minus = 0;
for (int i = 0; i < s; i++) {
if (a[i] > 0) plus++;
if (a[i] < 0) minus++;
}
return (plus > minus ?
"Положительных чисел больше" :
(plus < minus ?
"Отрицательных чисел больше" :
"Положителных и отрицательных поровну"));
}
/*
* Разница между максимальным и минимальным элементами.
* [5, 3, 2] => 5 - 2 = 3
*/
int maxMinDiff(int a[], int s)
{
int min = a[0], max = a[0];
for (int i = 1; i < s; i++) {
if (a[i] > max) max = a[i];
if (a[i] < min) min = a[i];
}
return max - min;
}
int main()
{
int n;
cout << "n = ";
cin >> n;
int a[n];
for (int i = 0; i < n; i++) {
cout << "Число " << i << ": ";
cin >> a[i];
}
cout << plusMinGreater(a, n) << endl
<< "Сумма: " << arraySum(a, n) << endl
<< "Разница максимального и минимального элементов: "
<< maxMinDiff(a, n) << endl;
return 0;
}