Azzera filtri
Azzera filtri

Get rid of unwanted output

1 visualizzazione (ultimi 30 giorni)
Krish Desai
Krish Desai il 10 Dic 2015
Modificato: James Tursa il 10 Dic 2015
I have the following code
function output=beautyofmath(i)
for i = 1:9
if i == 1
j(i, 1) = i;
else
j(i, 1) = j(i - 1, 1) * 10 + i;
end
j(i, 2) = i;
j(i, 3) = j(i, 1) * 8 + j(i, 2);
fprintf('%d x 8 + %d = %d\n', j(i, 1), j(i, 2), j(i, 3));
end
The problem is it outputs the following no matter the i value. If I type in beautyofmath(5), I only want the first 5 values to show up. How do I fix this?
1 x 8 + 1 = 9
12 x 8 + 2 = 98
123 x 8 + 3 = 987
1234 x 8 + 4 = 9876
12345 x 8 + 5 = 98765
123456 x 8 + 6 = 987654
1234567 x 8 + 7 = 9876543
12345678 x 8 + 8 = 98765432
123456789 x 8 + 9 = 987654321

Risposta accettata

James Tursa
James Tursa il 10 Dic 2015
Modificato: James Tursa il 10 Dic 2015
You overwrite your input variable i with your for loop index i, which always goes up to 9. So do this instead:
function output=beautyofmath(i_max)
for i = 1:i_max

Più risposte (0)

Categorie

Scopri di più su Variables in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by