My Function is supoosed to return 55 when i call it with 10 based off of pyramid sum. However my function gives me 20. Please help

1 Ansicht (letzte 30 Tage)
function CountUpToN(N)
i=0;
for i=1:N
i=i+N
disp(i)
end

Akzeptierte Antwort

Les Beckham
Les Beckham am 30 Sep. 2017
Bearbeitet: Les Beckham am 30 Sep. 2017
The main problem with this code is that you are using the same variable for the loop index and for accumulating your result. The for loop will replace i with the next loop index each time through the loop, thus obliterating your accumulated result.

Weitere Antworten (0)

Kategorien

Mehr zu Introduction to Installation and Licensing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by