Filter löschen
Filter löschen

creating 30 digits numbers and finding sum

5 Ansichten (letzte 30 Tage)
Nabin SUNAM
Nabin SUNAM am 17 Feb. 2015
Bearbeitet: Stephen23 am 5 Mär. 2015
this is the question basically: add two 30 digit numbers and print the result. This is how it wants me to do it: store the integers in vectors, where each element in the vector stores a digit of the integer. Your script should initialize two 30 digit integers, storing each in a vector and then add these integers, also storing the result in a vector. Create the original numbers using the randi function.
This is what I have done so far:
clear all
num1 = zeros(1,30);
for i = 1:30
num1(i) = randi(9);
end
fprintf('\n')
num1;
fprintf('%d',num1); fprintf('\n')
num2 = zeros(1,30);
for j = 1:30
num2(j) = randi(9);
end
fprintf('\n')
num2;
fprintf('%d',num2); fprintf('\n')
This gives me two integers. I need to add them and display the result. Am I doing good so far? Any idea on how to approach the sum now?

Antworten (2)

Stephen23
Stephen23 am 18 Feb. 2015
Bearbeitet: Stephen23 am 5 Mär. 2015
Two useful tips for learning MATLAB:
  • Get comfortable using the documentation : it has lots of examples and information that will make your own life easier! Learn to navigate the Contents (on the left-hand side of the webpage).
  • Learn about vectorization and how to write efficient code right from the beginning, rather than learning poor coding practices first and trying to unlearn them later...
Some examples from your code of how doing these can be useful. These four lines
num1 = zeros(1,30);
for i = 1:30
num1(i) = randi(9);
end
can be entirely replaced with one randi call
num1 = randi(9,1,30);
which is faster, neater and much easier to read!
But there is still a mistake here which we can fix by reading the randi documentation. It states quite clearly that randi(imax) returns a pseudorandom scalar integer between 1 and imax., so your code
randi(9)
will return any of 1,2,3,4,5,6,7,8,9 but excludes 0 because the lower bound of randi is one, not zero. Did you notice that zero is missing? How would you include zero in this list? (Hint: try a different upper bound, and subtract one). This brings us onto one more tip:
  • Always check your code as you are writing it! Check that each line does exactly what you want it to do. Do not write a massive amount of code, try to run it, and then get confused because you don't know where the mistake occurs.
To perform the sum you should consider:
  • preallocate a vector of zeros to put the sum in.
  • use a for-loop to iterate over the length of your vectors.
  • start at the ones (the right-end) and add the numbers one at a time.
  • take care of any remainders!

James Tursa
James Tursa am 17 Feb. 2015
Bearbeitet: James Tursa am 17 Feb. 2015
>> "Any idea on how to approach the sum now?"
Well, if you had two 30-digit numbers written on a piece of paper, how would you go about adding them? You would line up the columns and add them one column at a time with carry-over. So code up the same thing in MATLAB for these two numbers. There are ways to vectorize some of this, but I would suggest you simply code up a simple loop to do it the way you would do it on paper first.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by