MATLAB Answers


Switch to write programs

Asked by Thomas Lu on 17 Feb 2019
Latest activity Commented on by Thomas Lu on 18 Feb 2019
Use switch to write a program to prompt the user to enter an integer between 1 to 10 then display the English word corresponding to it. You can assume that the user inputs integers between 1 to 10.
How can I modify my program hence it works for bigger numbers? (e.g. 1-100)
I now do my 1-10 in this way. Thanks a lot!!
>> number = input('Input an integer: ');
Input an integer: 3
>> switch number
case 1
case 2
case 3
case 4
case 5
case 6
case 7
case 8
case 9
case 10


Sign in to comment.




2 Answers

Answer by Geoff Hayes
on 18 Feb 2019

Yu - rather than creating using a switch control with cases for each of the 100 integers, you could just create a cell array of strings for each of the numbers:
numbersAsStrings = {'one' , 'two', 'three', etc.};
And then use the input integer as an index into this array to return the correct string
number = input('Input an integer: ');
Though you may want to verify that number is a valid integer between 1 and 100 before trying to treat it as an index into your array.


the case statements are valid but they require that you use
switch true
how to use switch true? There's not much info on the documentation... where should I add the true value such that MATLAB understands the cases? It keeps turning to the 'otherwise' case and shows 'Error'...
"The case 0 < x && x < 10 is invalid ... "
They are valid MATLAB syntax.
"how to use switch true?"
switch true
case 0<x && x<10
... etc
But switch is still a very inefficient way to solve this task:

Sign in to comment.

Answer by John D'Errico
on 18 Feb 2019
Edited by John D'Errico
on 18 Feb 2019

You could write a switch statement for this. The problem is, since each case does a different thing, you effectively need 100 different cases. The point is, while your homework assignment tells you to solve it for the cases 1:10, and you did that, it is a terrbily inefficient, even poor way to write such a code. You simply don't want to write that code in general. Yes, homework is one thing. It taught you to write a switch/case construct to solve the problem you were explicitly requested to solve.
In any good programming style, this is NOT how you want to be writing code for any large problem. Even in the situation for 1:10, it is a minor kludge to write the code using a switch. Instead, you might use an array of names for the numbers. Thus you could have a cell array of 100 names.
numnames = {'one', 'two', 'three', 'four', .. , 'fifty two', .. , 'one hundred'};
That would work simply enough, although it would force you to create that cell array in advance.
But suppose your goal was to write out the name for any integer below one billion? One trillion? Why be simplistic and stop at 100? Now you would need to use the rules for naming numbers. In fact, I did this in my vpi2english tool, where it can do things like:
ans =
'one quadrillion, two hundred fifty two trillion, three hundred forty three billion, four hundred sixty three million, four hundred sixty five thousand, seven hundred twenty three '
Such a code breaks down the number into segments, three digits at a time. So it reduces the problem to something like this:
Decide how many blocks there are. Then process each block of three. Within a block of three digits, say 252, how many hundreds are there? Now, you would recognize that the numbers 1 - 99 all have similar names, based on which block of 10 you fall into. So 23 is created by recognizing the numbers 20-29 start with the prefix 'twenty', then you append 'three'. Now all you need to do is store two cell arrays, one for the possible one digit numbers, another for the two digit prefixes. A third cell array would allow you to name each block of three digits, thus {'thousand','million','billion',...}. So you might have
ones = {'one' 'two','three','four','five','six','seven','eight','nine'};
tens = {'', 'twenty', 'thirty', 'fourty', ... }
There would be special cases of course. So no tens prefix is needed for the numbers less than ten. And you might notice that the numbers 10:19 have their own names. So they probably need their own cell array for those names. And in the number 1000003,
ans =
'one million, three '
You will need to recognize you don't say zero thousand, etc.
The point is, you would use some very simple rules to break down any number, of any size. But one thing you never need to do is to use a massive switch statement. Instead, it will always be more efficient to break the number down, then use simple rules to name that number. The entire code for vpi2english was 171 lines of MATLAB, which includes a fair amount of comments and white space (blank lines). There are roughly 100 lines of actual code.
The argument behind all this is you don't need to write such huge switch statements, nor do you really want to. Instead, look for better, more efficient ways to write your code. As your programming skills improve, you will naturally look for such solutions. As I show above, the solution is to look for patterns in the names of numbers, then use them to your advantage.

  1 Comment

Eventually I gave up switch and came up with this and it works! Thanks everyone!!
% Q4 Challenge
numbersAsStrings = {'one','two','three','four','five','six','seven','eight','nine'};
numbersTens = {'eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen'};
numbersHundreds = {'ten','twenty','thirty','fourty','fifty','sixty','seventy','eighty','ninety','One Hundred'};
number = input('Input an integer: ');
x = number;
if (0 < x && x < 10)
elseif (10 < x && x < 20)
elseif (20 < x && x < 30)
disp(['twenty-' numbersAsStrings{mod(x,10)}]);
elseif (30 < x && x < 40)
disp(['thirty-' numbersAsStrings{mod(x,10)}]);
elseif (40 < x && x < 50)
disp(['forty-' numbersAsStrings{mod(x,10)}]);
elseif (50 < x && x < 60)
disp(['fifty-' numbersAsStrings{mod(x,10)}]);
elseif (60 < x && x < 70)
disp(['sixty-' numbersAsStrings{mod(x,10)}]);
elseif (70 < x && x < 80)
disp(['seventy-' numbersAsStrings{mod(x,10)}]);
elseif (80 < x && x < 90)
disp(['eighty-' numbersAsStrings{mod(x,10)}]);
elseif (90 < x && x < 100)
disp(['ninety-' numbersAsStrings{mod(x,10)}]);

Sign in to comment.