Square root table without using arrays

3 views (last 30 days)
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 Comments
James Tursa
James Tursa on 15 Feb 2019
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 on 15 Feb 2019
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.

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 15 Feb 2019
Edited: James Tursa on 15 Feb 2019
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.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!