36.0k views
1 vote
Write a MATLAB program using afor loopto determine the number of values thatarepositive, the number of values that are negative, and the number of values that equal zero ina vector containing N elements. Prompt the user to enter N and the vector of values and thendisplay the number of positive, negative, and zero values.

User Parishodak
by
5.7k points

1 Answer

1 vote

Answer:

see description

Step-by-step explanation:

clicking in new script, we type the following:

%inputs and variables

N = input('Enter N : ');

values = input('Enter values : ');

positive=0;

negatives=0;

zero=0;

%for loop

for c = 1:N

if values(c) > 0

positive = positive + 1;

elseif values(c) < 0

negatives = negatives + 1;

else

zero = zero + 1;

end

end

positive

negatives

zero

so we basically loop the array and add one to counters each time we read an element from the input, for example if we enter:

Example input

Enter N : 5

Enter values : [1,-1,0,2,352]

positive = 3

negatives = 1

zero = 1

User Damask
by
6.1k points