Solving Conditional Operators Homework with Switch-Case

Click For Summary
SUMMARY

The discussion focuses on implementing a switch-case structure in MATLAB to determine the number of sides of various shapes based on a string variable named Shape. The solution involves using a switch-case statement for recognized shapes and handling errors for unrecognized shapes with appropriate messages. Participants clarified how to check if the string ends with 'gon' to provide specific error messages, and emphasized the importance of using MATLAB syntax correctly to avoid operator errors.

PREREQUISITES
  • Understanding of MATLAB programming syntax
  • Familiarity with switch-case statements in MATLAB
  • Knowledge of string manipulation functions in MATLAB
  • Basic error handling techniques in programming
NEXT STEPS
  • Learn about MATLAB string functions, specifically endsWith and length
  • Explore regular expressions in MATLAB for advanced string matching
  • Study error handling and debugging techniques in MATLAB
  • Practice implementing switch-case statements with complex conditions in MATLAB
USEFUL FOR

Students learning MATLAB, beginner programmers seeking to understand control structures, and anyone interested in error handling in programming.

gfd43tg
Gold Member
Messages
949
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
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 14 ·
Replies
14
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K