PROGRAM Histogram;
{Programmer: Gregory A. Perkins
CS 65
DUE: 01 November 1996}
{This program accepts heights(up to a max. of 100) of a sample of the population
and displays a histogram(in text mode) of those heights. It also displays the
mean height and standard deviation of the sample.}
{Declarations section}
VAR Person, Cat, Stars : INTEGER;
VAR Number : INTEGER; {Total number of elements in the data ARRAY}
VAR MaxHt, Line : INTEGER; {MaxHt=tallest category for display/Line used in display procedure}
VAR HtSum, HtMean, HtDiff, HtSD : REAL;
CONST MaxData = 100; {maximum number of data elements}
TYPE BodyStats = ARRAY[1..MaxData] OF INTEGER;
TYPE Hist = ARRAY[1..7] OF INTEGER;
VAR Height : BodyStats;
VAR HtCount : Hist;
BEGIN {program}
{Initialization section}
Person := 0; Cat := 0; Stars := 0;
Number := 0; HtSum := 0; HtMean := 0;
HtDiff := 0; HtSD := 0; MaxHt := 0; Line := 0;
{Input section: prompts user for the heights}
WRITELN('Please enter the height of the person indexed by');
WRITELN('the number that appears on the left of the screen.');
WRITELN('(Only enter heights between 130 and 199 centimeters.)');
WRITELN('');
WRITELN;
REPEAT
Number := Number + 1; {increase count of number of elements so far}
WRITE('Person ', Number:1, ': ');
READLN(Person);
IF (Person > 0) THEN {if data-entry isn't completed then}
BEGIN
Height[Number] := Person; {assign height to appropriate element of array}
END
ELSE
BEGIN
Number := Number - 1; {decrease total count of elements when user is finished}
END;
UNTIL ((Person <= 0) OR (Number = MaxData)); {stop when MaxData reached or user enters "0"}
WRITELN;
{Histogram and Height Mean section}
FOR Cat := 1 TO 7 DO
HtCount[Cat] := 0; {intitialize all height categories to "0"}
FOR Person := 1 TO Number DO
BEGIN
Cat := (Height[Person] DIV 10) - 12;
HtCount[Cat] := HtCount[Cat] + 1; {increment the height count for category}
HtSum := HtSum + Height[Person]; {get sum of all heights to determine mean}
END;
HtMean := HtSum/Number; {determine the mean height of the sample}
{Standard deviation section}
HtSum := 0;
FOR Person :=1 TO Number DO
BEGIN
HtDiff := Height[Person] - HtMean;
HtDiff := HtDiff * HtDiff;
HtSum := HtSum + HtDiff;
END;
HtSD := SQRT(HtSum/(Number - 1));
{Histogram 'vertical' display section}
{************************************************************************************}
WRITELN('Histogram of heights:');
WRITELN;
{Determine the Max Height of all the categories}
MaxHt := HtCount[1]; {initialize max height to first category}
FOR Cat := 2 TO 7 DO
IF (HtCount[Cat] > MaxHt) THEN
MaxHt := HtCount[Cat];
{Draw the vertical display}
FOR Line := MaxHt DOWNTO 1 DO
BEGIN
WRITE(' '); {sets spacing for display of categories at bottom}
FOR Cat := 1 TO 7 DO
IF (HtCount[Cat] >= Line) THEN
BEGIN
WRITE(' * '); {displays '*' in category for line}
END
ELSE
BEGIN
WRITE(' '); {displays empty category for line}
END;
WRITELN; {advance to next line in display}
END;
WRITELN('Category: 1 2 3 4 5 6 7'); {displays categories at bottom of histogram}
{************************************************************************************}
{Display the mean height and standard deviation}
WRITELN;
WRITELN('The mean height is: ', HtMean:5:1);
WRITELN('The standard deviation of heights is: ', HtSD:4:1);
END. {of program}