Solving Conditional Operators Homework with Switch-Case

AI Thread Summary
The discussion focuses on using a switch-case structure in MATLAB to determine the number of sides for various shapes based on a string variable named Shape. Participants address challenges in implementing error messages for unrecognized shapes, specifically distinguishing between shapes that end in "gon" and those that do not. Suggestions include using string manipulation to check the last three characters of Shape and displaying appropriate error messages. There is confusion regarding syntax and language specifics, as some code examples provided were not in MATLAB. Clarifications emphasize the need for accurate syntax and the use of regular expressions for better error handling.
gfd43tg
Gold Member
Messages
947
Reaction score
48

Homework Statement


Using the switch-case construction, write code that take a variable named Shape containing a
string and assigns to the variable numSides the number of sides of the shape named in the variable
Shape. Your code should be able to return the number of sides for a triangle, square, pentagon,
hexagon, heptagon, or octagon. If the variable Shape contains a string that is not listed above,
assign NaN to the variable numSides and display one of the following two warnings:

If the string stored in Shape ends in `gon', you should display the following warning: "I
don't know how many sides a <string stored in Shape> has.".

If the string stored in Shape does not end in `gon', your code should display the following
warning: "I don't know what a <string stored in Shape> is.".


Homework Equations





The Attempt at a Solution


I am stuck about how to enter in the error messages, here is what I have working so far.
Code:
switch Shape
    case 'triangle'
        numSides = 3;
    case 'square'
        numSides = 4;
    case 'pentagon'
        numSides = 5;
    case 'hexagon'
        numSides = 6;
    case 'heptagon'
        numSides = 7;
    case 'octagon'
        numSides = 8;
    otherwise
        numSides = NaN;
end

This is how I have been trying to implement an error message
Code:
if numSides = NaN
error = 1;
if error
disp( 'I don''t know how many sides a <string stored in Shape> has.')
Of course this is wrong. There are two problems here for me. First, I don't know how to make an error message for two separate errors. Secondly, I don't know how to insert the string into the error message.

Thank you.
 
Physics news on Phys.org
You're close

try this pseudo code construct:
Code:
// DO we have an error?
if numSides == NaN:

    // WHICH error is it?
    if Shape.endswith("gon"):
        emsg="We don't know how many sides the "+Shape+" has.";

    else:
        emsg="We know know what "+Shape+" is.";
 
    // PRINT the error msg
    disp(emsg);
 
I'm not sure which language you're using, but I will assume the syntax is OK. But be suspect of the syntax that I suggest. Javascript? Visual Basic Script maybe?

You don't need "error = 1".

The additional code should be placed immediately after the "numSides = Nan;" statement.
First you need to isolate the last three characters from "Shape". Perhaps Shape.Mid(Shape.Length()-3)? The compare that to "gon". You can use an if/then/else construct so...
Code:
      ...
      numSides = Nan;
      if Shape.Mid(Shape.Length()-3) = "gon"
        disp( 'I don''t know how many sides a ' & Shape & ' has.')
      else
        disp( 'I don''t know what a ' & Shape & ' is.')
      end
 
I am using MATLAB
 
Maylis said:
I am using MATLAB
Ahh. so you have regular expressions available to you.
You should search for "\w*gon".
length( regexp(Shape,"\w*gon") ) > 0
 
Jedish, I don't know if the code you wrote is for MATLAB, but I get an operator error when I put it in the command window. I will put in thread titles that I am using MATLAB from now on to avoid confusion.
 
Maylis said:
Jedish, I don't know if the code you wrote is for MATLAB, but I get an operator error when I put it in the command window. I will put in thread titles that I am using MATLAB from now on to avoid confusion.

My code was python-based pseudo code, I figured you would know enough to convert to your language especially since you didn't mention what it was.
 
Not yet, only been programming for about a week!
 

Similar threads

Replies
2
Views
3K
Replies
14
Views
5K
Replies
4
Views
3K
Replies
7
Views
2K
Replies
3
Views
2K
Replies
6
Views
3K
Back
Top