Web Design, Development & Marketing

Web Development Articles

Pascal: Average Value Calculator

Here's a program written in Pascal that demonstrates how to calculate the average of several numbers. This was written "back in the day" when I was starting out in programming. Decided to share it online as it may prove useful to other newbies out there. Enjoy!

program MarkAverage(input,output);
uses crt;
{Author: Jonathan Jackson}

var
  {Declaring variables}
  value, total_value, no_of_values:integer;
  average:real;

Procedure initialise;
begin
  {Sets values to zero}
  no_of_values:=0;
  value:=0;
end;

Procedure input_and_process;
begin
  gotoxy(4,2);
  writeln('****************************');
  gotoxy(4,3);
  writeln('* Average Value Calculator *');
  gotoxy(4,4);
  writeln('****************************');
  gotoxy(4,5);
  writeln('By Jonathan Jackson');
  gotoxy(1,8);
  write('Please enter the first value (-1 to end): ');
  readln(value);
  while value<>-1 do
  begin
    total_value:=total_value+value;
    no_of_values:=no_of_values+1;
    write('Please enter the next value (-1 to end): ');
    readln(value);
  end;
end;

Procedure output_result;
begin
  {Outputs the calculated result}
  average:=total_value/no_of_values;
  writeln('');
  writeln('Average value is ',average:5:2);
  writeln('Total number of values is ',no_of_values:3);
  writeln('Press <Enter> to enter another set of values');
  readln;
  clrscr;
  initialise;
  input_and_process;
  output_result;
end;

begin
  initialise;
  input_and_process;
  output_result;
end.

Free Pascal is a free pascal compiler you can use to edit and compile pascal programs.

Subscribe to RSS Feed Bookmark and Share

Related Links

Related Articles / Posts

Pascal: Hello World Beginner's Program Code (03/05/2006)

Pascal: Average Value Calculator (03/05/2006)