CISC 5350 Financial Programming (Spring 2011)

Programming Assignment 4 (due March 2)

(Submit code and output to Blackboard under “Lab Assignments”)

(1) Payroll Program (File Handling Phase:

Retrieving Data from Sequential File)

Enhance your payroll program so that it reads in employee data from a file.

Create a text file named, for example: employees.txt and enter sample data for all of your inputs (id, first name, last name, hours worked, hourly rate, and status). Each employee will be entered as one row and data fields will be in columns (separated by space or tab).

Create an input function that reads the data from your text file and stores it into your arrays. The necessary arrays can be sent as parameters. The function must also send back the total number of employees - this can be done by either returning a value, or by sending a counter variable to the function by reference.

You will need the following to include file handling into your program.

(a) Include the file stream header file: #include <fstream>

using namespace std;

(b) Associate an input file name with an input file stream for accessing the data file.

ifstream fin(“employees.txt”);

(c) Use the associated input file stream to input the data:

int i=0;

while(finid[i]fname[i] >lname[i] >hours[i] >rate[i] > status[i])

{ ..…

..…

…..

i++;

}//while loop to read in data from file into arrays

This while loop will retrieve data until end of file (EOF) is reached.

(d) Close the file, indicating that the data access is terminated.

fin.close( );

(2)(a) Sorting a Data File

The Bubble Sort algorithm sorts values that are stored in an array. In this program, we will input integers from a sequential file of unsorted, random numbers, store the numbers in an array, apply bubble sort, and output the sorted integers to a new file. Generate a file of 500 random numbers in the range of 1 to 500 (see examples of rand() function posted), and call this file "unsorted_Random.txt". Output the sorted values into a file called "sorted_Random.txt".

Bubble Sort sample code to modify:

const int arraySize = 10; // size of array a

int a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };

int hold; // temporary location used to swap array elements

// bubble sort

// loop to control number of passes

for ( int pass = 0; pass < arraySize - 1; pass++ )

{

// loop to control number of comparisons per pass

for ( int j = 0; j < arraySize - 1; j++ )

{

// compare side-by-side elements and swap them if

// first element is greater than second element

if ( a[ j ] > a[ j + 1 ] )

{

hold = a[ j ];

a[ j ] = a[ j + 1 ];

a[ j + 1 ] = hold;

} // end if

} // end inner for loop

} // end outer for loop

(2)(b) STL Sort

Modify your program from part (a) to use the STL sort function. The sort function takes two parameters, a pointer to the beginning of an array and a pointer to one location past the end of an array. You will need to include the <algorithm> header file. For an array named "a" of size 500, the sort function would be called: sort( a, a + 500);

Alternatively, you could also use a vector of integers (vector <int> v;) and then use the STL sort function (sort(v.begin(), v.end());) to sort the vector. (We will be covering STL as a separate topic).