Solving Conditional Operators Homework with Switch-Case

In summary, the code should take a variable named Shape and assign to the variable numSides the number of sides of the shape named in the variable. 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: "I don't know how many sides a <string stored in Shape> has." or "I don't know what a <string stored in Shape> is."
  • #1
gfd43tg
Gold Member
950
50

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
  • #2
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);
 
  • #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:
      ...
      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
 
  • #4
I am using MATLAB
 
  • #5
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
 
  • #6
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.
 
  • #7
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.
 
  • #8
Not yet, only been programming for about a week!
 

FAQ: Solving Conditional Operators Homework with Switch-Case

1. What is the purpose of using switch-case in solving conditional operators homework?

The switch-case statement is used to evaluate a variable or an expression against a list of possible values and execute different blocks of code based on the matching value. It is commonly used to simplify complex if-else statements and make code more readable and organized.

2. How do I write a switch-case statement in my code?

The syntax for a switch-case statement is as follows:

switch (expression) {
case value1:
// code to be executed if expression matches value1
break;
case value2:
// code to be executed if expression matches value2
break;
...
default:
// code to be executed if expression doesn't match any case
}

Note that the break statement is used to end the switch-case statement and prevent the code from executing the code in the following cases.

3. Can I have multiple cases for the same block of code in a switch-case statement?

Yes, you can have multiple cases with the same block of code by omitting the break statement. For example:

switch (expression) {
case value1:
case value2:
// code to be executed if expression matches value1 or value2
break;
...
}

This can be useful when you want the same code to be executed for multiple values.

4. How do I handle default cases in a switch-case statement?

The default case is used to execute a block of code when the expression doesn't match any of the cases. It is not required, but it is good practice to include a default case to handle unexpected input. The default case should always be the last case in a switch-case statement.

5. Can I use conditional operators in a switch-case statement?

Yes, you can use conditional operators in the expression of a switch-case statement. For example:

switch (expression) {
case (value1 === value2):
// code to be executed if value1 is equal to value2
break;
case (value1 > value2):
// code to be executed if value1 is greater than value2
break;
...
}

Similar threads

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