hi im purely a beginner and im trying to write a program that will allow us to see graphically the number of people of every diferent age range in a given community then it should show the results graphically. i have done mostly all of the code and i just need help on displaying the results graphically. like this the stars need to be the number people. i need a procedure to add to my existing code so it will allow me to use stars to show the number of poeple in each age range.

example:

0-5 ***
6-11 ***
12-18 **
19-35 *****
36-55 *
56-65 *
66+  ****

this is my code.

im stuck on the write_age procedure. please help if u can >>>

program assignment2;
{$APPTYPE CONSOLE}
uses
SysUtils;


const population= 2;
type TpopRec = record
age:integer;
agerange: string
end;
Tperson = array[1..population] of TpopRec;


{PRE: true
POST: initialise the ages of all population}
procedure initialise_age(var ftn:Tperson);
var i:integer;
begin
for i:=1 to population do
begin
writeln('Introduce age of person ', i);
readln(ftn.age);
ftn.age:=0;
end;
end{initialise_age};
{PRE: true
POST: show the age ranges of all the population}
procedure write_age(ftn:Tperson);
var
i:integer;
begin
writeln('the ages are:');
for i:=1 to 7 do
writeln('0-5','age:',ftn.age);
end;


{PRE: ftn not empty}
{Post: Introduce ages for the population}
procedure intro_age(var ftn:Tperson);
var
i, age:integer;
begin
writeln('introduce the ages for the 10 people');
for i:=1 to 10 do
begin
read(age);
ftn.age:=age;
end;
end;
{PRE: and ftn is not empty
POST: average age for the population}
function average_age(ftn:Tperson):real;
var av:real;
std:integer;
begin
av:=0;
for std:=1 to population do
begin
av:=av+ftn[std].age;
end;
result:=av/population;
end;


{PRE: ftn is not empty
POST: maximum age}
function max_age(ftn:Tperson):integer;
var
maximum: integer;
i: integer;
begin
for i:=1 to population do
begin
if maximum < ftn.age then maximum:=ftn.age;
end;
result:=maximum;


end;
{PRE: ftn is not empty
POST: minimum age}
function min_age(ftn:Tperson):integer;
var
minimum: integer;
i: integer;
begin
for i:=1 to population do
begin
if minimum > ftn.age then minimum:=ftn.age;
end;
result:=minimum;
end;



{PRE: true
POST: menu with all the functionalities}
procedure menu(var ftn:Tperson; c:char);
begin
case c of
'r', 'R': intro_age(ftn);
'w','W': write_age(ftn);
'a','A': writeln('Average mark is: ',average_age(ftn));
'M': writeln('Maximum mark is: ', max_age(ftn));
'm': writeln('Minimum mark is: ', min_age(ftn));
else
writeln('error this option has not been recognised');
end;
end; {menu}


procedure prompts;
begin
writeln('Press r to introduce all the ages');
writeln('Press w to show all the ages');
writeln('Press a to calculate  average age');
writeln('Press M to calculate maximum age');
writeln('Press m to calculate minumum age');
writeln('Press q to quit');
end;


var
FTN:Tperson;
choice:char;
begin
writeln('Welcome to population statistics');
initialise_age(ftn);
writeln ('You have the following options');
prompts;
readln(choice);
while (choice <> 'q') and (choice<> 'Q') do
begin
menu(FTN,choice);
prompts;
readln(choice);
end;


writeln('Bye...');
readln;

end.

its got a menu so when i press w it should show the diferent age ranges and stars next to it whioch indicate no of people which i cant do so please help if u can . thanks in advance for any help. greatly appreciated.

Dani AI

Generated

Brief companion notes to complement ’s working solution and ’s case-based suggestion — focused on the real bugs in the original code, why they matter, and a few robustness tips to make the histogram reliable for larger or messy input.

Common bugs found in the original submission

  • Array fields were accessed without indexing and sometimes immediately overwritten; that destroys the input.
  • The population constant and loop bounds were inconsistent (the code used both 2 and 10); loops should use the array bounds (Low/High) or a single population value.
  • Max/min variables were not initialized before comparison, which produces undefined results.
  • No validation of input ages (negative values or implausible ages).
  • The write/display routine tried to print ranges directly instead of counting into buckets first.

Recommended, practical approach (algorithmic)

  • Read and store ages into the array with the correct index; validate each value (e.g. 0..120) and reject or re-prompt bad input.
  • Maintain seven integer counters (one per range). Increment the appropriate bucket for each recorded age — a case/range construct keeps this tidy.
  • Determine the largest bucket count; if that max is large, scale the star display so the longest line stays readable (choose a max display width like 40–60 stars and compute a scale factor). Always show the raw count (and optionally the percentage) beside the stars so scaled displays remain informative.
  • Initialize counters and min/max properly (either use the first element as seed or explicit sentinel values) and loop with Low/High to avoid off-by-one or hard-coded sizes.

Small polish items

  • Pad the labels for neat alignment, print totals, and consider letting the user pick “stars” or “numeric” output. These additions keep the program usable as population size grows and make the output trustworthy for later review.

Hope this helps...

program a2;
{$APPTYPE CONSOLE}
uses
  SysUtils;

const
   population = 10;
type
   TpopRec = record
      age: integer;
      agerange: string
   end;
   
   Tperson = array[1..population] of TpopRec;

{PRE: true
 POST: initialise the ages of all population}

