Solved

What Am I Doing Wrong???

Posted on 1998-01-27
8
208 Views
Last Modified: 2012-06-21
I am working on a program which I have basically completed the best I can.  I really need someone to tell me where my mistakes are and what I am doing wrong.  I am a beginner and I am having trouble with this as I'm sort of confused as to what I am doing.  I really would appreciate any help anyone can give me.  I have worked for hours on this and I am frustrated beyond believe.  In the work I have done prior I didn't have anything this complicated and my books don't tell me enough information.  The following is what I have written so far.  It's quite lengthy.  At the end I can point out where the problems are but I printed the whole thing in case it's my variables wrong or something.  I have asked this question before and I thought I could write it but it won't compile or do anything.

PROGRAM CattleRanchSummary (input, InFile, output);
{***********************************************************

  Input (file):   Color, weight and milk information.
  Purpose:        To see if their are noticeable
                  differences in average weight and milk
                  yields in the different color groups of
                  cows.
  Output:         The number of cows in each color group,
                  their average weight and average milk
                  yield.
***********************************************************}
CONST
MinWeight = 300.0;  MaxWeight = 799.9; {to validate weight}
MinMilk = 0.0;      MaxMilk = 399.9;   {to validate milk/m}
MaxNoOfCowsPerGroup = 50;
Hypens = ‘-----------------------------‘;
                                       {to separate lines  
                                       of output}
DiskNameOfInputFile = ‘CATTLE.DAT’;
TYPE
ColorType =   (black, red, white); {to be neat and index  
                                    the Counter array and
                                    the first dimension of
                                    Herd by color }
WeightMilkType =   array [1..2] of real;
                                    {pair of real numbers
                                     to store the weight
                                     and milk data of one
                                     cow }
GroupType =   array [1..MaxNoOfCowsPerGroup ] of
              WeightMilkType;
                            {one WeiMilType pair for each
                             of a possible maximum of
                             MaxNoOfCowsPerGroup cows in a
                             color group }
HerdType =   array [ColorType] of GroupType;
                             {one GroupType array for each
                             of the three color groups }
CounterType =  array [ColorType] of integer;
                             {three integers to count the
                              actual number of cows in each
                              color group}
string19 =   string[19];  {this type of variable will hold
                           the error messages passed from
                           the input validating routine}
string[5] =   string[5];  {for ColorOut}
InFileType =   text;   {type of input file}
       
CONST
ColorOut:  array[ColorType] of string5 = (‘Black’, ‘Red’,
                                          ‘White’);
                                {comes handy when labelling
                                 the output results}
ColorCode:  array [ColorType] of char = (‘B’,’R’,’W’);
                                {to validate color-code and
                                 to convert code to a
                                 ColorType value}
VAR
Herd:   HerdType;   {3-dim array for weight and milk
                     data of the herd}
Counter:  CounterType;  {a counter of the cows for each
                         color group}
InFile  InFileType;  {file variable; internal name of input
                      file}
