Asked by Jimmy
on 21 Nov 2012

Hey, guys, one last question for today...

I need to write an algorithm that determines 50 numbers and print how many are even, odd, positive and negative.

These are the code and pseudocode I wrote, but for some reason the "for" loop doesn't work correctly. ¿What is causing this?

**PSEUDOCODE**

even, odd, positive, negative even ← 0 odd ← 0 positive ← 0 negative ← 0

For x ← 1 to 50 then Read n

If n % 2 = 0 then

even ← even + 1

else odd <- odd + 1

end

If n > 0 then

positive ← positive + 1

else negative ← negative + 1

end

disp even, odd, positive, negative

**CODE:**

num = input('Enter a number: '); p = 0; %even im = 0; %odd pos = 0; %positives n = 0; %negatives for x = 1; x <= 50; x = x+1; if rem(num,2) == 0; p = p + 1; else im = im + 1; if num > 0 pos = pos + 1; else n = n + 1; end end end

disp('the even numbers are'); disp(p); disp('the odd numbers are') disp(im); disp('the positive numbers are') disp(pos); disp('the negative numbers are') disp(n);

For example, these are the answers I'm getting for the number 50.

Enter a number: 50 the even numbers are 1 the odd numbers are 0 the positive numbers are 0 the negative numbers are 0

And this is the correct answer for 50.

Enter a number: 50 the even numbers are 25 the odd numbers are 25 the positive numbers are 50 the negative numbers are 0

Answer by Walter Roberson
on 22 Nov 2012

Accepted answer

In MATLAB, the line

for x = 1; x <= 50; x = x+1;

means:

- Start a for loop. The index variable will be named "x". The first value for "x" is 1.
- inside the "for" loop, every iteration, calculate whether x <= 50, creating a boolean reasult. Throw away the boolean result.
- next inside the "for" loop, every iteration, assign x+1 to x, so x will become 2 as the second action in the first iteration
- when the "end" is reached, check to see what the next value to iterate to is. As the list of values was only "1", there is no next value in the list "1", exit the loop, leaving "x" at its last value.

If this was not your intention, you might want to read the documentation for "for" at http://www.mathworks.com/help/matlab/ref/for.html

Show 10 older comments

Walter Roberson
on 22 Nov 2012

That example shows how to process a vector, a single element at a time.

Answer by Muruganandham Subramanian
on 22 Nov 2012

Hi,

try this code:

num = input('Enter a number: ');

p = 0; %even

im = 0; %odd

pos = 0; %positives

n = 0; %negatives

for x = 1:num;

if rem(x,2) == 0;

p = p + 1;

else

im = im + 1;

end

if x > 0

pos = pos + 1;

else

n = n + 1;

end

x = x+1;

end

disp('the even numbers are');

disp(p);

disp('the odd numbers are')

disp(im);

disp('the positive numbers are')

disp(pos);

disp('the negative numbers are')

disp(n);

Show 4 older comments

Walter Roberson
on 22 Nov 2012

I saved the file as negs.m after changing the "for" line to "for x = num". I then ran:

>> negs Enter a number: [-5 9 12 14 16 -1] the even numbers are 3

the odd numbers are 3

the positive numbers are 4

the negative numbers are 2

Is that the incorrect answer?

I did ask above what the input was at the prompt, and you specifically said "The user must enter 50 natural numbers."

Opportunities for recent engineering grads.

## 1 Comment

## Jimmy (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/54443#comment_112586

OK, I edited the tag.