Square root table without using arrays

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by