Дан массив А ( 28 ) . Найти сумму положитель " ых элементов массива . Если сумма будет больше 100 , то обнулить элементы в чётных ячейках , ссли сумма будет меньше или равна 100 , то обнулить элементы в нечетных ячейках
Ответы на вопрос
Ответил pacixor
0
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int n = 28;
int main()
{
int A[n];
int sum = 0;
cout << "Source array: n";
srand(time(NULL));
for (int i = 0; i < n; i++) {
A[i] = rand() % 23 - 10;
if (A[i] > 0) sum += A[i];
cout << A[i] << " ";
}
cout << "nSum = " << sum << endl;
cout << "nNew array: n";
if (sum > 100)
for (int i = 0; i < n; i++)
{
if (i % 2 == 1) A[i] = 0;
cout << A[i] << " ";
}
else
for (int i = 0; i < n; i++)
{
if (i % 2 == 0) A[i] = 0;
cout << A[i] << " ";
}
cout << endl;
system("pause");
return 0;
}
#include <stdlib.h>
#include <time.h>
using namespace std;
const int n = 28;
int main()
{
int A[n];
int sum = 0;
cout << "Source array: n";
srand(time(NULL));
for (int i = 0; i < n; i++) {
A[i] = rand() % 23 - 10;
if (A[i] > 0) sum += A[i];
cout << A[i] << " ";
}
cout << "nSum = " << sum << endl;
cout << "nNew array: n";
if (sum > 100)
for (int i = 0; i < n; i++)
{
if (i % 2 == 1) A[i] = 0;
cout << A[i] << " ";
}
else
for (int i = 0; i < n; i++)
{
if (i % 2 == 0) A[i] = 0;
cout << A[i] << " ";
}
cout << endl;
system("pause");
return 0;
}
Ответил DonPedro80
0
const
n=28;
var
a:array[1..n] of integer;
i:byte;
s:integer;
begin
randomize;
for i:=1 to n do
begin
a[i]:=random(41)-20;
Write(a[i],' ');
if a[i]>0 then s:=s+a[i]
end;
writeln;
if s>100 then
begin
i:=2;
while i<=n do
begin
a[i]:=0;
i:=i+2
end
end
else
begin
i:=1;
while i<=n do
begin
a[i]:=0;
i:=i+2
end
end;
writeln('Сумма равна ',s);
for i:=1 to n do Write(a[i],' ');
writeln
end.
n=28;
var
a:array[1..n] of integer;
i:byte;
s:integer;
begin
randomize;
for i:=1 to n do
begin
a[i]:=random(41)-20;
Write(a[i],' ');
if a[i]>0 then s:=s+a[i]
end;
writeln;
if s>100 then
begin
i:=2;
while i<=n do
begin
a[i]:=0;
i:=i+2
end
end
else
begin
i:=1;
while i<=n do
begin
a[i]:=0;
i:=i+2
end
end;
writeln('Сумма равна ',s);
for i:=1 to n do Write(a[i],' ');
writeln
end.
Новые вопросы
Другие предметы,
2 года назад
Русский язык,
2 года назад
Геометрия,
8 лет назад
Математика,
8 лет назад
Литература,
9 лет назад