Square root table without using arrays

1 回表示 (過去 30 日間)
Benjamin Baker
Benjamin Baker 2019 年 2 月 15 日
編集済み: James Tursa 2019 年 2 月 15 日
I have to create a square root table like the one in the first comment. I can't use arrays, and have to print the results in the format displayed at the bottom of the assignment.
Here's what i have, and what it produces:
  3 件のコメント
James Tursa
James Tursa 2019 年 2 月 15 日
Please repost your code as regular text (*not* as a picture) highlighted by the "CODE" button in the format bar.
John D'Errico
John D'Errico 2019 年 2 月 15 日
So what is the question? Where is the problem? All we know is what your assignment is. Are you asking for advice? Use a loop.

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

採用された回答

James Tursa
James Tursa 2019 年 2 月 15 日
編集済み: James Tursa 2019 年 2 月 15 日
You need to put the new line \n in appropriate places in your loops. In particular, you only need one new line \n printed once you have all of the square roots for that particular line printed. E.g.,
for x=first:1:second
% print the x value here
for y=0:0.1:0.9
% print the sqrt(x+y) value here
end
% print the new line \n here (this is the only place you print the \n)
end
A tip: In general, it is not a good idea to have a loop dependent on fractional values like the 0:0.1:0.9 because that last one might not happen due to floating point arithmetic effects. A better way to code this would be to have the loop use integers y=0:9 and then use y/10 within the loop.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by