1. Write a MATLAB function called sqrtList that takes two positive integers n1 a
ID: 3778297 • Letter: 1
Question
1. Write a MATLAB function called sqrtList that takes two positive integers n1 and n2 as an input. The functions must a) not accept nonsensical input b) include help comments and other descriptive comments throughout your code c) return a 2-column array containing the integers from n1 to n2 in the first column and the square roots of the integers from n1 to n2 in the second column, and d) display the statement The square root of x1 is x2 for each integer from n1 to n2, and each on a different line. The square roots should be displayed using 5 decimal places, and the ones places in both numeric fields should be aligned for all statements. For example: The square root of 98 is 9.89949 The square root of 99 is 9.94987 The square root of 100 is 10.00000 The square root of 101 is 10.04988
Explanation / Answer
function f = sqrtList(n1,n2)
if(isinteger(n1)&&isinteger(n2)&&n1<=n2)
arr=[]
j=1
for i=n1:n2
arr(j,1)=i
m=sqrt(i)
arr(j,2)=m
fprintf(" square root of %d is %4.5f",i,m)
j=j+1
end
f=arr
else
f=[]
fprintf("invalid input")
end
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.