Need help with summation in a while loop. i believe my argument inside of while is wrong, but i'm not sure how to fix. Assignment is to Write a while loop that assigns summedValue with the sum of all values from 1 to userNum.(assume userNum>=1).

3 ビュー (過去 30 日間)
function summedValue = SummationWithLoop(userNum)
x=1:1:userNum;
xSum=0;
k=1;
while (xSum<=userNum)
xSum = xSum+x(k);
k=k+1;
end
  2 件のコメント
Stephen23
Stephen23 2017 年 9 月 21 日
@Snooping Poppet: it is not appreciated when you edit away the text of your question. Imagine if everyone did that, then this entire forum would be useless for anyone else as it would only consist of thousands of threads all reading "This post was taken down". Would you find that useful?
By deleting your question you unilaterally decided that jlt199's volunteered time helping you shall not be useful to anyone else. Does jlt199 get a say in this decision? Do you think this is why we volunteers come here and write our advice and comments?
If you want a consulting service then you will find plenty of them on them internet. This is a community forum with answers provided by volunteers.

サインインしてコメントする。

採用された回答

jlt199
jlt199 2016 年 9 月 15 日
You don't need your variable x. k starts at one and increases by one with each iteration, so achieves the same thing as you are trying to do with the vector x. Just add k to your sum instead.
Also your while loop condition won't work properly, but I'll let you have a go at figuring that out.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by