Borrar filtros
Borrar filtros

I nee to change this str2double code while keeping the same output for my collatz function.

1 visualización (últimos 30 días)
The following is the script to my function:
function [steps,maxnum]=collatz(num)
str=input('Enter an integer greater than 1:','s');
num=str2double(str);
cnt=0;
maxnum=num;
while num>1
cnt=cnt+1;
if mod(num,2);
num=num*3+1;
else
num=num/2;
end
if num>maxnum; maxnum=num;
end
end
s=cnt
maxnum
I was told that I cannot use the str2double(str) function but I cannot figure out how to get the same output any other way. My script is supposed to take a number greater than 1 and apply a while loop by multiplying the integer by 3 then adding 1 if it is odd or dividing it by 2 if it is an even number until the function equals 1, where the loop ends. I need a different method for finding the steps it takes for the loop to end. For example if the user enters "3" the steps would be 3 → 10 → 5 → 16 → 8 → 4 → 2 → 1. I would need the output to be s=7 %meaning it took 7 steps to complete the loop m=16 %representing the maximum number reached during the loop
  3 comentarios
Stephen23
Stephen23 el 25 de Sept. de 2017
@KSSV: there is nothing wrong with the code. The problem is the ill-informed tutor.
Kevin Smith
Kevin Smith el 25 de Sept. de 2017
Editada: Kevin Smith el 25 de Sept. de 2017
I apologize I didn't even need that code. I wrote it as a script prompting a user for an integer instead of writing a function like I needed. As a script it had to be a string to find the maximum value, but I don't have to have a string to find the max value when it is a function.

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 25 de Sept. de 2017
Editada: Stephen23 el 25 de Sept. de 2017
str = input('Enter an integer greater than 1:','s');
num = sscanf(str,'%i');
Although str2double has the advantage that it returns NaN for incorrect inputs.
Tell your mysterious advisor that using input to return a string is much safer than getting input to evaluate whatever random things the user has given it, and that they should encourage their students to learn good (defensive) programming practices. Using input without returning a string causes it to evaluate the input given to it by the user, which is an awfully insecure, unsafe, and buggy way to write code, and all of the points listed here apply (particularly the insecure part):
You might like to read the discussion here too:
If your mysterious advisor then ill-advisedly insists that you really really need to use input by getting it evaluate whatever that user gives it then of course you will have to follow their instructions... and you will have learned the valuable lesson that people do not always know what they are talking about. You will know better.
  1 comentario
Stephen23
Stephen23 el 25 de Sept. de 2017
function [steps,maxnum] = collatz(num)
I didn't notice the function definition. You should simply remove the input command entirely.

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 25 de Sept. de 2017
Why are you prompting for a value instead of passing the value on the command line, since you do have num as a parameter for your function?

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by