procedure initialise_age(var ftn: Tperson);
var
   i: integer;
begin
   for i := Low(ftn) to High(ftn) do
   begin
      writeln('Introduce age of person ', i);
      readln(ftn[i].age);
   end;
end {initialise_age};

{PRE: true
POST: show the age ranges of all the population}

procedure write_age(ftn: Tperson);
var
   AgeGroup: string;
   Count: array[1..7] of Integer;
   I: Integer;
   J: Integer;
begin
   { Initialize Count to 0 (zero) }
   for I := 1 to 7 do
   begin
      Count[I] := 0;
   end;

   { Count the number of ages in each of the seven groups. }
   for I := Low(ftn) to High(ftn) do
   begin
      if (ftn[I].age <= 5) and (ftn[I].age >= 0) then
      begin
         Count[1] := Count[1] + 1;
      end
      else if (ftn[I].age <= 11) and (ftn[I].age >= 6) then
      begin
         Count[2] := Count[2] + 1;
      end
      else if (ftn[I].age <= 18) and (ftn[I].age >= 12) then
      begin
         Count[3] := Count[3] + 1;
      end
      else if (ftn[I].age <= 35) and (ftn[I].age >= 19) then
      begin
         Count[4] := Count[4] + 1;
      end
      else if (ftn[I].age <= 55) and (ftn[I].age >= 36) then
      begin
         Count[5] := Count[5] + 1;
      end
      else if (ftn[I].age <= 65) and (ftn[I].age >= 56) then
      begin
         Count[6] := Count[6] + 1;
      end
      else if ftn[I].age >= 66 then
      begin
         Count[7] := Count[7] + 1;
      end;
   end;

   writeln('the ages are:');
   for I := 1 to 7 do
   begin
      case I of
         1: AgeGroup := '0-5:   ';
         2: AgeGroup := '6-11:  ';
         3: AgeGroup := '12-18: ';
         4: AgeGroup := '19-35: ';
         5: AgeGroup := '36-55: ';
         6: AgeGroup := '56-65: ';
         7: AgeGroup := '66+:   ';
      end;

      write(AgeGroup);
      if Count[I] > 0 then
      begin
         for J := 1 to Count[I] do
         begin
            write('*');
         end;
      end;
      writeln;
   end;
end;

{PRE: ftn not empty}
{Post: Introduce ages for the population}

procedure intro_age(var ftn: Tperson);
var
   i, age: integer;
begin
   writeln('introduce the ages for the 10 people');
   for i := 1 to 10 do
   begin
      read(age);
      ftn[i].age := age;
   end;
end;
{PRE: and ftn is not empty
POST: average age for the population}

function average_age(ftn: Tperson): real;
var
   av: real;
   std: integer;
begin
   av := 0;
   for std := 1 to population do
   begin
      av := av + ftn[std].age;
   end;
   result := av / population;
end;

{PRE: ftn is not empty
POST: maximum age}

function max_age(ftn: Tperson): integer;
var
   maximum: integer;
   i: integer;
begin
   maximum := 0;

   for i := 1 to population do
   begin
      if maximum < ftn[i].age then
         maximum := ftn[i].age;
   end;
   result := maximum;

end;
{PRE: ftn is not empty
POST: minimum age}

function min_age(ftn: Tperson): integer;
var
   minimum: integer;
   i: integer;
begin
   minimum := 1000;
   for i := 1 to population do
   begin
      if minimum > ftn[i].age then
         minimum := ftn[i].age;
   end;
   result := minimum;
end;

{PRE: true
POST: menu with all the functionalities}

procedure menu(var ftn: Tperson; c: char);
begin
   case c of
      'r', 'R': intro_age(ftn);
      'w', 'W': write_age(ftn);
      'a', 'A': writeln('Average mark is: ', average_age(ftn));
      'M': writeln('Maximum mark is: ', max_age(ftn));
      'm': writeln('Minimum mark is: ', min_age(ftn));
   else
      writeln('error this option has not been recognised');
   end;
end; {menu}

procedure prompts;
begin
   writeln('Press r to introduce all the ages');
   writeln('Press w to show all the ages');
   writeln('Press a to calculate average age');
   writeln('Press M to calculate maximum age');
   writeln('Press m to calculate minumum age');
   writeln('Press q to quit');
end;

var
   FTN: Tperson;
   choice: char;
begin
   writeln('Welcome to population statistics');
   initialise_age(ftn);
   writeln('You have the following options');
   prompts;
   readln(choice);
   while (choice <> 'q') and (choice <> 'Q') do
   begin
      menu(FTN, choice);
      prompts;
      readln(choice);
   end;

   writeln('Bye...');
   readln;

end.

thanks mate it works nice one much appreciated

You could also neaten up the if..then..else..if ladder using a case, like this:

[B]for[/B] I := low( ftn ) [B]to[/B] high( ftn ) [B]do[/B]
[B]begin[/B]
  [B]case[/B] ftn[I].age [B]of[/B]
      0..5 : inc( Count[1] );
     6..11 : inc( Count[2] );
    12..18 : inc( Count[3] );
    19..35 : inc( Count[4] );
    36..55 : inc( Count[5] );
    56..65 : inc( Count[6] );
    [B]else[/B]     inc( Count[7] );
  [B]end[/B];
[B]end[/B];

There's a lot less typing there, and less typing means fewer chances for mistakes. Plus, IMNSHO, cases make for easier reading. Use them wherever you can.

Daaave

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.