calling one program to another
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Image Analyst
2013-9-10
编辑:Image Analyst
2013-9-10
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 个评论
Image Analyst
2013-9-10
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!