calling one program to another
1 view (last 30 days)
Show older comments
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 Comments
Accepted Answer
Image Analyst
on 10 Sep 2013
Edited: Image Analyst
on 10 Sep 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 Comments
Image Analyst
on 10 Sep 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.
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!