switch statements and trig function

11 views (last 30 days)
Marina Christakos
Marina Christakos on 24 Jan 2019
Edited: Kevin Phung on 24 Jan 2019
val = str2double(input('enter number','s'));
chr = input('enter letter','s');
switch chr
case 's'
disp(sin(val))
case 'c'
disp(cos(val));
case 't'
disp(tan(val));
otherwise
disp('Oh no! Try a different character.');
end
I wrote this code to find the sin,cos,or tan of an entered individual number. I now need to modify it so that instead of reading a single character/number, the program continually reads character/number pairs and displays the trigonometry result. If a number outside the range [-100, 100] is entered, the program should not display the trigonometry result and should ask for another character/number pair to be entered. I can't figure out how to make the initial modifications of reading in pairs - any shortcuts I can use?
  2 Comments
Kevin Phung
Kevin Phung on 24 Jan 2019
what do you mean by reading in pairs?
Marina Christakos
Marina Christakos on 24 Jan 2019
Edited: Marina Christakos on 24 Jan 2019
instead of the program asking for a letter and number separately, I as the user am able to write c90 and the output will respond with 0

Sign in to comment.

Answers (2)

Kevin Chng
Kevin Chng on 24 Jan 2019
I think the most simple approach is to add another loop which is
val = str2double(input('enter number','s'));
chr = input('enter letter','s');
if val>=-100 && val<=100
switch chr
case 's'
disp(sin(val))
case 'c'
disp(cos(val));
case 't'
disp(tan(val));
otherwise
disp('Oh no! Try a different character.');
end
else
warning('enter new input, your input is out of range')
end
  2 Comments
Marina Christakos
Marina Christakos on 24 Jan 2019
that still makes me input the number and letter at separate times
Kevin Chng
Kevin Chng on 24 Jan 2019
I might miss understanding your question.
However, it is still input them in separate line, if wants to input in the same line, later, you might need to separate them since one is string/char and the other one is numeric
clc;clear all;
chr = input('enter letter & number :','s');
% Need some logic to separate them and ensure the user key in the right thing
C = {'1','2','3','4','5','6','7','8','9','0'};
for i = 1:10
Index = strfind(chr, C{i});
if isempty(Index)~=1
if exist('thefirstdigit')==0
thefirstdigit=Index;
else
if thefirstdigit>Index
thefirstdigit=Index;
end
end
end
end
%indexing to chr, then separate them into val and chr2
val = str2double(chr(thefirstdigit:end));
chr2 = (chr(1:thefirstdigit-1));
if val>=-100 && val<=100
switch chr2
case 's'
disp(sin(val))
case 'c'
disp(cos(val));
case 't'
disp(tan(val));
otherwise
disp('Oh no! Try a different character.');
end
else
Run the program and enter :s100

Sign in to comment.


Kevin Phung
Kevin Phung on 24 Jan 2019
Edited: Kevin Phung on 24 Jan 2019
you can just run the input once, and evaluate the input as a whole.
word = input('enter letter followed by number','s');
if numel(word)>1
chr = word(1);
val =str2num(word(2:end))
if isempty(val) || val >100 || val < -100 %occurs when 2nd character is a letter and not a number
disp('Try again')
return
end
switch chr
case 's'
disp(sin(val))
case 'c'
disp(cos(val));
case 't'
disp(tan(val));
otherwise
disp('Oh no! Try a different character.');
end
else
disp('Try again')
end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!