Square root table without using arrays

1 visualización (últimos 30 días)
Benjamin Baker
Benjamin Baker el 15 de Feb. de 2019
Editada: James Tursa el 15 de Feb. de 2019
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 comentarios
James Tursa
James Tursa el 15 de Feb. de 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 el 15 de Feb. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

James Tursa
James Tursa el 15 de Feb. de 2019
Editada: James Tursa el 15 de Feb. de 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.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by