Using repeating loops to calculate profits
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Payton Philip Clark Chase
el 9 de Dic. de 2019
Respondida: Ashutosh Thakur
el 30 de Jun. de 2022
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!
0 comentarios
Respuestas (1)
Ashutosh Thakur
el 30 de Jun. de 2022
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)
You can initialize Ptot(1) with P(1) and run the loop till length(P) - 1 because you are incrementing Ptot by i+1.
0 comentarios
Ver también
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!