Switch Case Construction Code help

(Ajay deleted the question, so I (MF) am restoring it.)
I am trying to create a function that takes a cell array containing strings as an input. The string can be triangle, square, pentagon, hexagon, heptagon, octagon, nonagon, or decagon. The output is the sum of the interior angles of the requested shape. If the input is not valid, the code should return a 0. The input should be able to handle cell arrays containing multiple strings and output the corresponding angles as a vector.
I thought I got it, but then I found out I am required to use "switch case" construction, which I have no idea how to do. I tried solving it twice, and both times i got errors. Here are the 2 codes I worked out: What am I doing wrong? I tried reading on switch case construction, and I don't get what's wrong with either code?!
switch InteriorAngle(input)
case 'triangle'
disp ('180')
case 'square'
disp ('360');
case 'pentagon'
disp ('540');
case 'hexagon'
disp ('720');
case 'heptagon'
disp ('900');
case 'octagon'
disp ('1080');
case 'nonagon'
disp ('1260');
case 'decagon'
disp ('1440');
otherwise
disp ('0');
end
CODE 2
function output = InteriorAngle(input)
n=input('s')
switch InteriorAngle
case ('triangle')
n=3;
sum=(n-3).*(180)
case ('square')
n=4;
dispsum=(n-3).*(180)
case('pentagon')
n=5;
sum=(n-3).*(180)
case ('hexagon')
n=6;
sum=(n-3).*(180)
otherwise
disp ('0')
end

4 commentaires

Azzi Abdelmalek
Azzi Abdelmalek le 8 Oct 2012
what does that mean?
Walter Roberson
Walter Roberson le 8 Oct 2012
Looks like an attempt to edit the meaningful part of a question out of existence :(
Matt Fig
Matt Fig le 8 Oct 2012
Ajay is particularly bad about this. I personally will be restoring what I can of his old questions and ignoring further questions from Ajay....
Matt Fig
Matt Fig le 9 Oct 2012
Saved from google cache:
I am trying to create a function that takes a cell array containing strings as an input. The string can be triangle, square, pentagon, hexagon, heptagon, octagon, nonagon, or decagon. The output is the sum of the interior angles of the requested shape. If the input is not valid, the code should return a 0. The input should be able to handle cell arrays containing multiple strings and output the corresponding angles as a vector.
I thought I got it, but then I found out I am required to use "switch case" construction, which I have no idea how to do. I tried solving it twice, and both times i got errors. Here are the 2 codes I worked out: What am I doing wrong? I tried reading on switch case construction, and I don't get what's wrong with either code?!
switch InteriorAngle(input) case 'triangle' disp ('180') case 'square' disp ('360'); case 'pentagon' disp ('540'); case 'hexagon' disp ('720'); case 'heptagon' disp ('900'); case 'octagon' disp ('1080'); case 'nonagon' disp ('1260'); case 'decagon' disp ('1440'); otherwise disp ('0'); end
CODE 2
function output = InteriorAngle(input) n=input('s') switch InteriorAngle case ('triangle') n=3; sum=(n-3).*(180) case ('square') n=4; dispsum=(n-3).*(180) case('pentagon') n=5; sum=(n-3).*(180) case ('hexagon') n=6; sum=(n-3).*(180) otherwise disp ('0') end

Connectez-vous pour commenter.

 Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 8 Oct 2012
Modifié(e) : Azzi Abdelmalek le 8 Oct 2012

0 votes

switch input('InteriorAngle')
case 'triangle'
disp ('180')
case 'square'
disp ('360');
case 'pentagon'
disp ('540');
case 'hexagon'
disp ('720');
case 'heptagon'
disp ('900');
case 'octagon'
disp ('1080');
case 'nonagon'
disp ('1260');
case 'decagon'
disp ('1440');
otherwise
disp ('0');
end

5 commentaires

Ajay
Ajay le 8 Oct 2012
Modifié(e) : Ajay le 8 Oct 2012
i'm stil getting an error? when i run
in = cell(1,3);
in{1,1} = 'triangle';
in{1,2} = 'hexagon';
in{1,3} = 'dodecagon';
out = InteriorAngle(in)
Julien
Julien le 8 Oct 2012
Modifié(e) : Julien le 8 Oct 2012
because your function InteriorAngle doesn't exist . You have to create it first. Put this code in a m-file, save it with the same name of the function.
function InteriorAngle(in)
for i=1:length(in)
switch char(in(i))
case 'triangle'
disp ('180')
case 'square'
disp ('360');
case 'pentagon'
disp ('540');
case 'hexagon'
disp ('720');
case 'heptagon'
disp ('900');
case 'octagon'
disp ('1080');
case 'nonagon'
disp ('1260');
case 'decagon'
disp ('1440');
otherwise
disp ('0');
end
end
and after that you can do this:
in = cell(1,3);
in{1,1} = 'triangle';
in{1,2} = 'hexagon';
in{1,3} = 'dodecagon';
InteriorAngle(in)
as the function returns no outputs (just dips function used) you don't have to specify an output variable.
You do not show any "in" in your original code.
input() as a request to the user to supply input at the command line. If the data is already in a cell array, the switch() should be on the contents of one cell, such as
switch in{K}
Azzi Abdelmalek
Azzi Abdelmalek le 8 Oct 2012
Modifié(e) : Azzi Abdelmalek le 8 Oct 2012
function output = InteriorAngle(s)
switch s
case ('triangle')
n=3;
sum=(n-3).*(180)
case ('square')
n=4;
dispsum=(n-3).*(180)
case('pentagon')
n=5;
sum=(n-3).*(180)
case ('hexagon')
n=6;
sum=(n-3).*(180)
otherwise
disp ('0')
end
%
InteriorAngle('triangle')
Ajay
Ajay le 8 Oct 2012
OH i see i see. thanks :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Identification dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by