i:  ColorType  {index for Counter array and first-dim of
                Herd}
{**********************************************************}
PROCEDURE LoadHerd (var InFile: InFileType;{input file
                                           of cattle.data}
                    var herd: HerdType; {array to hold
                                         weight/milk data}
                    var counter: CounterType);
                            {# of cows in each of 3 groups}
{
  imports:  input file variable, InFile
  input:    color and a WeightMilkType pair for each cow
            in the herd; from InFile it handles all input
            tasks, including assigning and resetting
            InFile.  It reads in and validates data for
            each cow until the end of InFile is reached. 
            Valid data is stored in the 3-dimensional array
            Herd.  The first dimension segregates the cows
            by color, the second dimension distinguishes
            individual cows within a color group, and the  
            third dimension discerns weight and monthly
            yield of milk.  The cows in each color group
            are counted by array Counter.  If invalid input
            data is detected, then it displays an
            appropriate error message, and ignores the
            invalid data line.
  exports: InFile (position pointer at eof mark)
           Herd, filled with weight/milk data
           Counter, giving the # of cows for each color
           group
output:  if invalid data is detected, then one of three
         types of error messages is sent to the screen  }

{**********************************************************}
FUNCTION Convert ( c : char ) : ColorType;
{
  imports: a valid color code, ‘B’, ‘R’, ‘W’
  task:    convert the code into the pertinent ColorType
           value, black, red, or white
  exports: the function value, ColorType value pertaining
           to ColorCode }

VAR
   CCI:ColorType;
BEGIN
   Ch := UpCase(Ch);
   CCI := ";
   CASE OF Ch 'B':CCI := 'Black';
      'R': CCI := 'Red';
      'W': CCI := 'White';
   END{Case};
  Convert := CCI;
END {Convert}
{**********************************************************}
PROCEDURE Validate ( color:char; {value parameter for color
                                  code}
                     WeiMil:WeightMilkType; {value
                               parameter for weight & milk}
                     VAR Weight, Milk:integer;
                     var ERROR:string 19);  {exports an
                                        error message or "}
{
imports:  data of a cow, in color and WeiMil
task:     validates color, weight, and milk.  If all are
          valid, then ERROR exports the null string (ERROR
          := "); Otherwise, ERROR exports one of the
          following error messages: ‘invalid color code’
                                    ‘out of range milk/m’
                                    ‘out of range weight’
exports:  an error message if input error is detected; the
          null string otherwise.  }
 
Label Skip;
BEGIN
   Error := ";
   IF (Color <> Black) AND (Color <> Red) AND
        (Color <> White) THEN
      BEGIN
         Error := 'Invalid Color Code';
         GO TO Skip;
      END
   IF (Weight < MinWeight) OR (Weight > MaxWeight) THEN
      BEGIN
         Error := 'Out of Range Weight';
         GO TO Skip;
      END;
   IF (Milk < MinMilk) OR (Milk > MaxMilk) THEN
      BEGIN
         Error := 'Out of Range Milk/M';
         GO TO Skip;
      END
   Skip;
END{Validate};
{**********************************************************}
VAR
   CowColor:char;
   j:=integer;

BEGIN{LoadHerd}
   Assign (InFile, 'CATTLE.DAT');
   Reset (InFile);
   FOR CowColor := black to white DO
      Counter := 0
      WHILE NOT seekeof(InFile) DO
      BEGIN
         read(InFile, CowColor);
         FOR j:= 1 TO 2 DO
         read(InFile, WeightMilk[j];
         readln(InFile);
         Validate (CowColor, WeightMilk, Error);
         IF (Color <> Black) AND (Color <> Red)AND
              (Color <> White) THEN
           writeln(Error);
         ELSE
           CowColor := Convert(CowColorCode)
           IF (Color = 'B') THEN
              Counter[black]= Counter[black]+1;
              Herd[black, Counter[black] := WeightMilk;
           ELSE IF
              (Color = 'R') THEN
              Counter[red]= Counter[red]+1;
              Herd[red, Counter[red] := WeightMilk;
           ELSE IF
               (Color = 'W') THEN
              Counter[white]= Counter[white]+1;
              Herd[white, Counter[white] := WeightMilk;
           END{IF};
    END{LoadHerd}
{**********************************************************} PROCEDURE PrintAverages ( cows: GroupType; n integer ;
                           C: ColorType);
                           VAR Number:integer; {for loop
                                                control}
{
imports:  weight and milk/m data of a group; and number of
          cows in the group
task:     computes average weight and milk\m, and produces
          five lines of display:
          a line of hyphens
          a line reporting the number of cows
          a line reporting the average weight of the cows
          a line reporting the average monthly yeild of    
          milk
          a line of hypens.  }
VAR
   SumWeight, AvgWeight, SumMilk, AvgMilk: real;

BEGIN
   SumWeight := 0;
   AvgWeight := 0;
   SumMilk := 0;
   AvgMilk := 0;
   FOR Number := 1 TO n DO
   BEGIN
      {summing formulas for weight and milk yield}
   END{FOR};
   {Calculate Averages}
   {write output}
END{PrintAverages}

BEGIN{Main}
   LoadHerd;
   FOR i := black to white DO
   Print Averages (Herd, Counter[i],i);
END. {Main}

Well there it is.  Probably pretty bad.  I don't know if I am using the right variables or arrays, if I,m calling the functions and procedures right and if I am using the right assigment statements.  Also I'm now lost in the Print averages because what variables and formulas do I use.  Your help would be greatly appreciated as I'm trying really hard to understand all of this and since I am doing this on my own I have noone to explain any of it.  I understand most of the basics but I get lost when I try to apply it.  Thanks Shel
     
0
Comment
Question by:shelly120497
  • 5
  • 3
8 Comments
 
LVL 6

Accepted Solution

by:
tecbuilder earned 250 total points
Comment Utility
I went through the code and fixed a lot of syntax errors.  Try it and see how it works.

PROGRAM CattleRanchSummary (input, InFile, output);
  {***********************************************************
    Input (file): Color, weight and milk information.
    Purpose: To see if their are noticeable
      differences in average weight and milk
      yields in the different color groups of
      cows.
    Output: The number of cows in each color group,
      their average weight and average milk
      yield.
  ***********************************************************}

  CONST
  MinWeight = 300.0;  MaxWeight = 799.9; {to validate weight}
  MinMilk = 0.0;      MaxMilk = 399.9;   {to validate milk/m}
  MaxNoOfCowsPerGroup = 50;
  Hypens = ‘-----------------------------‘;
    {to separate lines of output}
  DiskNameOfInputFile = ‘CATTLE.DAT’;
  TYPE
  ColorType =   (black, red, white); {to be neat and index
      the Counter array and the first dimension of Herd by color }
  WeightMilkType =   array [1..2] of real;
      {pair of real numbers to store the weight and milk data of one cow }
  GroupType =   array [1..MaxNoOfCowsPerGroup ] of WeightMilkType;
         {one WeiMilType pair for each of a possible maximum of
           MaxNoOfCowsPerGroup cows in a color group }
  HerdType =   array [ColorType] of GroupType;
      {one GroupType array for each of the three color groups }
  CounterType =  array [ColorType] of integer;
      {three integers to count the actual number of cows in each color group}
  string19 =   string[19];  {this type of variable will hold
      the error messages passed from the input validating routine}
  string5 =   string[5];  {for ColorOut}

  CONST
  ColorOut:  array[ColorType] of string5 = (‘Black’, ‘Red’, 'White’);
         {comes handy when labelling the output results}
  ColorCode:  array [ColorType] of char = (‘B’,’R’,’W’);
         {to validate color-code and to convert code to a ColorType value}
  VAR
  Herd:   HerdType;   {3-dim array for weight and milk data of the herd}
  Counter:  CounterType;  {a counter of the cows for each color group}
  InFile: Text;  {file variable; internal name of input file}
  i:  ColorType  {index for Counter array and first-dim of Herd}
  {**********************************************************}

  PROCEDURE LoadHerd (var InFile: InFileType;{input file of cattle.data}
   var herd: HerdType; {array to hold weight/milk data}
   var counter: CounterType); {# of cows in each of 3 groups}
  {
    imports:  input file variable, InFile
    input:    color and a WeightMilkType pair for each cow
      in the herd; from InFile it handles all input
      tasks, including assigning and resetting
      InFile.  It reads in and validates data for
      each cow until the end of InFile is reached.
      Valid data is stored in the 3-dimensional array
      Herd.  The first dimension segregates the cows
      by color, the second dimension distinguishes
      individual cows within a color group, and the
      third dimension discerns weight and monthly
      yield of milk.  The cows in each color group
      are counted by array Counter.  If invalid input
      data is detected, then it displays an
      appropriate error message, and ignores the
      invalid data line.
    exports: InFile (position pointer at eof mark)
    Herd, filled with weight/milk data
    Counter, giving the # of cows for each color
      group
  output:  if invalid data is detected, then one of three
      types of error messages is sent to the screen }

  {**********************************************************}

  FUNCTION Convert ( c : char ) : ColorType;
  {
    imports: a valid color code, ‘B’, ‘R’, ‘W’
    task:    convert the code into the pertinent ColorType value, black, red, or white
    exports: the function value, ColorType value pertaining to ColorCode }

  BEGIN
    if UpCase(Ch) = 'B' then Convert := 'Black'
    else if UpCase(Ch) = 'R' then Convert := 'Red'
    else if UpCase(Ch) 'W' then Convert := 'White';
  END {Convert}
  {**********************************************************}

  PROCEDURE Validate ( color:char; {value parameter for color code}
      WeiMil: WeightMilkType; {value parameter for weight & milk}
      VAR Weight, Milk: integer;
      ERROR: string19);  {exports an error message or "}
  {
  imports:  data of a cow, in color and WeiMil
  task:     validates color, weight, and milk.  If all are valid, then
    ERROR exports the null string (ERROR := "); Otherwise, ERROR exports
    one of the following error messages: ‘invalid color code’
    ‘out of range milk/m’ ‘out of range weight’
  exports:  an error message if input error is detected; the
    null string otherwise.  }
BEGIN
  Error := ";
  IF (Color <> Black) AND (Color <> Red) AND (Color <> White) THEN
    BEGIN Error := 'Invalid Color Code'; exit; END
  IF (Weight < MinWeight) OR (Weight > MaxWeight) THEN
    BEGIN Error := 'Out of Range Weight'; exit; END;
  IF (Milk < MinMilk) OR (Milk > MaxMilk) THEN
    BEGIN Error := 'Out of Range Milk/M'; exit; END;
END{Validate};

  {**********************************************************}

VAR
  CowColor:char;
  j:=integer;

BEGIN{LoadHerd}
  Assign (InFile, 'CATTLE.DAT');
  Reset (InFile);
  FOR CowColor := black to white DO
    Counter := 0
  WHILE NOT EOF(InFile) DO BEGIN
    read(InFile, CowColor);
    FOR j:= 1 TO 2 DO
      read(InFile, WeightMilk[j];
    readln(InFile);
    Validate (CowColor, WeightMilk, Error);
    IF Error <> '' THEN writeln(Error)
    ELSE CowColor := Convert(CowColorCode)
    IF (Color = 'B') THEN
    begin
      Counter[black]= Counter[black]+1;
      Herd[black, Counter[black] := WeightMilk;
    end
    ELSE IF Color = 'R' THEN
     begin
       Counter[red]= Counter[red]+1;
       Herd[red, Counter[red] := WeightMilk;
     end
     ELSE IF Color = 'W' THEN
     begin
       Counter[white]= Counter[white]+1;
       Herd[white, Counter[white] := WeightMilk;
     END{IF};
  end;
END;{LoadHerd}

  {**********************************************************}

PROCEDURE PrintAverages ( cows: GroupType; n integer ; C: ColorType);
VAR Number:integer; {for loop control}
  {
  imports:  weight and milk/m data of a group; and number of cows in the group
  task:     computes average weight and milk\m, and produces five lines of display:
            a line of hyphens
            a line reporting the number of cows
            a line reporting the average weight of the cows
            a line reporting the average monthly yeild of milk
            a line of hypens.  }
VAR
  SumWeight, AvgWeight, SumMilk, AvgMilk: real;

BEGIN
  SumWeight := 0;
  AvgWeight := 0;
  SumMilk := 0;
  AvgMilk := 0;
  FOR Number := 1 TO n DO
  BEGIN
    {summing formulas for weight and milk yield}
  END;{FOR}
  {Calculate Averages}
  {write output}
END;{PrintAverages}

BEGIN{Main}
   LoadHerd;
   FOR i := black to white DO
     Print Averages (Herd, Counter[i],i);
END. {Main}
0
 
LVL 6

Expert Comment

by:tecbuilder
Comment Utility
Use this code instead.  When I was able to use my compiler I found a lot of bugs.  The code is bug free and will compile for you.  You will notice some changes from what you had to what I have.  Some of these were done due to bugs and others were done because I could see no other reason for doing it the way you had.  What compiler and version are you using, eg. Borland 5.0?

PROGRAM CattleRanchSummary (input, InFile, output);
{***********************************************************
  Input (file): Color, weight and milk information.
  Purpose: To see if their are noticeable differences in
  average weight and milk yields in the different color
  groups of cows.
  Output: The number of cows in each color group, their
  average weight and average milk yield.
***********************************************************}

CONST
  MinWeight = 300.0; MaxWeight = 799.9; {to validate weight}
  MinMilk = 0.0; MaxMilk = 399.9; {to validate milk/m}
  MaxNoOfCowsPerGroup = 50;
  Hypens = '-----------------------------';
  {to separate lines of output}
  DiskNameOfInputFile = 'CATTLE.DATA';
TYPE
  ColorType = (black, red, white); {to be neat and index the Counter array and the first dimension of Herd by color }
  WeightMilkType = array [1..2] of real;
  {pair of real numbers to store the weight and milk data of one cow }
  GroupType = array [1..MaxNoOfCowsPerGroup ] of WeightMilkType;
  {one WeiMilType pair for each of a possible maximum of MaxNoOfCowsPerGroup cows in a color group }
  HerdType = array [ColorType] of GroupType;
  {one GroupType array for each of the three color groups }
  CounterType = array [ColorType] of integer;
  {three integers to count the actual number of cows in each color group}
  string19 = string[19]; {this type of variable will hold
  the error messages passed from the input validating routine}
  string5 = string[5]; {for ColorOut}

CONST
  ColorOut: array[ColorType] of string5 = ('Black', 'Red', 'White');
  {comes handy when labelling the output results}
  ColorCode: array [ColorType] of char = ('B','R','W');
  {to validate color-code and to convert code to a ColorType value}
VAR
  Herd: HerdType; {3-dim array for weight and milk data of the herd}
  Counter: CounterType; {a counter of the cows for each color group}
  InFile: Text; {file variable; internal name of input file}
  i: ColorType; {index for Counter array and first-dim of Herd}
  WeightMilk: WeightMilkType;
{**********************************************************}

PROCEDURE LoadHerd;
{
  imports: input file variable, InFile
  input: color and a WeightMilkType pair for each cow
  in the herd; from InFile it handles all input
  tasks, including assigning and resetting
  InFile. It reads in and validates data for
  each cow until the end of InFile is reached.
  Valid data is stored in the 3-dimensional array
  Herd. The first dimension segregates the cows
  by color, the second dimension distinguishes
  individual cows within a color group, and the
  third dimension discerns weight and monthly
  yield of milk. The cows in each color group
  are counted by array Counter. If invalid input
  data is detected, then it displays an
  appropriate error message, and ignores the
  invalid data line.
  exports: InFile (position pointer at eof mark)
  Herd, filled with weight/milk data
  Counter, giving the # of cows for each color
  group
output: if invalid data is detected, then one of three
  types of error messages is sent to the screen }

{**********************************************************}

FUNCTION Convert ( c : char ) : ColorType;
{
  imports: a valid color code, 'B', 'R', 'W'
  task: convert the code into the pertinent ColorType value, black, red, or white
  exports: the function value, ColorType value pertaining to ColorCode }
BEGIN
  if UpCase(c) = 'B' then Convert := Black
  else if UpCase(c) = 'R' then Convert := Red
  else if UpCase(c) = 'W' then Convert := White;
END; {Convert}
{**********************************************************}

PROCEDURE Validate ( VAR Weight, Milk: real;
  ERROR: string19); {exports an error message or "}
{
  imports: data of a cow, in color and WeiMil
  task: validates color, weight, and milk. If all are valid, then
  ERROR exports the null string (ERROR := "); Otherwise, ERROR exports
  one of the following error messages: 'invalid color code'
  'out of range milk/m' 'out of range weight'
  exports: an error message if input error is detected; the
  null string otherwise. }
BEGIN
  Error := '';
  IF (Weight < MinWeight) OR (Weight > MaxWeight) THEN
    BEGIN Error := 'Out of Range Weight'; exit; END;
  IF (Milk < MinMilk) OR (Milk > MaxMilk) THEN
    BEGIN Error := 'Out of Range Milk/M'; exit; END;
END{Validate};

{**********************************************************}

VAR
  CowColor: ColorType;
  j: integer;
  CowColorCode: char;
  Error: string;

BEGIN{LoadHerd}
  Assign (InFile, 'CATTLE.DAT');
  Reset (InFile);
  FOR CowColor := black to white DO
    Counter[CowColor] := 0;
  WHILE NOT EOF(InFile) DO BEGIN
    read(InFile, CowColorCode);
    FOR j:= 1 TO 2 DO
      read(InFile, WeightMilk[j]);
    readln(InFile);
    if CowColorCode in ['B','R','W']
    then Convert(CowColorCode)
    else Error := 'Invalid color code.';

    IF Error <> '' THEN begin writeln(Error); exit; end;

    Validate (WeightMilk[1], WeightMilk[2], Error);
    CowColor := Convert(CowColorCode);

    IF (CowColorCode = 'B') THEN
    begin
      Counter[black] := Counter[black]+1;
      Herd[black, Counter[black]] := WeightMilk;
    end
    ELSE IF CowColorCode = 'R' THEN
    begin
      Counter[red] := Counter[red]+1;
      Herd[red, Counter[red]] := WeightMilk;
    end
    ELSE IF CowColorCode = 'W' THEN
    begin
      Counter[white] := Counter[white]+1;
      Herd[white, Counter[white]] := WeightMilk;
    END{IF};
  end;
END;{LoadHerd}

{**********************************************************}

PROCEDURE PrintAverages ( cows: GroupType; n: integer ; C: ColorType);
VAR Number:integer; {for loop control}
{
imports: weight and milk/m data of a group; and number of cows in the group
task: computes average weight and milk\m, and produces five lines of display:
  a line of hyphens
  a line reporting the number of cows
  a line reporting the average weight of the cows
  a line reporting the average monthly yeild of milk
  a line of hypens. }
VAR
  SumWeight, AvgWeight, SumMilk, AvgMilk: real;

BEGIN
  SumWeight := 0;
  AvgWeight := 0;
  SumMilk := 0;
  AvgMilk := 0;
  FOR Number := 1 TO n DO
    BEGIN
     {summing formulas for weight and milk yield}
    END;{FOR}
  {Calculate Averages}
  {write output}
END;{PrintAverages}

BEGIN{Main}
  LoadHerd;
  FOR i := black to white DO
{    Print Averages (Herd, Counter[i],i);}
END. {Main}
0
 

Author Comment

by:shelly120497
Comment Utility
Wow! Thanks a lot.  I am using Borland Turbo Pacal 7.0.  As I said I'm a beginner and not really sure of what I'm doing.  One more thing though.  Could you help me out with the Print Average Procedure.  I'm not quite sure how to get my averages and how to get them displayed properly.  This is what it should look like for each cow color.

-----------------------------------------------------------------
Black Cows
-----------------------------------------------------------------
Number of Cows:                                                 1
Average Weight in Kilograms                                 388.8
Average Monthly Yield of Milk in Litres                      77.7
-----------------------------------------------------------------

What I have trouble with is how to access where the information about each cow is stored.  

Thanks a lot for your help.  If you can help me with this I'll add some more points and regardless, I'll still rate this as excellent as I know it takes time to go through all of this.

Thanks a bunch
Michelle
0
 
LVL 6

Expert Comment

by:tecbuilder
Comment Utility
I made some additional changes to the code to clean things up a bit.  Use the code below for your program.  I created a data set to run and it worked.  Just make sure that your CATTLE.DAT file looks something like:

B 500 20
R 600 80
...

Do not use comma to seperate the data.  Only spaces.

PROGRAM CattleRanchSummary (input, InFile, output);
{***********************************************************
  Input (file): Color, weight and milk information.
  Purpose: To see if their are noticeable differences in
  average weight and milk yields in the different color
  groups of cows.
  Output: The number of cows in each color group, their
  average weight and average milk yield.
***********************************************************}

CONST
  MinWeight = 300.0; MaxWeight = 799.9; {to validate weight}
  MinMilk = 0.0; MaxMilk = 399.9; {to validate milk/m}
  MaxNoOfCowsPerGroup = 50;
  {to separate lines of output}
  DiskNameOfInputFile = 'CATTLE.DATA';
TYPE
  ColorType = (black, red, white); {to be neat and index the Counter array and the first dimension of Herd by color }
  {one WeightMilkRec pair for each of a possible maximum of MaxNoOfCowsPerGroup cows in a color group }
  HerdType = array [ColorType, 1..MaxNoOfCowsPerGroup, 1..2] of real;
  {one GroupType array for each of the three color groups }
  CounterType = array [ColorType] of integer;
  {three integers to count the actual number of cows in each color group}
  string19 = string[19]; {this type of variable will hold
  the error messages passed from the input validating routine}
  string5 = string[5]; {for ColorOut}

CONST
  ColorOut: array[ColorType] of string5 = ('Black', 'Red', 'White');
  {comes handy when labelling the output results}
  ColorCode: array [ColorType] of char = ('B','R','W');
  {to validate color-code and to convert code to a ColorType value}
VAR
  Herd: HerdType; {3-dim array for weight and milk data of the herd}
  Counter: CounterType; {a counter of the cows for each color group}
  Error: string;
{**********************************************************}

PROCEDURE LoadHerd(var Error: string);
{
  imports: input file variable, InFile
  input: color and a WeightMilkType pair for each cow
  in the herd; from InFile it handles all input
  tasks, including assigning and resetting
  InFile. It reads in and validates data for
  each cow until the end of InFile is reached.
  Valid data is stored in the 3-dimensional array
  Herd. The first dimension segregates the cows
  by color, the second dimension distinguishes
  individual cows within a color group, and the
  third dimension discerns weight and monthly
  yield of milk. The cows in each color group
  are counted by array Counter. If invalid input
  data is detected, then it displays an
  appropriate error message, and ignores the
  invalid data line.
  exports: InFile (position pointer at eof mark)
  Herd, filled with weight/milk data
  Counter, giving the # of cows for each color
  group
output: if invalid data is detected, then one of three
  types of error messages is sent to the screen }

{**********************************************************}

FUNCTION Convert ( c : char ) : ColorType;
{
  imports: a valid color code, 'B', 'R', 'W'
  task: convert the code into the pertinent ColorType value, black, red, or white
  exports: the function value, ColorType value pertaining to ColorCode }
BEGIN
  if UpCase(c) = 'B' then Convert := Black
  else if UpCase(c) = 'R' then Convert := Red
  else if UpCase(c) = 'W' then Convert := White;
END; {Convert}
{**********************************************************}

function ValidateWeightMilk (const W, M: real): string;
{ Range checks the weight (W) and milk (M).  If either falls outside of
  range then an validate will indicate which parameter exceeded the range.
  If neither parameter exceed the range then validate will be an empty string.}
BEGIN
  if (W < MinWeight) or (W > MaxWeight) then
    ValidateWeightMilk := 'Weight Out of Range'
  else if (M < MinMilk) or (M > MaxMilk) then
    ValidateWeightMilk := 'Milk Out of Range'
  else ValidateWeightMilk := '';
END{Validate};

{**********************************************************}

VAR
  CowColor: ColorType;
  j: integer;
  CowColorCode: char;
  InFile: Text; {file variable; internal name of input file}
  Weight, Milk: real;

BEGIN{LoadHerd}
  Assign (InFile, 'CATTLE.DAT');
  Reset (InFile);
  { CATTLE.DAT file should have the following format:
    CowColorCode Weight Milk  (Note: No spaces between values);
    Example:
      B 377 15
      B 390 10
      W 300 20
  }

  Error := '';
  FOR CowColor := black to white DO
    Counter[CowColor] := 0;

  WHILE NOT EOF(InFile) DO BEGIN
    read(InFile, CowColorCode);
    if CowColorCode in ['B','R','W']
    then CowColor := Convert(CowColorCode)
    else Error := 'Invalid color code.';
    if Error <> '' then begin writeln(Error); readln; exit; end;

    readln(InFile, Weight, Milk);
    Error := ValidateWeightMilk(Weight, Milk);
    if Error <> '' then begin writeln(Error); readln; exit; end;

    case CowColorCode of
      'B': CowColor := black;
      'R': CowColor := red;
      'W': CowColor := white;
    end;
    Counter[CowColor] := Counter[CowColor]+1;
    Herd[CowColor, Counter[CowColor],1] := Weight;
    Herd[CowColor, Counter[CowColor],2] := Milk;
  end;
END;{LoadHerd}

{**********************************************************}

PROCEDURE PrintAverages;
{
imports: weight and milk/m data of a group; and number of cows in the group
task: computes average weight and milk\m, and produces five lines of display:
  a line of hyphens
  a line reporting the number of cows
  a line reporting the average weight of the cows
  a line reporting the average monthly yeild of milk
  a line of hypens. }
VAR
  SumWeight, AvgWeight, SumMilk, AvgMilk: real;
  i:integer; {for loop control}
  r: ColorType; {index for Counter array and first-dim of Herd}

BEGIN
  for r := black to white do
    begin
      SumWeight := 0;
      SumMilk := 0;
      FOR i := 1 TO Counter[r] DO
        BEGIN
        {summing formulas for weight and milk yield}
          SumWeight := Herd[r,i,1] + SumWeight;
          SumMilk := Herd[r,i,2] + SumMilk;
        END;{FOR i}

      {Calculate Averages}
      AvgWeight := SumWeight / Counter[r];
      AvgMilk := SumMilk / Counter[r];

      {write output}
      Writeln('-----------------------------');
      Writeln(ColorOut[r], ' Cows');
      Writeln('-----------------------------');
      Writeln('Number of Cows: ', Counter[r]);
      Writeln('Average Weight in Kilograms: ', AvgWeight:5:1);
      Writeln('Average Monthly Yield of Milk in Litres: ', AvgMilk:5:1);
      Writeln('-----------------------------');
      if r <> white then Writeln;
      Readln;
    end; {for r}
END;{PrintAverages}

BEGIN{Main}
  LoadHerd(Error);
  if Error = '' then PrintAverages;
END. {Main}
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 6

Expert Comment

by:tecbuilder
Comment Utility
Oh, one other note.  I put a readln statement after the stats for each cow type is printed so you can see each type one at a time.  If you don't want this just remove it.  It is located 8 lines from the bottom of the code.  Also, something I just thought of that you should add is a ClrScr; function within the PrintAverages procedure before the For r := black to white do.  This will clear the screen.  So your code would look like:

ClrScr;
For r := black to white do

Let me know if you have any questions, but I think this will take care of you.  All my code changes are in lowercase since most of your coding used uppercase.
0
 

Author Comment

by:shelly120497
Comment Utility
Thanks a lot.  It is much easier to learn when you can see what and where you went wrong instead of being frustrated and not knowing what is wrong.

Shelly
0
 

Author Comment

by:shelly120497
Comment Utility
To Techbuilder:

I have finished the program and followed your last code but I keep getting errors.  I was wondering if you could just look at my code and see if I'm missing something or have something wrong.  It's a run-time error 106 that I keep coming up with saying it's an invalid data type or something.  Would you be able to just e-mail me and then I can forward you a text copy of the program or a copy of the program itself if you prefer.  

Thanks,
Shelly
0
 
LVL 6

Expert Comment

by:tecbuilder
Comment Utility
Sure, but all I ask is that I receive some points for the effort.

You can send the file to tecbuilder@bigfoot.com
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Find out what Office 365 Transport Rules are, how they work and their limitations managing Office 365 signatures.
A procedure for exporting installed hotfix details of remote computers using powershell
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now