Huge Addition
Matlab. This was an interesting assignment in the Matlab course I took before. Its inputs and output could be very large, in fact, larger than representable number in Matlab. Solution can show its output with full digits even if it is more than 16 digits.
For example;
input 1 = ‘392819655968612702353430868739406976733152178677243’
input 2 = ‘800648666501228076207488540123’
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.
Solution: After checking if the inputs are char or not, char inputs were converted to double and passed to the calculation stage. I wrote an function inside called “calculator” for addition creating a char vector by taking care of any carries. In the final stage, this function is called after the input sizes are compared and zeros are added to close the digit gap between the inputs.
answer = ‘392819655968612702354231517405908204809359667217366’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
function output = test_huge_add tic char1 = '392819655968612702353430868739406976733152178677243'; %input 1 char2 = '800648666501228076207488540123'; %input 2 %Checking Stage for the Inputs if ~ischar(char1) || ~ischar(char2) output = -1; return end number1 = str2num(char1); number2 = str2num(char2); if length(number1) ~=1 || length(number2) ~=1 output = -1; return end if number1 < 0 || fix(number1) ~= number1 output = -1; return end if number2 < 0 || fix(number2) ~= number2 output = -1; return end %Calculating Stage function calculator() new_char_vector = ''; carry = 0; for i = length(char1):-1:1 x = str2num(char1(i)) + str2num(char2(i)) + carry; carry = 0; if x < 10 x = num2str(x); new_char_vector = [x new_char_vector]; x = str2num(x); end if x >= 10 remainder = mod(x,10); remainder = num2str(remainder); new_char_vector = [remainder new_char_vector]; remainder = str2num(remainder); carry = carry + 1; end end if carry == 1 %if there is carry at last addition new_char_vector = ['1' new_char_vector]; end output = new_char_vector; end %Final Stage - checking which input is bigger and finalising the array if length(char1) == length(char2) %if both digit numbers are equal calculator(); %calling the function end if length(char1) > length(char2) %if the first number digit is bigger d = length(char1) - length(char2); %difference between digits for j = 1:d char2 = ['0' char2]; %adding zeros to the new char vector end calculator(); end if length(char1) < length(char2) %if the second number digit is bigger d = length(char2) - length(char1); %difference between digits for j = 1:d char1 = ['0' char1]; %adding zeros to the new char vector end calculator(); end toc end |