Problem with for loop on MATLAB

on 21 Nov 2012

Walter Roberson (view profile)

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
`    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
```

Jimmy

Jimmy (view profile)

on 22 Nov 2012

OK, I edited the tag.

Products

Walter Roberson (view profile)

on 22 Nov 2012

In MATLAB, the line

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

means:

1. Start a for loop. The index variable will be named "x". The first value for "x" is 1.
2. inside the "for" loop, every iteration, calculate whether x <= 50, creating a boolean reasult. Throw away the boolean result.
3. 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
4. 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

Jimmy

Jimmy (view profile)

on 22 Nov 2012

You're very nice for helping me, but I should apply those increments into this code? How?

Walter Roberson

Walter Roberson (view profile)

on 22 Nov 2012

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

Jimmy

on 22 Nov 2012

Thanks.

Muruganandham Subramanian (view profile)

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);`

Walter Roberson

Walter Roberson (view profile)

on 22 Nov 2012

I did say, do not use

```for x = 1:num
```

use

```for x = num
```
Ilham Hardy

Ilham Hardy (view profile)

on 22 Nov 2012

Walter,

num is a single number instead of a vector of 50 numbers.

Walter Roberson

Walter Roberson (view profile)

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
```

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

Join the 15-year community celebration.

Play games and win prizes!

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi