Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please check all work in matlab and correct all syntax error or any other error that prevents it from running. % % Question 4 %
Please check all work in matlab and correct all syntax error or any other error that prevents it from running.
Question
generate two vectors consisting digits from to
vecrandi; row vector of length consisting of digits between to
vecrandi;
add; initialize to an empty vector
first; initialized to zero this is the digit will be obtained after addition of two
integers eg: then first
loop starts in revers ie
for i::
add the two numbers of the ith location from vec and vec
avecivecifirst;
now convert the number a into a string array
astrnumstra;
check if isuppose the first digit in vec and vec are and
then if you don't use this conditional then
only wll be stored into the vector add
if i
adda add;
else
check if astr is of length
if lengthastr
then it's a two digit number so get the first digit from left
firstastr;
convert back first into a number
firststrdoublefirst;
secondastr;
secondstrdoublesecond;
addsecond add; store into the vector add
else
adda add;
first; change first back to
end
end
end
one more thing to do is that check if the first number in the vector add
is of two digit
if lengthnumstradd
strnumstradd;
addstrdoublestr strdoublestr add:end; this is done
to have space
between and
as if add
display to the command window
space taken just to have a good formating output
dispnumstrvec use to concatenate strings
dispnumstrvec
disp
disp
dispnumstradd
else
dispnumstrvec
dispnumstrvec
disp
disp
dispnumstradd
end
Question
clear
fprintf
welcome to guess my number game'
disp
a input
Please tell me you nice Name : s;
fprintf
t Welcome to the game sa
disp;
m;
while m
disp;
dispChoose the level of playing:
disp
disp
disp
o inputenter the level you want to play: ;
disp
switch o
case
n ;
case
n ;
end
b randpermn;
index randpermlengthb;
r bindex;
m;
count ;
while m
guessr;
while guess r
guess inputInput your guess: s;
end
guess strdoubleguess;
if guess r
disp
fprintfCorrect My secret number was: i
r
m;
elseif guess n
disp
fprintfToo high, please less than i
n
disp
elseif g
disp
dispToo low, please more than i
n
disp
elseif guess r
dispLower
else guess r;
dispHigher
end
count count;
end
disp
fprintfGuesses needed to find my secret number: i
c
disp
if count
dispmarvallous
elseif count && count n
dispexcellent
elseif count n
dispAvg
elseif count n && count n
dispits very poor'
else count n;
dispbetter luck next time
end
disp
p inputwill u wish to play again? yesno: s;
if p no
m;
end
disp
fprintfThank you for playing this game s
a
end
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started