Subfunction help with triangles

20 ビュー (過去 30 日間)
Ryan Da Great
Ryan Da Great 2018 年 10 月 6 日
コメント済み: Rik 2018 年 10 月 10 日
For a right triangle with sides a, b, c, where c is the hypotenuse. Write a script that calls a function to prompt the user and read in values for the sides a and b then calls a function to calculate and return the hypotenuse and a function to print out all values in a sentence format. (Note: You need to write three different functions)Here is an example of running the script: Enter the side a: 5 Enter the side b: 12 For a right triangle with sides 5.0 and 12.0,the hypotenuse is 13.0.
  2 件のコメント
Ryan Da Great
Ryan Da Great 2018 年 10 月 6 日
my code so far:
function getinputs=righttri
A=input('Enter side A:');
B=input('Enter side B:');
C=calcside
end
function C=calcside
C=sqrt(A^2+B^2);
reslult=printR
end
function reslult=printR
fprintf('For a right triangle with sides %d and %d,\n',A,B)
fprintf('the hypotenuse is %d,\n',C)
end
madhan ravi
madhan ravi 2018 年 10 月 7 日
編集済み: madhan ravi 2018 年 10 月 7 日
what's your error/question ?

サインインしてコメントする。

回答 (1 件)

Rik
Rik 2018 年 10 月 7 日
You need to provide the subfunctions with inputs.
function getinputs=righttri
A=input('Enter side A:');
B=input('Enter side B:');
C=calcside(A, B) ;
end
function C=calcside(A, B)
C=sqrt(A^2+B^2);
printR(A, B, C)
end
function printR(A, B, C) %this function has no output
fprintf('For a right triangle with sides %d and %d,\n',A,B)
fprintf('the hypotenuse is %d,\n',C)
end
  1 件のコメント
Rik
Rik 2018 年 10 月 10 日
Did this suggestion solve your problem? If so, please consider marking it as accepted answer. It will make it easier for other people with the same question to find an answer. If this didn't solve your question, please comment with what problems you are still having.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSoftware Development Tools についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by