Using sum function to add varibles in a loop

조회 수: 17 (최근 30일)
Zack
Zack 2014년 4월 25일
댓글: Zack 2014년 4월 25일
Hi,
I am trying to get my program to store the bonuses variable until after the loop is over and display the summation of all the loop iterations. As it is written now it just displays the last iteration of the loop.
num=input('Enter Total Number of employees: ');
for j=1:num %number of employs for 2nd number
emp=input('Enter Employee ID Number: ');
r=input('Enter Employee Rating: ');
sal=input('Enter Employee Salary: ');
if r==5
bonus=sal*1.04;
elseif r==3
bonus=sal*1.02;
elseif r~=5 && r~=3
bonus=sal*1.01;
end;
end
disp(sum(bonus))

채택된 답변

Walter Roberson
Walter Roberson 2014년 4월 25일
bonus(j) = ....
rather than
bonus = ....
  댓글 수: 1
Zack
Zack 2014년 4월 25일
Thanks for the help that worked perfectly

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by