How can I correct this error?
1 次查看(过去 30 天)
显示 更早的评论
Write function called huge_add that adds together two positive integers of any length specified as strings using decimal notation. The single output argument is the result and it is a string 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.
Here is my code:
function[c]=huge_add(a1,b1)
aaa=length(a1);
bbb=length(b1);
c=0;
if isnumeric(a1) ||isnumeric(b1)
c=-1
else if ~isnumeric(a1) ||~isnumeric(b1)
for i=1:aaa
if a1(i)~='1'&& a1(i)~='2'&&a1(i)~='3'&&a1(i)~='4'&&a1(i)~='5'&&a1(i)~='6'&&a1(i)~='7'&&a1(i)~='8'&&a1(i)~='9'&&a1(i)~='0'
c=-1
end
end
for i=1:bbb
if b1(i)~='1'&&b1(i)~='2'&&b1(i)~='3'&&b1(i)~='4'&&b1(i)~='5'&&b1(i)~='6'&&b1(i)~='7'&&b1(i)~='8'&&b1(i)~='9'&&b1(i)~='0'
c=-1
end
end
end
a=a1;
b=b1;
aa=length(a);
bb=length(b);
as=(str2num(a));
bs=(str2num(b));
af=fliplr(a);
bf=fliplr(b);
if c~=-1
if aa<bb
for ii=1:aa
x(ii)=af(ii);
y(ii)=bf(ii);
z(ii)=str2num(x(ii))+str2num(y(ii));
zz{ii}=num2str(z(ii));
end
cl=fliplr(z);
rem=(b(1):b(bb-aa));
ca=[rem,cl(1):cl(end)];
else if aa>bb
for ii=1:bb
x(ii)=af(ii);
y(ii)=bf(ii);
z(ii)=str2num(x(ii))+str2num(y(ii));
zz(ii)=num2str(z(ii))
end
cl=fliplr(z);
rem=(a(1):a(aa-bb));
ca=[rem,cl(1):cl(end)];
else
for ii=1:aa
x(ii)=af(ii);
y(ii)=bf(ii);
z(ii)=str2num(x(ii))+str2num(y(ii));
zz(ii)=num2str(z(ii));
end
cl=fliplr(z);
rem=[];
ca=[rem,cl(1):cl(end)];
end
end
d=str2num([rem,fliplr(zz)])
%d=([rem,fliplr(zz)])
c=num2str(d)
end
end
end
when the arguments are long numbers such as:'387363715250650494', '2959321308593705578801966108511980929658625'
I get this error:
In an assignment A(:) = B, the number of elements in A and B must be the
same.
Error in huge_add (line 33)
zz(ii)=num2str(z(ii));
How can I fix this?
0 个评论
回答(2 个)
Srishti Saha
2018-5-13
This works perfectly:
%problem huge add
function summa = huge_add(a,b)
if ~ischar(a) || ~ischar(b) || sum(isstrprop(a,'digit')) ~= length(a) || ...
sum(isstrprop(b,'digit')) ~= length(b)
summa = -1;
return;
end
lng = max([length(a) length(b)]);
a = [a(end:-1:1) '0'+zeros(1,lng-length(a))]; % flip and pad with zeros if necessary
b = [b(end:-1:1) '0'+zeros(1,lng-length(b))]; % flip and pad with zeros if necessary
carry = 0;
for ii = 1:lng
c = carry + str2num(a(ii)) + str2num(b(ii)) % add the digits plus the caryy
carry = c > 9; % is there carry? (0 or 1)
summa(ii) = num2str(mod(c,10)); % put the character in the result
end
if carry
summa(end+1) = '1'; % need a leading 1 if carry
end
summa = summa(end:-1:1); % flip it back
end
2 个评论
Jan
2018-5-13
@Srishti Saha: When a student delivers a solution copied from a forum, this can be treated as trial to cheat. Therefore it is much better to assist the students in finding a solution by their own.
As soon as this forum degrades to a homework cheater community, it will loose its power. Please consider this. Thank you.
Walter Roberson
2016-8-9
A few lines up from there you have
zz{ii}=num2str(z(ii));
but in this line you have
zz(ii)=num2str(z(ii));
Is zz a cell array or is it a numeric array?
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!