Write function called huge_add that adds together two positive integers of any l
ID: 3847154 • Letter: W
Question
Write function called huge_add that adds together two positive integers of any length specified as char
vectors using decimal notation. The single output argument is the result and it is a char vector as well. The
inputs and output must contain digits only; no commas, spaces or any other characters are allowed. If any of
these assumptions are violated by the input, the function returns the number -1. Hint: you cannot use any of
the built-in numerical data types of MATLAB to do the calculation since the function needs to work for
numbers that are larger than what can be represented as a number in MATLAB.
Explanation / Answer
function n=chrToNum(vec)
n=0
j=0
n=str2num(vec)
end
function sum=huge_add(num1,num2)
n1=chrToNum(num1)
n2=chrToNum(num2)
sum=n1+n2
sum=num2str(sum)
fprintf(" %d",sum)
end
num1='123456789'
num2='987654321'
huge_add(num1,num2)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.