Answered step by step
Verified Expert Solution
Link Copied!

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 4
% generate two vectors consisting digits from 0 to 9
vec1=randi(9,1,20); % row vector of length 20 consisting of digits between 0 to 9
vec2=randi(9,1,20);
add=[]; % initialize to an empty vector
first=0; % initialized to zero( this is the digit will be obtained after addition of two
% integers eg: 5+7=12,then first=1)
% loop starts in revers i.e 20,19,18,..,1
for i=20:-1:1
% add the two numbers of the ith location from vec1 and vec2
a=vec1(i)+vec2(i)+first;
% now convert the number a into a string array
a_str=num2str(a);
% check if i==1,suppose the first digit in vec1 and vec2 are 5 and 6
% then 5+7=12,if you don't use this conditional then
% only 2 wll be stored into the vector add
if i==1
add=[a add];
else
% check if a_str is of length 2
if length(a_str)==2
% then it's a two digit number ,so get the first digit from left
first=a_str(1);
% convert back first into a number
first=str2double(first);
second=a_str(2);
second=str2double(second);
add=[second add]; % store into the vector add
else
add=[a add];
first=0; % change first back to 0
end
end
end
% one more thing to do is that check if the first number in the vector add
% is of two digit
if length(num2str(add(1)))==2
str=num2str(add(1));
add=[str2double(str(1)) str2double(str(2)) add(2:end)]; % this is done
% to have space
% between 1 and 2
% as if add(1)=12
% display to the command window
% space taken just to have a good formating output
disp(['',num2str(vec1)])%[] use to concatenate strings
disp(['',num2str(vec2)])
disp('+')
disp("-------------------------------------------------------------")
disp(num2str(add))
else
disp(num2str(vec1))
disp(num2str(vec2))
disp('+')
disp("----------------------------------------------------------")
disp(num2str(add))
end
%% Question 5
clear
fprintf('
welcome to guess my number game')
disp('')
a = input('
Please tell me you nice Name : ','s');
fprintf('
\t Welcome to the game %s',a)
disp('');
m1=1;
while m1==1
disp('');
disp('Choose the level of playing:')
disp('1)1-10')
disp('2)1-100')
disp('')
o = input('enter the level you want to play: ');
disp('')
switch o
case 1
n =10;
case 2
n =100;
end
b = randperm(n);
index = randperm(length(b));
r = b(index(1));
m2=1;
count =0;
while m2==1
guess='r';
while guess =='r'
guess = input('Input your guess: ','s');
end
guess = str2double(guess);
if (guess == r)
disp('')
fprintf('Correct! My secret number was: %i
',r)
m2=2;
elseif (guess > n)
disp('')
fprintf('Too high, please less than %i
',n)
disp('')
elseif (g <1)
disp('')
disp('Too low, please more than %i
',n)
disp('')
elseif (guess > r)
disp('Lower')
else ,(guess < r);
disp('Higher')
end
count = count+1;
end
disp('')
fprintf('Guesses needed to find my secret number: %i
',c)
disp('')
if (count ==1)
disp('marvallous')
elseif (count >=2 && count <0.5*n)
disp('excellent')
elseif (count ==0.5*n)
disp('Avg')
elseif (count >0.5*n && count <=0.8*n)
disp('its very poor')
else ,(count >0.8*n);
disp('better luck next time !')
end
disp('')
p = input('will u wish to play again? (yes/no): ','s');
if p =='no'
m1=2;
end
disp('')
fprintf('Thank you for playing this game %s
',a)
end

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What question does this logarithm answer? log_(2)8

Answered: 1 week ago

Question

Evaluate the importance of the employee handbook.

Answered: 1 week ago

Question

Discuss the steps in the progressive discipline approach.

Answered: 1 week ago