Problem with for loop on MATLAB

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

 Respuesta aceptada

Walter Roberson
Walter Roberson el 22 de Nov. de 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

13 comentarios

Jimmy
Jimmy el 22 de Nov. de 2012
Thanks, but my code doesn't work, I need someone to tell me what I'm doing wrong.
Walter Roberson
Walter Roberson el 22 de Nov. de 2012
Your "for" loop is wrong. Have you changed it yet?
Jimmy
Jimmy el 22 de Nov. de 2012
No, it should be like this?
for x =1:50;
Need a little help here, please.
Walter Roberson
Walter Roberson el 22 de Nov. de 2012
Yes, it should be.
The semi-colon is not strictly needed, but is advised if you have other statements on the same line.
Jimmy
Jimmy el 22 de Nov. de 2012
Editada: Jimmy el 22 de Nov. de 2012
I just fixed "for", but there's still a error, because I always get this answer with any number:
_
the even numbers are
0
the odd numbers are
50
the positive numbers are
50
the negative numbers are
0
Please, help me to fix the code, this is my last exercise...
Walter Roberson
Walter Roberson el 22 de Nov. de 2012
Your code only inputs one number, and then repeats the same code with the same number 50 times.
When the user is prompted to enter a number, is the user expected to enter a single number or 50 numbers?
Jimmy
Jimmy el 22 de Nov. de 2012
The user must enter 50 natural numbers. The algorithm must show how many of them are even, odd, negative and positive.
Your loop tests "num" in each step. "num" is a vector of numbers, so
if rem(num,2) == 0;
is calculating a vector of remainders, comparing those remainders to 0 and creating a boolean array, and applying "if" to that entire boolean array.
An evaluated expression is true when the result is nonempty and contains all nonzero elements (logical or real numeric). Otherwise, the expression is false.
Please re-read the documentation for "for", and in particular take a closer look at the examples. http://www.mathworks.com/help/matlab/ref/for.html
Jimmy
Jimmy el 22 de Nov. de 2012
So, I should have used mod instead of rem?
Walter Roberson
Walter Roberson el 22 de Nov. de 2012
mod() applied to a vector of numbers is going to calculate a vector of remainders, exactly the same. Whether you use mod() or rem() is not relevant to your problem.
Go back to the documentation for "for" and read the example that starts with "Step by increments"
Jimmy
Jimmy el 22 de Nov. de 2012
You're very nice for helping me, but I should apply those increments into this code? How?
Walter Roberson
Walter Roberson el 22 de Nov. de 2012
That example shows how to process a vector, a single element at a time.
Jimmy
Jimmy el 22 de Nov. de 2012
Editada: Jimmy el 22 de Nov. de 2012
Thanks.

Iniciar sesión para comentar.

Más respuestas (1)

Muruganandham Subramanian
Muruganandham Subramanian el 22 de Nov. de 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);

7 comentarios

Jimmy
Jimmy el 22 de Nov. de 2012
Oh, awesome, I was so blind...
Thank you very much!
No, the code should be
for x = num
and remove the "x = x + 1" at the end.
Muruganandham Subramanian
Muruganandham Subramanian el 22 de Nov. de 2012
No,It won't work ever..
Walter Roberson
Walter Roberson el 22 de Nov. de 2012
What will not work, Muruganandham ? Remember, "num" is to be entered as a vector by the user at the input() prompt.
I did say, do not use
for x = 1:num
use
for x = num
Ilham Hardy
Ilham Hardy el 22 de Nov. de 2012
Walter,
num is a single number instead of a vector of 50 numbers.
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."

Iniciar sesión para comentar.

Categorías

Productos

Preguntada:

el 21 de Nov. de 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by