2 views (last 30 days)

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

disp('one');

case 2

disp('two');

case 3

disp('three');

case 4

disp('four');

case 5

disp('five');

case 6

disp('six');

case 7

disp('seven');

case 8

disp('eight');

case 9

disp('nine');

case 10

disp('ten');

otherwise

disp('Error');

end

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: ');

numbersAsStrings{number}

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.

Walter Roberson
on 18 Feb 2019

the case statements are valid but they require that you use

switch true

Stephen Cobeldick
on 18 Feb 2019

"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

end

But switch is still a very inefficient way to solve this task:

Sign in to comment.

John D'Errico
on 18 Feb 2019

Edited: 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.

HOWEVER...

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'};

numnames{num}

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:

vpi2english(vpi('1252343463465723'))

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:

1,252,343,463,465,723

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,

vpi2english(vpi('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.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.