Problem with for loop on MATLAB
Mostrar comentarios más antiguos
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
1 comentario
Jimmy
el 22 de Nov. de 2012
Respuesta aceptada
Más respuestas (1)
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
el 22 de Nov. de 2012
Walter Roberson
el 22 de Nov. de 2012
No, the code should be
for x = num
and remove the "x = x + 1" at the end.
Muruganandham Subramanian
el 22 de Nov. de 2012
No,It won't work ever..
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.
Walter Roberson
el 22 de Nov. de 2012
I did say, do not use
for x = 1:num
use
for x = num
Ilham Hardy
el 22 de Nov. de 2012
Walter,
num is a single number instead of a vector of 50 numbers.
Walter Roberson
el 22 de Nov. de 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."
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!