%-----------------------------------------------------------------------------% % GRADES PROCESSING EXAMPLE % % 10/21/2001 DIS % %-----------------------------------------------------------------------------% % Algorithm: % % ========== % % Initialize data: % % + no grade is current stored % % + sum of grades so far is zero % % + count of grades so far is zero % % + current max grade is minimum value or lower % % + current min grade is maximum value or higher % % Get the current grade. % % If the current grade is within bounds, % % + increment the count of legal grades % % + increment the sum of grades % % + store the current grade % % + Update the max grade: % % - if the current grade is bigger than the old max, % % the new max is the current grade % % - otherwise, keep the old max % % + Update the min grade: % % - if the current grade is smaller than the old min, % % the new min is the current grade % % - otherwise, keep the old min % % + Get the next grade. % % + Repeat. % % If the count of grades is still zero, then: % % - Nothing was entered. % % - Report a warning/alert. % % - Otherwise, report the data. % %-----------------------------------------------------------------------------% % Initialize data: % clc, clear % clean things up % highGrade = 100; % highest possible grade % lowGrade = 0; % lowest possible grade % maxGrade = lowGrade; % the max grade so far % minGrade = highGrade; % the min grade so far % sumGrade = 0; % sum of grades entered so far % countGrade = 0; % count of VALID grades so far % allGrades = []; % collection of grades so far % %-----------------------------------------------------------------------------% % Welcome the user: disp('+---------------------------------------------------------+'); disp('| Welcome to the grades processing program! |'); disp('| You will be prompted to enter grades. |'); disp('| Be sure to enter real, finite numbers. |'); disp('| Enter a value less than 0 or greater than 100 to quit. |'); disp('+---------------------------------------------------------+'); %-----------------------------------------------------------------------------% % Prompt for first grade. The user must enter a real, finite number. % Reprompt the user otherwise: grade = str2double(input(' Enter a grade: ','s')); while ~isreal(grade) | isinf(grade) | isnan(grade) | ~isnumeric(grade) disp(' Please enter a real, finite number: '); grade = str2double(input(' Enter a grade: ','s')); end %-----------------------------------------------------------------------------% % Process grades: % - store the grades % - find the sum and count % - determine the max/min grades % Stop processing when the user enters an out-of-bounds grade: while grade >= lowGrade & grade <= highGrade % Increment count of valid grades: countGrade = countGrade + 1; % Accumulate sum of valid grades: sumGrade = sumGrade + grade; % Store the current grade: allGrades(countGrade) = grade; % Update the max grade so far, if necessary: if grade > maxGrade maxGrade = grade; end % Update the min grade so far, if necessary: if grade < minGrade minGrade = grade; end % Get next grade: grade = str2double(input(' Enter a grade: ','s')); while ~isreal(grade) | isinf(grade) | isnan(grade) | ~isnumeric(grade) disp(' Please enter a real, finite number: '); grade = str2double(input(' Enter a grade: ','s')); end end % continue processing grades %-----------------------------------------------------------------------------% % Report the number of valid grades entered: disp('+---------------------------------------------------------+'); disp('| Done processing grades! |'); disp('+---------------------------------------------------------+'); if countGrade==0 disp('| No valid grades entered! |'); disp('+---------------------------------------------------------+'); else disp([' Amount of grades entered: ',num2str(countGrade)]); disp([' Maximum grade entered: ',num2str(maxGrade)]); disp([' Minimum grade entered: ',num2str(minGrade)]); disp([' Average of all grades: ',num2str(sumGrade/countGrade)]); disp([' Collection of grades:']); for ii=1:countGrade disp([' Count: ',num2str(ii),', Grade: ',num2str(allGrades(ii))]); end disp('+---------------------------------------------------------+'); end %-----------------------------------------------------------------------------% % Future additions: % - round average % - other statistical data % - plot grades with hist(allGrades) % - prompt user to enter Y or N to continue program % choice = input(.....), while choice...., reprompt, end % - store grade info in file %-----------------------------------------------------------------------------%