Using repeating loops to calculate profits

1 回表示 (過去 30 日間)
Payton Philip Clark Chase
Payton Philip Clark Chase 2019 年 12 月 9 日
回答済み: Ashutosh Thakur 2022 年 6 月 30 日
Write a program which calculate the total profit/year by the end of each month in a year, given the variable P which stores profit/month for that year:
P= [10 7 5 4 6 8 1 6 8 5 11 12]*1000
Set up a loop to calculate Ptotal(i+1)=Ptotal(i)+P(i+1), to help decide about start/end conditions, use an iteration table.
clear
clc
P= [10 7 5 4 6 8 1 6 8 5 11 12]*1000;
Ptot = 1;
for i = 1:length(P)
if Ptot(i)==1
Ptot=P
else
Ptot(i+1) = Ptot(i)+P(i+1)
end
end
disp(Sum);
Could someone help me with this, i've been stuck on this question for a while and just kind of lost? I did a similar question earlier and got the right answer but because the index is start at 0 instead of 1 is making me kind of confused on how to go about this question?
Thanks for the help!

回答 (1 件)

Ashutosh Thakur
Ashutosh Thakur 2022 年 6 月 30 日
You can follow this code for understanding about start/end conditions
P= [10 7 5 4 6 8 1 6 8 5 11 12]*1000;
Ptot(1) = P(1);
for i = 1:length(P)-1
Ptot(i+1) = Ptot(i) + P(i+1);
end
disp(Ptot)
10000 17000 22000 26000 32000 40000 41000 47000 55000 60000 71000 83000
You can initialize Ptot(1) with P(1) and run the loop till length(P) - 1 because you are incrementing Ptot by i+1.

カテゴリ

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

Translated by