% avgGrades.m   P. Conrad, demo for CISC106 Fall 2006
% input a sequence of grades (all out of 100) and calculate the average


% *** Print welcome, and instructions to the user ***

fprintf('Welcome to the grade averager.  If you\n');
fprintf('input a sequence of grades, I can compute the average.\n');
fprintf('\n');
fprintf('I''ll ask for grades one at a time.\n');
fprintf('  * First I''ll ask you for a title for each grade (e.g. "hwk1")\n');
fprintf('  * Then I''ll ask for the score out of 100 points\n');
fprintf('  * Enter "done" for the title when you are finished\n');
fprintf('\n');

% **** set up variables

grades = [];  % an empty array at first; this will store all the grades
count = 0;    % this variable will count how many grades there are

% **** Main loop to accumulate grades

% Note the pattern: ask for input before the while loop, and at the 
% _bottom_ of the while loop.   See this pattern too, on p. 150-151 of
% Chapman textbook (MATLAB Programming for Engineers, 3rd Edition).

title = input('Enter a title for this grade (or "done" when finished): ','s');
while (strcmp(title,'done')==0)  % while title is NOT the word 'done' 
     % ask user for a grade

     prompt = ['Enter a grade out of 100 points for ' title ': '];
     thisGrade = input(prompt);

     count = count + 1;
     grades(count)  =  thisGrade;

     title = ...
	input('Enter a title for this grade (or "done" when finished): ','s');
end

% Use a for loop to print the values, and accumulate the sum
 
fprintf('\n***Summary of Grades***\n\n');

sum = 0; % initialize the sum

for (ii = 1:count)

  % figure out message
  if (grades(ii) < 60)
    message = 'failed'
  else
    message = 'passed'
  end

  % print a line of output for this grade
  fprintf('Grade #%2d was %3d (%s)\n',ii,grades(ii),message);

  % add this grade into the sum
  sum = sum + grades(ii);

end

% compute average and print it out
average = sum / count;
fprintf('\n\nThe average of your %d grades is %5.1f\n',count,average);



