calling one program to another
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
I have a program that has a function and takes an input 'n' and runs it through the hailstone sequence and counts the number of outputs it goes through until if ends at 1. I need to write another function that takes as input n and whose output is the number less than n that has the longest Collatz sequence and the length of that maximal sequence.
the thing is, the second program MUST be a function and it MUST call the first function without just copy-pasting the first function, i have no idea how to do this...
0 comentarios
Respuesta aceptada
Image Analyst
el 10 de Sept. de 2013
Editada: Image Analyst
el 10 de Sept. de 2013
It's not clear what first and second program, and first and second function are, but I'll take a guess.
In a file called hailstone.m
function numberOfOutputs = hailstone(n)
% Write the code here.
In a file called collatz_sequence.m
function [theNumber, lengthMaxSequence] = collatz_sequence(n)
% Code here to calculate theNumber, and lengthMaxSequence.
In the main, first program called first_program.m:
% Call the hailstone function
n = 42; % whatever...
numberOfOutputs = hailstone(n)
% Call the other function
[theNumber, lengthMaxSequence] = collatz_sequence(n)
In the other, second program called second_program.m:
function second_program()
% Call the "first" function (whatever that is - it's not clear).
% Let's assume that the "first" function is the hailstone function.
n = 42; % whatever...
numberOfOutputs = hailstone(n)
2 comentarios
Image Analyst
el 10 de Sept. de 2013
You answered as I realized the ambiguity in your question and was trying to be more clear. See my edit, however you are still not being clear. I have no idea how to write the code inside the function - I guess that's some algorithm you learned in class so you should be able to do it.
Más respuestas (0)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!