how to solve this using the for loop and if
이전 댓글 표시
the price, in dollars, of a certain stock over a 10 day period is given in following array.
price=[19,18,22,21,25,19,17,21,27,29]
suppose you owned 1000 shares at the start of the 10 day period, and you bought 100 shares everyday the price was below $20 and sold 100 shares every day the price was above $25.
compute:
1-the amount you spent in buying share
2-the amount you received in selling shares
3-total number of shares you own after the 10th day
4-the net increase in the worth of your portfolio
x=[19,18,22,21,25,19,17,21,27,29]
p=0;
for i=1:10;
if x(i)<20;
p=p+(100*(x(i)));
end
U=sum(p)
end
k=0;
for i=1:10;
if x(i)>25
k=k+(100*x(i))
end
end
total=1000
댓글 수: 1
Walter Roberson
2013년 5월 4일
What problem are you observing?
Why are you overwriting "U" during each iteration of the first "for" loop ?
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Financial Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!