Solved

Program: Beginner Needs Help

Posted on 1997-12-04
11
199 Views
Last Modified: 2010-04-16
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
0
Comment
Question by:shelly120497
11 Comments
 
LVL 2

Expert Comment

by:mitchell042997
Comment Utility
Do you have a specific question, or just a basic explanation of what needs to be done?
0
 

Expert Comment

by:gnome
Comment Utility
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.
0
 

Author Comment

by:shelly120497
Comment Utility
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
0
 

Expert Comment

by:s_mind
Comment Utility
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)
0
 

Author Comment

by:shelly120497
Comment Utility
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
0
Free Trending Threat Insights Every Day

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.

 

Author Comment

by:shelly120497
Comment Utility
Adjusted points to 100
0
 
LVL 1

Expert Comment

by:aluiken
Comment Utility
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
0
 
LVL 1

Expert Comment

by:zircon
Comment Utility
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.


0
 
LVL 1

Accepted Solution

by:
zircon earned 100 total points
Comment Utility
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}

      begin
        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}

      begin
        {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}

  begin
    {initialize summing variables to zero}
    for numb := 1 to n do
      begin
        {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!

Bruce
0
 

Expert Comment

by:weinrj
Comment Utility
hey, why dont you do your own homework!
0
 

Author Comment

by:shelly120497
Comment Utility
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.  
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
Scam emails are a huge burden for many businesses. Spotting one is not always easy. Follow our tips to identify if an email you receive is a scam.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 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

8 Experts available now in Live!

Get 1:1 Help Now