Borrar filtros
Borrar filtros

count function, whwn i enter numbers and its wrong the count isnt going read the commnets i made

2 visualizaciones (últimos 30 días)
choice= input('Enter a vector sufficent for an odd-order polynomial: '); % inputs an even or odd vector [1]- odd [1 2]-even
a=length(choice);
Count = 1;
%b = mod(a,2);
while Count < 5 && mod(a,2)==0
choice= input('Enter a vector sufficent for an odd-order polynomial: ');
Count = Count+1; % this part isnt working for some reason
if Count > 5
warning('Odd number of coefficients entered. Last element removed');
choice(:,end)=[];
end
end
if mod(a,2)~=0
fprintf('Please enter a valid vector.\n');
end

Respuestas (1)

Walter Roberson
Walter Roberson el 25 de Feb. de 2020
Editada: Walter Roberson el 25 de Feb. de 2020
Suppose Count has reached 3 and mod(a, 2)==0. The loop will continue. choice will be input. Count will be incremented to 4. 4 is not greater than 5 so the last element of choice is not removed.
We return to the loop test. Count is 4 and that is less than 5. a has not changed so mod(a, 2) is still 0. So you will prompt for choice again, and then you will increment Count from 4 to 5. 5 is not greater than 5 so the last element of choice is not removed. Notice that the content of choice from the last iteration was ignored.
We return to the loop test. Count is 5 which is not < 5 so the loop terminates.
If you think about this, the test Count>5 can not be satisfied, so there is no point in having that code.
What went wrong? This:
You failed to update "a" after you changed the content of choice
  2 comentarios
Alex Doan
Alex Doan el 25 de Feb. de 2020
can you like highlight the part that needs to be fixed im not understanding lol
Walter Roberson
Walter Roberson el 25 de Feb. de 2020
My last paragraph already highlights what needs to be changed.
You initialize a when you read in choice the first time. After that you never change a. That is the mistake. You need to change a every time you change choice.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by