Link to home
Start Free TrialLog in
Avatar of shelly120497
shelly120497

asked on

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:
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 fi
       
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}


{************************************************************************}
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 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
  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
Avatar of mitchell042997
mitchell042997

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

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.
Avatar of shelly120497

ASKER

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
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)
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.

Thanks
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 (aluiken@Amgen.com)

Regards, Arijan
Shelly,

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
                                             cattle.dat}
                    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
       begin
         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.


ASKER CERTIFIED SOLUTION
Avatar of zircon
zircon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hey, why dont you do your own homework!
Bruce:
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.