Using sum function to add varibles in a loop

2 vues (au cours des 30 derniers jours)
Zack
Zack le 25 Avr 2014
Commenté : Zack le 25 Avr 2014
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))

Réponse acceptée

Walter Roberson
Walter Roberson le 25 Avr 2014
bonus(j) = ....
rather than
bonus = ....
  1 commentaire
Zack
Zack le 25 Avr 2014
Thanks for the help that worked perfectly

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by