In c++ va rog
Sa se afle cate elemente negative sunt pe liniile pare si impare a unei matrice.
Răspunsuri la întrebare
In C++ am facu pentru elementele negativa dar am calulat si suma lor
#include <iostream>
#include <stdlib.h>
void main()
{
int mas[10];
int number = 0;
int negSum = 0;
for (int i = 0; i < 10; ++i)
{
mas[i] = rand()%30 - 15;
std::cout << mas[i] << " ";
}
for (int i = 0; i < 10; ++i)
{
if (mas[i] < 0)
{
negSum += mas[i];
number++;
}
}
std::cout << "\nsum = " << negSum <<
"\nnumber of negative elements = " << number << std::endl;
system("pause");
}
Mai departe singur
Am facut in Pascal doar pentru elementele negative:
const
n=5;
m=3;
var
a:array[1..n,1..m] of integer;
k,i,j:integer;
begin
randomize;
for i:=1 to n do
begin
for j:=1 to m do
begin
a[i,j]:=random(21)-10;
write(a[i,j]:4);
end;
writeln;
end;
writeln;
for i:=1 to n do
begin
k:=0;
for j:=1 to m do
if a[i,j]<0 then
inc(k);
writeln('K= ',k);
end;
end.