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 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