How to write this in Matlab program? (a) Prompt the user to enter a scalar integ
ID: 2081817 • Letter: H
Question
How to write this in Matlab program? (a) Prompt the user to enter a scalar integer end store the integer in a variable named Integer input. Assume the user will always enter o numerical scalar that does not contain any leading zeros. Utilize a while-end statement to continuously prompt the user until the user actually enters an integer. You may use the built-in round() function to help you determine whether or not the user entered an integer scalar. (b) Prompt the user to enter a scalar integer representing a decimal digit (i.e., a positive integer number in the range 0 to 9, inclusive) and store the scalar integer m a variable named digitToFind. Assume the user will always enter a numerical scalar Utilize a while-end statement to continuously prompt the user until the user actually enters a decimal digit m the range 0 to 9. Inclusive. (c) Utilize a whi1e-end statement m addition to other statements to perform the following: Compute the number of individual digits contained within the integer stored in integerInput. Store the result in a variable named numofDigits. Compute the number of times (i.e., the frequency) at which the digitToFind appears within the integer entered by the user Store the result in a variable called digitToFindFreq. (d) Using multiple instances of the built-in fprintf() function, display numofDigits and digitToFindFreq. Format each variable as an integer. Thoroughly test your program for integers having less than or equal to approximately 15 digits. Use the sample output shown above to guide you m the testing process.Explanation / Answer
Copy the following code to a new script in MATLAB.
close all; clear all; clc;
% Part (a)
integerInput = input('Enter an interger : ');
while(integerInput-round(integerInput)~=0)
fprintf('Invalid input. Try again! ');
integerInput = input('Enter an interger: ');
end
% Part (b)
digitToFind = input('Enter a digit number to find (0 to 9): ');
n=0:9;
% n==digitToFind returns a logical array in which exactly one element is 1
% if digitToFind lies in 0 to 9. check will be equal to 1 if digitToFind
% lies in 0 to 9 else check will be equal to 0
check = sum(n==digitToFind);
while(~check)
fprintf('Invalid input. Try again! ');
digitToFind = input('Enter a digit number to find (0 to 9): ');
check = sum(n==digitToFind);
end
% Part (c)
x = integerInput;
numbers = 0;
while(x~=0)
p = fix(x/10); % returns the quotient rounded nearer to zero when x is divided by 10
h = (x - (10*p))*(-1*(x<0)+1*(x>=0)); % retuns the LSD
numbers = [h numbers]; % storing digits in integerInput. Contains an additional 0.
x = p;
end
if (integerInput==0)
digits = 0; % condition if integerInput is 0
else
digits = numbers(1:end-1); % storing digits in integerInput with 0 removed.
end
N = length(digits);
y = find(digits==digitToFind);
digitToFindFreq = length(y);
fprintf('Interger %d contains %d digits. ',integerInput,N);
fprintf('Occurence frequency of digit %d is %d. ',digitToFind,digitToFindFreq);
You'll get the ouput in Command Window.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.