Definition
For this question we have to write MATLAB code to detect a scalene triangle by reading in three numerical values representing the lengths of the three sides and printing “it’s a scalene” if the triangle is not an isosceles or equilateral triangle and the sum of any two side’s length is greater than the length of the other side.
Testing
On paper, or in a text file, write three new tests for the above program. As an example, three tests for this program might be.
inputs output
5 5 5 <no output>
2 1 2 <no output>
3 2 1 <no output>
3 4 5 “it’s a scalene”
Coding
If you are sharing a computer, swap a new group member to the terminal. Write a MATLAB script called q6.m that implements the definition above using at least if-statements.
close all,
clear all,
clc,
%{
ValString = input('Enter the sides: ', 's');
Vals = regexp(ValString, '[ ,]', 'split');
sides = str2double(Vals);
%}
sides = [
5,5,5;
2,1,2;
3,2,1;
3,4,5
];
for r=1:length(sides)
Flag=0;
a=sides(r,1); b=sides(r,2); c = sides(r,3);
if(a+b>c && b+c>a && c+a>b),
if ((a==b && a~=c) || (b==c && b~=a) || (c==a && c~=b))
disp(['<',num2str(sides(r,:)),'> no output']);
elseif (a==b && b==c && c==a)
disp(['<',num2str(sides(r,:)),'> no output']);
else
disp(['<',num2str(sides(r,:)),'> Its a Scalene']);
end
else
disp(['<',num2str(sides(r,:)),'> no output']);
end
end
Matlab Output:
<5 5 5> no output
<2 1 2> no output
<3 2 1> no output
<3 4 5> Its a Scalene
>>
Comments
Leave a comment