Question

4. The goal in this problem is to add two 20 digit integers Sounds easy right? Try this: 1 11111111 1111; printf(%30.08 ) Th
how would i code this in MATLAB
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Used vector to store numbers and add them using for loop

l l 1]; l l 2 2 l l l l 2 2 2 2 2 l l 2 2 l l l l 2 2 2 2 >> a =[l l l l l >> b = [2 2 2 2 2 >> sum(a,b) ans = . 3 3 3 3 3 3

>> a =[1 iiiiiiiiiiiiiiiiii 1]; >> b = [9 9 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2]; >> sum(a,b) ans = - 7 1 0 3 3 3 3 3 3 3 3 3

function res = sum (a,b) HN * 4 7 6 5 4 3 2 1 3 2 1 HH ON 1 i=20; res = zeros (1, 20); carry = 0; while i>o sum = carry+a (i)

Code

function res = sum(a,b)
  
i=20;
res = zeros(1,20);
carry = 0;
while i>0
sum = carry+a(i)+b(i);
res(i) = mod(sum,10);
carry = floor(sum/10);
i=i-1;
end
if carry!=0
res = [carry res];
end
end

Add a comment
Know the answer?
Add Answer to:
how would i code this in MATLAB 4. The goal in this problem is to add...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • please there are some specific instructions on the question so i would greatly appreciate if they...

    please there are some specific instructions on the question so i would greatly appreciate if they are followed . thank you very much for your time This lab covers: arrays functions input exception handling Question 1 The purpose of this question is to write a python program (script) that manipulates arrays using vector arithmetic. You will compute the values of points The ellipse has a major axis whose length is a and a minor axis whose length is b. For...

  • Don't attempt if you can't attempt fully, i will dislike a nd negative comments would be...

    Don't attempt if you can't attempt fully, i will dislike a nd negative comments would be given Please it's a request. c++ We will read a CSV files of a data dump from the GoodReads 2 web site that contains information about user-rated books (e.g., book tit le, publication year, ISBN number, average reader rating, and cover image URL). The information will be stored and some simple statistics will be calculated. Additionally, for extra credit, the program will create an...

  • Don't attempt if you can't attempt fully, i will dislike and negative comments would be given...

    Don't attempt if you can't attempt fully, i will dislike and negative comments would be given Please it's a request. c++ We will read a CSV files of a data dump from the GoodReads 2 web site that contains information about user-rated books (e.g., book titnle, publication year, ISBN number, average reader rating, and cover image URL). The information will be stored and some simple statistics will be calculated. Additionally, for extra credit, the program will create an HTML web...

  • This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation...

    This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation inside a class. Task One common limitation of programming languages is that the built-in types are limited to smaller finite ranges of storage. For instance, the built-in int type in C++ is 4 bytes in most systems today, allowing for about 4 billion different numbers. The regular int splits this range between positive and negative numbers, but even an unsigned int (assuming 4 bytes)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT