code:

Can't Copy and Paste this?

Click here for a copy-and-paste friendly version of this code!

 

Terms of Agreement:   

By using this code, you agree to the following terms...   

1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.   

2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   

3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 

4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.  

//     

/*

J.R. Martin

baseball.cpp

this program gets integer data from a file in the following repetitive format: 

player#, hits, walks, outs.

The program declares arrays for each, calculates batting average based on that data,

then prints out the player number, batting average and total walks per player.

The user has to make a file with twenty players in the order declared above (make 

sure there is whitespace between each entry!)

*/

#include 

#include 

#include 

#include 

using namespace std;

void OpenFile ( ifstream&, string );

void ZeroArray ( int[], int );

void FillArrays ( ifstream&, int[], int[], int[]);

void GetAve ( int[], int[], float[], int);

void Print ( int[], float[], int[]);

const int col = 20;

int main()

    {

     ifstream in; // InFile stream

     string FileName;

     string exit;

     cout << "Please enter the name of the file you made to calculate the averages: " ;

     cin >> FileName;

     OpenFile (in , FileName );

     int i = 0;

     int person[col], 

     hit[col], 

     walk[col],

     out[col];

     float ave[col];

     ZeroArray(person,col); 

     ZeroArray(hit,col);

     ZeroArray(walk,col);

     ZeroArray(out,col);

     for ( i=0; i<20; i++ ) 

     person[i]=i+1;

     FillArrays (in, hit, walk, out); // Get the data and fill arrays.

     GetAve(hit, out, ave, col); // Calculate batting averages.

     Print(person, ave, walk); // Output the results

     cout << "Please type \"exit\" to kill program: ";

     cin >> exit;

    return 0;

}

void OpenFile ( ifstream& SomeInFile, string FileName )

    {

     SomeInFile.open(FileName.c_str());

     if ( !SomeInFile )

     {

     cout << "\n\nEither your FileName is incorrect, or the file is not \a\n" <<

     "in the same directory as this program.\n\n"; 

     exit (1); 

     }

}

//end OpenFile()

// *************************************

//     

void ZeroArray ( int a[] , int c )

    {

    for (int i=0; i

    a[i]=0;

}

// Set array[i] to zero.

//**************************************

//     *

void FillArrays ( ifstream& in, int hit[], int walk[], int out[] )

    {

     int p, h, w, o;

     in >> p >> h >> w >> o;

     while (in)

     {

     hit[p-1] += h;

     walk[p-1] += w ;

     out[p-1] += o;

     in >> p >> h >> w >> o;

     }

}

//**************************************

//     ** 

void GetAve ( int h[], int o[], float a[], int c)

    {

     for (int i=0; i

     a[i]= (float) h[i] / float((float)h[i] + (float)o[i]);

}

//**************************************

//     **

void Print (int p[], float a[], int w[])

    {

     cout << "Player Number Batting Ave Walks\n\n\n"; 

    

     for (int i = 0; i < 20; i++) 

         {

         if (i<9)

         cout << " " << p[i] << "" << setprecision(3) << a[i] 

         << " " << w[i] << endl;

         else

         cout << " " << p[i] << "" << setprecision(3) << a[i] 

         << " " << w[i] << endl;

         }

    }

    /*

    The output from this program is as follows:

    Player Number Batting Ave Walks

    10.678 38

    20.597 48

    30.524 46

    40.595 41

    50.474 64

    60.491 49

    70.594 43

    80.624 44

    90.423 31

    100.515 29

    110.564 49

    120.418 36

    130.489 48

    140.589 43

    150.495 27

    160.624 59

    170.533 58

    180.466 32

    190.515 61

    200.692 24

    Press any key to continue

    using this file as an input.

    2 9 0 2

    10 8 0 2

    3 1 5 2

    2 7 1 1

    16 3 1 4

    12 2 1 3

    13 1 5 4

    19 2 0 9

    12 3 1 2

    8 4 0 3

    4 2 2 5

    14 5 1 2

    14 7 2 1

    3 3 1 5

    4 7 0 2

    17 3 2 6

    9 0 2 8

    5 6 1 3

    11 7 0 2

    7 3 2 4

    10 5 4 2

    17 6 0 2

    12 4 3 3

    7 0 6 1

    19 3 2 1

    14 8 0 3

    5 3 2 1

    14 5 0 1

    5 0 9 1

    18 3 0 6

    7 5 0 1

    16 5 4 2

    12 0 0 10

    2 6 2 2

    8 0 7 4

    18 0 10 1

    6 1 8 2

    2 2 2 2

    7 3 6 1

    1 7 3 1

    11 3 1 7

    9 1 1 7

    3 3 1 4

    15 6 1 3

    9 3 4 2

    2 5 0 1

    19 4 0 4

    8 10 0 1

    9 6 0 1

    11 10 0 1

    10 7 0 1

    1 8 2 1

    19 5 0 4

    5 7 2 2

    16 6 0 1

    4 1 7 1

    9 0 0 6

    1 6 1 1

    10 1 4 3

    13 4 0 2

    19 7 1 3

    19 4 5 2

    17 2 2 5

    13 6 2 1

    3 3 3 2

    10 0 0 6

    2 2 3 1

    2 4 0 7

    13 4 2 5

    1 3 4 1

    2 3 0 3

    16 4 1 2

    3 5 0 2

    18 4 2 3

    10 5 0 1

    17 1 4 4

    6 4 1 2

    2 1 0 7

    19 2 5 2

    20 7 1 2

    10 0 6 1

    14 8 1 2

    7 2 2 3

    16 0 1 5

    17 4 2 1

    14 0 5 2

    13 0 1 7

    14 1 3 2

    7 1 4 5

    17 1 5 2

    16 5 2 1

    5 2 6 3

    14 2 2 4

    6 1 1 7

    15 0 2 6

    8 6 1 4

    3 3 2 2

    15 0 2 4

    13 8 2 1

    8 0 1 6

    14 3 3 2

    13 0 10 1

    14 0 4 3

    8 4 4 1

    4 5 0 3

    4 3 2 1

    6 0 10 1

    3 0 4 4

    8 5 2 1

    11 5 3 2

    12 0 3 5

    6 0 4 6

    16 5 0 1

    17 8 0 2

    12 6 1 1

    12 2 0 6

    20 0 7 3

    1 4 1 1

    11 6 2 3

    4 0 3 3

    3 5 2 3

    18 1 0 7

    10 7 0 1

    3 3 5 2

    18 3 1 3

    5 2 4 4

    11 1 0 9

    12 0 4 7

    11 4 3 1

    1 0 4 3

    1 2 3 2

    6 4 3 4

    19 0 6 1

    18 5 2 1

    14 5 0 2

    1 3 0 3

    6 1 4 1

    16 2 6 2

    4 4 2 1

    7 3 1 5

    18 1 1 7

    17 1 4 3

    18 5 0 1

    4 1 5 3

    20 6 3 1

    14 2 2 2

    15 7 2 1

    13 0 8 3

    12 1 0 6

    16 7 0 1

    15 5 1 1

    20 9 0 2

    7 5 1 2

    11 9 0 1

    14 5 2 1

    17 5 1 1

    3 5 0 1

    14 1 7 1

    3 3 2 3

    5 0 3 5

    17 2 3 2

    16 2 4 2

    18 2 7 1

    10 9 0 2

    6 7 0 3

    11 5 0 2

    12 4 2 4

    16 5 3 3

    5 6 0 1

    16 2 5 3

    10 1 2 3

    8 2 6 1

    14 1 1 6

    16 2 2 3

    5 1 7 1

    11 7 1 2

    16 0 4 2

    8 6 2 1

    2 1 5 1

    11 0 5 2

    5 5 0 1

    12 2 4 2

    11 3 3 5

    4 8 1 1

    5 1 8 1

    14 7 0 4

    7 5 0 1

    20 8 2 1

    6 1 3 2

    19 1 3 2

    13 2 7 1

    11 3 3 3

    4 6 4 1

    16 1 7 1

    8 3 1 2

    10 0 1 8

    9 0 4 4

    2 2 0 4

    7 5 0 1

    11 4 1 1

    17 6 1 4

    6 1 4 1

    5 3 6 1

    18 4 3 3

    19 3 0 5

    16 10 0 1

    5 1 2 8

    16 5 0 4

    9 6 2 2

    14 4 0 5

    7 5 3 2

    6 6 1 3

    15 0 4 2

    17 2 3 5

    16 5 1 3

    6 3 2 2

    11 4 0 2

    14 5 0 4

    9 1 1 6

    4 7 0 1

    9 5 0 1

    10 0 0 10

    19 0 5 5

    2 5 0 1

    4 8 0 3

    10 4 1 1

    15 2 3 1

    12 0 1 6

    9 2 6 3

    6 2 2 3

    6 10 0 1

    3 7 2 1

    2 4 1 1

    20 3 1 3

    18 4 0 5

    12 5 0 3

    16 6 0 3

    13 2 4 2

    6 4 0 6

    1 2 1 4

    16 5 2 4

    14 1 2 5

    17 4 3 2

    2 3 6 1

    9 2 0 6

    12 1 3 3

    14 1 3 3

    9 7 1 3

    11 3 1 2

    7 4 1 3

    8 9 0 1

    2 4 5 2

    17 4 1 1

    2 4 2 2

    2 1 7 2

    10 1 1 7

    5 6 0 1

    4 0 1 8

    19 7 1 2

    2 6 2 2

    15 3 0 4

    11 10 0 1

    15 1 2 4

    12 3 4 1

    20 2 4 2

    16 5 0 3

    19 0 9 2

    20 6 0 2

    3 3 6 2

    7 5 1 2

    8 2 7 1

    19 4 0 4

    19 1 0 8

    3 1 0 5

    3 7 1 1

    19 5 1 1

    17 1 6 1

    19 6 0 1

    1 7 1 1

    16 5 0 1

    19 4 0 6

    9 7 1 1

    16 4 6 1

    16 2 0 6

    6 5 0 3

    2 5 0 3

    11 0 5 4

    6 1 5 2

    18 3 1 2

    15 1 4 1

    16 8 0 1

    18 6 0 1

    9 1 2 7

    13 1 4 2

    17 4 0 2

    10 2 3 1

    4 2 4 1

    20 7 1 1

    17 5 4 1

    5 3 2 5

    10 3 0 3

    10 5 0 4

    19 6 1 2

    11 0 1 5

    11 6 1 1

    16 4 2 4

    3 1 0 6

    13 5 1 1

    18 0 0 9

    15 5 0 1

    2 2 7 1

    9 2 3 3

    9 5 3 3

    20 4 2 4

    20 5 1 4

    7 7 1 2

    8 4 2 2

    8 1 2 8

    12 5 0 1

    4 4 0 2

    1 3 0 4

    11 4 1 4

    18 6 1 2

    18 0 1 5

    17 0 6 1

    7 1 2 4

    4 1 5 5

    9 7 1 1

    4 3 2 2

    9 0 0 11

    13 5 0 3

    6 3 1 7

    15 4 2 2

    2 1 3 4

    11 0 1 5

    11 5 3 1

    20 6 2 3

    3 6 0 2

    7 4 2 1

    12 5 2 1

    4 2 2 6

    13 2 1 3

    19 6 0 5

    19 5 2 2

    15 8 1 1

    4 7 0 1

    10 2 0 5

    7 3 2 1

    11 0 4 4

    14 6 1 1

    11 0 3 4

    3 0 1 7

    5 0 4 4

    8 7 0 1

    5 0 6 5

    16 0 8 1

    13 1 1 4

    15 1 2 4

    15 5 0 1

    1 1 4 1

    1 8 1 2

    4 4 1 1

    13 3 0 6

    1 4 4 1

    1 0 1 6

    11 4 0 3

    12 3 1 3

    1 10 0 1

    11 0 5 1

    19 5 5 1

    5 0 2 4

    8 5 2 1

    11 3 2 4

    3 0 7 4

    17 3 3 2

    17 0 5 1

    15 0 1 8

    1 3 7 1

    17 0 2 4

    8 0 7 3

    7 1 2 3

    10 6 0 1

    2 2 1 4

    19 3 3 2

    18 3 2 1

    2 4 1 2

    14 3 1 3

    14 4 1 1

    10 0 7 1

    12 3 4 2

    12 2 2 2

    18 4 1 4

    15 3 0 8

    7 1 7 1

    10 4 0 3

    17 2 1 4

    3 6 4 1

    1 7 1 3

    14 5 2 2

    19 1 6 3

    */