1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Conditional Operators

  1. Jun 19, 2014 #1

    Maylis

    User Avatar
    Gold Member

    1. The problem statement, all variables and given/known data
    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.".


    2. Relevant equations



    3. 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 (Text):
    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 (Text):
    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.
     
  2. jcsd
  3. Jun 19, 2014 #2

    jedishrfu

    Staff: Mentor

    You're close

    try this pseudo code construct:
    Code (Text):


    // 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);

     
     
  4. Jun 19, 2014 #3
    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 (Text):

          ...
          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
     
     
  5. Jun 19, 2014 #4

    Maylis

    User Avatar
    Gold Member

    I am using MATLAB
     
  6. Jun 19, 2014 #5
    Ahh. so you have regular expressions available to you.
    You should search for "\w*gon".
    length( regexp(Shape,"\w*gon") ) > 0
     
  7. Jun 19, 2014 #6

    Maylis

    User Avatar
    Gold Member

    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.
     
  8. Jun 19, 2014 #7

    jedishrfu

    Staff: Mentor

    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.
     
  9. Jun 19, 2014 #8

    Maylis

    User Avatar
    Gold Member

    Not yet, only been programming for about a week!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted