Question 3 Write a function function named:posnum that prompts the user to enter
ID: 3028556 • Letter: Q
Question
Question 3
Write a function function named:posnum that prompts the user to enter a positive num-
ber and loops to error-check until user enters positive number. It returns the positive
number entered by the user and print it out on the screen. It calls a subfunction in the
loop to print an error message. The subfunction has a persistent variable to count the
number of times an error has occurred. [10]
Here is the sample output of calling the function: posnum
>> enteredvalue = posnum
Enter a positive number: -5
Error # 1 : Follow instructions!
Does -5.00 look like a positive number to you?
Enter a positive number: -33
Error # 2 . Follow instructions!
Does -33.00 look like a positive number to you?
Enter a positive number: 6
enteredvalue =
6
Explanation / Answer
clc;
clear all
close all;
k=0;
x=-2;
j=0;
while(x<=0)
x =posnum;
if (x<0)
j=subfunction(k);
end
k=k+1;
end
display('You enter =');
x
display('Number of times you enter wrong input =');
j
function n=posnum
n=input('Enter the valid positive number=');
function m = subfunction (k)
m=k+1;
display ('Please follow the instruction');
Enter the valid positive number=-7
Please follow the instruction
Enter the valid positive number=-6
Please follow the instruction
Enter the valid positive number=-9
Please follow the instruction
Enter the valid positive number=-3
Please follow the instruction
Enter the valid positive number=7
You enter =
x =
7
Number of times you enter wrong input =
j =
4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.