Previous topic | Ada Home Page | Index

FOR example

Specification

A program is required to calculate the average number of assignments marked by a lecturer per month over a 12 month period. The program will ask the user for the number of assignments marked each month, calculate the average number marked per month, and display this value.

User interface


	ASSIGNMENT AVERAGE PROGRAM

	No marked in month  1   iiiiiiii
	No marked in month  2   iiiiiiii
	No marked in month  3   iiiiiiii
	No marked in month  4   iiiiiiii
	No marked in month  5   iiiiiiii
	No marked in month  6   iiiiiiii
	No marked in month  7   iiiiiiii
	No marked in month  8   iiiiiiii
	No marked in month  9   iiiiiiii
	No marked in month 10   iiiiiiii
	No marked in month 11   iiiiiiii
	No marked in month 12   iiiiiiii

	Average per month is    fffff.ff


Algorithm


	1. initialisation
		1.1 display heading
		1.2 set total to zero
	2. get values over year
		2.1 for each month
			2.1.1 prompt for and get number marked
			2.1.2 add to total
	3. calculate average
	4. display average

Data design

NAMETYPENotes
max_month(constant) 12No of months to process
ass_monthINTEGERNo of assignments in a month
total_assINTEGERTotal assignments in a year
this_monthINTEGERLoop parameter
average_assFLOATAverage assignments per month


Program

--------------------------------------------------------
-- Fintan Culwin Sept '88 v1.0
-- program to illustrate simple definite iteration
-- b1s5p1 see text p58-59
-- Modified by X. Yao on 18/05/97
--------------------------------------------------------

with TEXT_IO; use_text_io;

procedure b1s5p1 is

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

   max_month : constant integer := 12;  -- Months per year

   total_ass : INTEGER;                 -- total no ass
   ass_month : INTEGER;                 -- ass this month

   average_ass : FLOAT;                 -- average per month

begin -- b1s5p1 

   -- initialisation
   PUT_LINE("  ASSIGNMENT AVERAGE PROGRAM "); NEW_LINE;
   total_ass := 0.0;

   -- read in number of assignments marked each month
   for this_month in integer range 1 .. max_month loop
      PUT( "No. marked in month");
      PUT( this_month, width=>3); PUT( "  ");
      GET( ass_month ); SKIP_LINE;
      total_ass := total_ass + ass_month;
   end loop;

   -- calculate average
   average_ass := FLOAT(total_ass) / FLOAT(max_month);

   -- display average
   NEW_LINE;
   PUT( "Average per month is ");
   PUT( average_ass, fore=>6, aft=>2, exp=>0 ); 
   NEW_LINE;

end b1s5p1;


Previous topic | Ada Home Page | Index
x-yao@adfa.oz.au -- 18 May 1997