Previous topic | Ada Home Page | Index

Loop example

Finding largest and smallest values

Specification

A program is required to process a batch of values representing the scores of students on an exam. Valid scores for the exam are integers in the range 0 to 100. The number of students taking the exam is not known in advance, and it is possible that no students take the exam. The end of the batch of scores will be indicated by the user entering a negative value as the score.

When all the scores have been processed, a summary showing the number of students taking the exam, the highest score, the lowest score, and the average score (accurate to one decimal place) is to be displayed. The summary information is to be suppressed if no students take the exam.

User interface


	EXAM ANALYSIS

	Enter score (or -1 to finish)	iiiii
	Enter score (or -1 to finish)	iiiii	
		...	...	...	...	...	...
	Enter score (or -1 to finish)	-1

	Number of students is		iiiii
	Highest score is		iiiii
	Lowest  score is		iiiii
	Average score is		fff.f

Data design

NAMETYPENotes
num_stuINTEGERno of students
min_so_farINTEGERminimum so far
max_so_farINTEGERmaximum so far
this_scoreINTEGERcurrent student's score
total_scoresINTEGERtotal of all scores
average_scoreFLOATaverage score per student

Algorithm


	1. do init
		1.1 get a score
		1.2 init variables
	2. do students
		2.1 while more scores
			2.2 increment number of students
			2.3 add score to total
			2.4 check if have a new max or min score
			2.5 prompt for and get next score
	3. do summary
		3.1 display number of students
		3.2 if some students took the exam
			3.3 calculate average
			3.4 display summary info


Program

-- Fintan Culwin Sept '88 v1.0
-- program to illustrate simple definite iteration
-- b1s6p1 see text pp75-76

with TEXT_IO;
package int_io is new TEXT_IO.INTEGER_IO( INTEGER );
with TEXT_IO;
package float_inout   is new TEXT_IO.FLOAT_IO( FLOAT );
with TEXT_IO, int_io, float_inout;
use  TEXT_IO, int_io, float_inout;

procedure b1s6p2  is
     num_stu       : INTEGER;   -- num students processed
     min_so_far    : INTEGER;   -- min score recorded
     max_so_far    : INTEGER;   -- max score recorded
     total_score   : INTEGER;   -- total of all scores
     this_score    : INTEGER;   -- current score
     average_score : FLOAT;     -- final average
begin -- of b1s6p1
   -- do init
   PUT( "Enter score (or -1 to finish) ");
   GET( this_score); SKIP_LINE;

   num_stu := 0;                -- zero num students done
   total_score := 0;            -- total score is zero
   min_so_far := this_score;    -- min score so far
   max_so_far := this_score;    -- max score so far

   -- do students
   while (this_score >= 0) loop
      num_stu := num_stu + 1;
      total_score := total_score + this_score;

      if this_score > max_so_far then
         max_so_far := this_score;
      elsif this_score < min_so_far then
         min_so_far := this_score;
      end if;

      PUT("Enter score (or -1 to finish) ");
      GET( this_score); SKIP_LINE;
   end loop;

   -- do summary
   NEW_LINE(2);
   PUT("No of students is "); PUT( num_stu, width => 4);
   NEW_LINE;

   -- if some students took the exam, display summary info
   if (num_stu > 0) then
      average_score := float(total_score)/float(num_stu);
      PUT("Highest score is "); 
      PUT( max_so_far, width => 8); NEW_LINE;
      PUT("Lowest  score is "); 
      PUT( min_so_far, width => 8); NEW_LINE;
      PUT("Average score is ");
      PUT( average_score, fore => 4, aft => 2, exp => 0);
      NEW_LINE;
   end if;
end b1s6p2;


Previous topic | Ada Home Page | Index
c-lokan@adfa.oz.au / 14 Feb 96