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!