Find the number of outputs of a value.

7 次查看(过去 30 天)
Hi,
I need help finding the number of outputs a function has. This function finds pythagorean triples given a maxumum value of the hypotenuse. Addditionally, it negates all outputs that are not positive integers. The output of the code looks like:
Input the maximum length of a triangle: 10
Triangle:
Side A = 3
Side B = 4
Hypotenuse = 5
x = 3 %x is the value of Side A
Triangle:
Side A = 6
Side B = 8
Hypotenuse = 10
x = 6
I need to find how many x values the function has produced which should be 2 for this. I could also solve my problem by numbering the triangle by n+1 so the first triangle would be 1, the second 2, and so forth.
Thanks
  7 个评论
Adam Danz
Adam Danz 2019-10-18
Good, I think that's the right course of action. Feel free to submit the updated code here as a comment. By the way, it's easier if you just copy-paste the code as a comment and then format it using the [ > ] button rather than attaching it as a PDF.
Zach Adams
Zach Adams 2019-10-18
编辑:Adam Danz 2019-10-18
function [A, B, hypotenuse] = M(C);
%This calculates the pythagorean triples given the length of the
%hypotenuse.
H;
disp([1:1:C]);
for max = 1:1:C
for m = round(sqrt(max/2)):round(sqrt(max))
m == sqrt(max/2) && m == sqrt(max);
if m == floor(m);
n = sqrt(max - m^2);
end
A = m^2 -n^2;
B = 2*m*n;
hypotenuse = m^2 + n^2;
if A > B;
temp_A=A;
A = B;
B = temp_A;
end
for hypotenuse > A && hypotenuse > B && hypotenuse^2 == B^2+A^2 && A >= 0 && A == round(A) && B >= 0 && B == round(B);
fprintf('Side A:')
A
fprintf('Side B:')
B
fprintf('Hypotenuse:')
hypotenuse
end
end
end
This is my new code, H asks for a value of C from the user. I now need to know how many values of A (it could also be B or C) were printed in order to find how many pythagorean triples were made.

请先登录,再进行评论。

采纳的回答

Adam Danz
Adam Danz 2019-10-18
@Zach Adams, first, more feedback. Please take some time to digest that. Second, I've improved your code without changing the algorithm and I've shown you how to determine the number of outputs.
Feedback on code
1) What is H and where does it come from? If H is supposed to be a variable, doesn't it cause an error? If H is a script or a function without any inputs, that needs improved for 2 reasons. a) "H" is a really bad function/file name. b) calling a script from a function defeats the advantages of functions - reproducibility and self-containment. If H is a function that merely asks for input you could move that code into his function. If H is a function that is used by other function, it's OK to keep it as it is but the name should really change. Functions should generally contain verbs or describe what it does. Example: getUserInput().
2) I see you have a variable named "max". You should avoid naming variables after common functions for two reasons. a) if you have a variable named max you will not be able to use the max() function anywhere in the same workspace and b) other people reading your code, including your future-self, will get confused because they are used to the "max" being a function. You can rename it to something like "maxC" if you'd like.
3) Use smart-indentation by selecting the entire code (ctrl + a) and then ctrl+i. I've corrected the code in your comment so that it has smart indentation. This makes it MUCH easier to read.
4) Your final for-loop is actually a conditional statement. Instead of "for hypotenuse > A &&..." use "if hypotenuse > A &&..."
5) 'M' is a very bad function name (same reasons as in point #1 above).
Cleaner version of your code
See inline comments.
function [A, B, hypotenuse] = findPythagTriplets(C)
%This calculates the pythagorean triples given the length of the hypotenuse.
% H; % H isn't even used anywhere
disp(1:1:C);
A = [];
B = [];
hypotenuse = [];
for maxC = 1:1:C
for m = round(sqrt(maxC/2)):round(sqrt(maxC))
% m == sqrt(maxC/2) && m == sqrt(maxC); %This line doesn't do anything at all
if m == floor(m)
n = sqrt(maxC - m^2);
end
A(end+1) = m^2 -n^2; %* The A(end+1) is generally bad practice but I didn't want to make major changes to your code
B(end+1) = 2*m*n; % this too
hypotenuse(end+1) = m^2 + n^2; %this too
if A(end) > B(end)
temp_A=A(end);
A(end) = B(end);
B(end) = temp_A;
end
if hypotenuse(end) > A(end) && hypotenuse(end) > B(end) && hypotenuse(end)^2 == B(end)^2+A(end)^2 && ...
A(end) >= 0 && A(end) == round(A(end)) && B(end) >= 0 && B(end) == round(B(end))
fprintf('Side A:')
disp(A(end))
fprintf('Side B:')
disp(B(end))
fprintf('Hypotenuse:')
disp(hypotenuse(end))
else
A(end) = [];
B(end) = [];
hypotenuse(end) = [];
end
end
end
Determine number of values in the output
To determine the number of triangles, just call the function with the output and count the number of values in the output variables.
[A, B, hypotenuse] = findPythagTriplets(10)
numberOfTriangles = numel(A)
% numberOfTriangles =
% 2
  2 个评论
Zach Adams
Zach Adams 2019-10-18
Thank you. Especially for sticking with me because I definitely learned a lot through this and I appreciate that.
Adam Danz
Adam Danz 2019-10-18
Thanks for the feedback! I was at the same stage several years ago so I know how ya feel.

请先登录,再进行评论。

更多回答(1 个)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2019-10-18
Hi,
Here is your code improved a bit and bugs removed:
function [A, B, hypotenuse] = M(C)
%This calculates the pythagorean triples given the length of the
%hypotenuse.
disp([1:1:C]);
for max = 1:1:C
for m = round(sqrt(max/2)):round(sqrt(max))
m == sqrt(max/2) && m == sqrt(max);
if m == floor(m)
n = sqrt(max - m^2);
end
A = m^2 -n^2;
B = 2*m*n;
hypotenuse = m^2 + n^2;
if A > B
temp_A=A;
A = B;
B = temp_A;
end
if hypotenuse>A && hypotenuse > B && hypotenuse^2 == B^2+A^2 && A >= 0 && A == round(A) && B >= 0 && B == round(B)
fprintf('Side A = %f \n', A)
fprintf('Side B = %f \n', B)
fprintf('Hypotenuse = %f \n', hypotenuse)
end
end
end
Good luck.
  1 个评论
Zach Adams
Zach Adams 2019-10-18
Thank you for cleaning it up, but i was wondering if there is a way to determine how many times the if statement displays a value for the triangles. This would help me find how many pythagorean triples were created.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by