Program: Beginner Needs Help

I'm a student who really needs help.  I am trying to write a program where a cattle rancher will create a file including data about his three groups of cow.  The data includes the color of the cow - black, red, or white; the weight of each cow and the yield of milk the cow produces.  What he wants from this program is the total # of cows in each color group, their average weight and their average monthly yield of milk.  This program must use arrays and procedures and functions.  Also the data is read from a file and an eof function is required.  A set of constants, variables, types, functions and procedures is supplied and must be used.  The output should be to screen in a format similiar to the following:
Black Cows                                                10
Average weight in kilograms                            842.3
Average monthly milk yield                             333.3
for each group.  I just do not understand how the arrays work and what they do really.  The following is a list of the required constansts, types, variables, procedures and functions:
 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’;
 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 fi
 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}
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}

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 }
  PROCEDURE LoadHerd (var InFile: InFileType; {input file of}
                                             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
  task:        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
PROCEDURE Validate ( color:char;     {value parameter for color code}
                                         WeiMil:WeightMilkType; {value parameter for weight & milk}
                                          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.  }

PROCEDURE PrintAverages ( cows: GroupType; n integer );
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.  }

Thanks a lot, Shelly
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Do you have a specific question, or just a basic explanation of what needs to be done?

Obviously I cannot write the whole program for you but I can try to explain the arrays.

The arrays are there to structure your data. The final array is of herdtype.

To understand what this means we looka at herdtype. This is and array [colortype] of grouptype. The array has three elements (red, black and white - the typed constants used to note the colour of the cow). each element itself contains an array [1..50] of weightmilktype. So if you like cow no 25 is black and is the 25th cow of this type - her weight is stored in herd[black, 25 {cow number}, 1 {the first real number in the weightmilktype array holds the weight of the cow].

To get to her milk yield you would access herd [black, 25, 2].

The second array variable is the counter. This keeps track of the number of cows in each color group. The import should read as follows - fead the data from the file - check colour group, use a case to increase the appropriate colour counter (if the cow is red then inc (counter[red]); You can then use the value stored in the counter array as the pointer to the enter your new cow's data into the herd array - herd[cow colour, counter [cow colour], 1] := value - to enter the cows weight.
shelly120497Author Commented:
I now understand how the arrays work but how do I incorporate them in with the procedures and then into the program.  I really need a basic explanation as to how to put it all together.  How do I start the program out and which variables do I use with what commands.

Thanks Michelle
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

I Could give u the whole source but i need some answers first..

1: Do u REALY have to have thoose const and types?.

2: If the answer to question nr 1 is YES then could i atleast
   use a better filetype?.. (OLD: InFileType = text
                             NEW: InFileType = File Of HerdType)
shelly120497Author Commented:
Yes, I pretty well need to use those const and types but I can change them a little bit.  I really would appreciate it if you could give me the whole source.

shelly120497Author Commented:
Adjusted points to 100
I will help you out, if i understand it correctly you are a student who must code something in borland pascal. But you have no (or almost no) experience in this language. If you need help you can mail me (

Regards, Arijan

I've worked out the source code already, and it works, but I'm a little reluctant to provide the whole source if this is a class project. I generally don't believe in doing homework for someone, especially a somewhat lengthy assignment like this one. I would rather provide psuedocode or work through the problem gradually so you will retain the concepts better and learn to program on your own.

When is the project due?  What percent of your grade will it be?  Give me a good reason for me to provide you with the source code and I'll consider it.

Start the program with the CONST, TYPE and VAR declaration sections.  Most of this work seems already present in the code included in the description of the problem you provided in your original question.  Then work on each of the procedures.  Make sure you define each procedure or function before it is called, for instance, you must define the procedure VALIDATE either nested within the procedure LOADHERD or before the procedure LOADHERD.

To transfer arrays to or from procedures, you must declare a value or variable parameter in the procedure header using the TYPE for the array defined in the program TYPE declaration section. Value parameters only transfer values into a procedure, whereas variable parameters allow the data stored in the array to be passed to and/or from the procedure.  Again, much of this code appears to be present in your original question.  (For example: the code

PROCEDURE LoadHerd (var InFile: InFileType; {input file of
                    var herd: HerdType; {array to hold weight/                                          milk data}
                    var counter: CounterType); {# of cows in
                                                each of 3 groups}

allows the values read into the array Herd and stored in the array Counter during the procedure LoadHerd to be transferred back to the main program so the information can be used in the body of the program and the procedure PrintAverages. The arrays must be defined in the TYPE section of the main program so that the defined types can be used to declare the variable parameters in procedure headers since it is illegal to define arrays within the procedure header itself.

You can use a while loop to read the data from the file into the array herd.  I recommend using the seekeof function instead of the eof because it is more robust, if your compiler supports using seekeof.  For example, for a portion of  procedure LoadHerd, write:

     while not seekeof (InFile) do
         read (InFile, cowcolor);
           for j := 1 to 2 do
             read (InFile, WeightMilk[j]);
           readln (InFile);
         VALIDATE (cowcolor, WeightMilk, ERROR); {calls the                                                 procedure VALIDATE}
         {if statement to test if a string was returned by
          the validate procedure and either write that statement
          to the screen or add the current line/record of input
          into the array.  The then statement writes the error
          message to the screen, the else statement calls the
          function CONVERT using the statement
                CowColor := Convert (CowColorCode);
          and adds 1 to the appropriate Counter for the color of           cow read in.  A for loop assigns the weight and milk
          yeild to the Herd array.}
       end; {while}
Where cowcolor is a local variable of type char, WeightMilk is a local array of type WeightMilkType, and j is a local variable of type integer.

I nested the function CONVERT and the procedure VALIDATE inside the procedure LOADHERD.  The body of the function CONVERT is a simple for loop based on a ColorType control variable and uses the constant ColorCode as defined in the information you provided.

Here is the psuedocode:

program CattleHerdSummary;

CONST {declarations for constants as you provided}

TYPE {declaration of types you provided}

CONST {declaration of typed constants you provided}

VAR {declaration of variables Herd (3-dim array, HerdType), Counter (array, CounterType), InFile (InFileType), i (ColorType)}

PROCEDURE LoadHerd ({declarations as you provided});

  VAR {declare local variables for the color code (one char and   one ColorType), weight and milk yield (array, WeightMilkType),   j (for loop control variable, integer), and ERROR (string19)}

    FUNCTION Convert (c: char): ColorType;

    VAR {a control variable (cci) of ColorType to control the for          loop}

        for cci := black to white do
          {if statement and formula to convert from color input            from char type to ColorType variable}
      end; {Convert}

    PROCEDURE Validate ({declarations as you provided});
      VAR {declare a for loop control variable of ColorType}
          {declare a boolean variable for validity check}

        {initialize boolean variable as false}
        for cci := black to white do
          {linear nested if statements using constants defining            the valid ranges for color, weight and milk yield,            these nested if statements are used to assign the            appropriate error message string to the variable            ERROR}
      end; {Validate}
  begin {LoadHerd}
    {assign statement}
    {reset statement}
    for CowColor := black to white do
      {initialize Counter array to zero};
    while not seekeof (InFile) do
      {see code I provided above}
    close (InFile);
  end; {LoadHerd}

PROCEDURE PrintAverages (cows: HerdType; n: integer;
                         c: ColorType);
  VAR {for loop control integer variable}
      {declarations for SumWeight, AvgWeight, SumMilk, AvgMilk}

    {initialize summing variables to zero}
    for numb := 1 to n do
        {summing formulas for weight and milk yield}
      end; {for}
    {calculate averages}
    {write output}
  end; {PrintAverages}

begin {main}
  LoadHerd ({variables used to transfer values to and from              procedure and main body});
  for i := black to white do
    PrintAverages (Herd, Counter[i], i)
end. {main}

I hope this helps.

Good Luck!


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hey, why dont you do your own homework!
shelly120497Author Commented:
Sorry I took so long to get back to you but thanks for your help.  It helped me out a lot.

And to weinrj:
If I understood Pascal I wouldn't need to ask.  I wasn't asking anyone to do my homework for me, I just needed help in understanding what I was supposed to do because unfortunately we are not all as smart or as perfect as you.  
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.