Perform For-loop and if-else

Hi, could anyone help me in this questions? Can't really understand how to do as I just can generate the random integers only..
You are required to write a script to add two ten-digit random integer and print the result. Use for-loop and if-else in your script.
The script for integers: n=randi([1e9,1e10-1],1,2)
Thanks for the help...

5 件のコメント

KSSV
KSSV 2020 年 11 月 20 日
What have you attempted?
Alex Xiang
Alex Xiang 2020 年 11 月 20 日
I have attempted to generate the random integers that contains 10 digits only... as I'm not understand the for-loop and if-else but it do require me to do in this question...
Alex Xiang
Alex Xiang 2020 年 11 月 20 日
編集済み: dpb 2020 年 11 月 20 日
For the section of For-loop, is my script correct to use?
for n=randi([1e9,1e10-1],1,2);
fprintf('value of n: %d\n', n);
end
And here is the result:
value of n: 3853895320
value of n: 9551998439
dpb
dpb 2020 年 11 月 20 日
I suppose the idea of the instructor was to generate the 10-digit numbers a digit at a time....that way could use a loop and have a test for a certain case with the result being a zero.
Your technique is much more "the MATLAB way" to write code; it would seem you're too far ahead for the level the class has reached so far.
I'd probably submit your one-liner but also the solution as asked for by the assignment.
Rik
Rik 2020 年 11 月 20 日
You can also be too smart for your own good by submitting something like this:
if false
%satisfy requirement of using if/else
else
for n=1
%satisfy requirement of using for loop
num=randi([1e9,1e10-1],1,2);
%now add the numbers and fprintf the result
end
end
This might not be a smart thing to submit, although if your instructor is anything like me, you would get a chuckle and maybe a bonuspoint.

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2020 年 11 月 20 日

コメント済み:

Rik
2020 年 11 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by