Using C++ Goal: Searching and sorting, pointers, const, and parameter passing. P
ID: 3688012 • Letter: U
Question
Using C++
Goal: Searching and sorting, pointers, const, and parameter passing.
Problem Specification:
A Company has recently hired you to help them generate a payroll report for all their employees. You are given a data file containing the employee’s data. The data for each employee consists of the employee id, name, hours worked and rate of pay. There are 15 employees in the file.
You are to write functions that does the following:
* Read this data into parallel arrays, calculate the gross pay, for each employee by multiplying the hours by the rate and storing it in a gross array.
* Write a function that prints the id, name and gross pay for all the employees whose gross is above $500.00.
* Write a function that sorts the arrays, in ascending order, based on id, using the selection sort (procedure is in the book). In the function place a counter to count the number of comparisons it took before the array was sorted.
* Write a function that prints the arrays after sorting including the number of comparisons.
* Write a function that requests an id number and displays the name and gross for the person with that id number. This function calls the following binary search function.
* A binary search function that will return the position of a requested id number.
Requirements:
1. Every function must have specifications in the form of comments.
2. Make sure every function accomplishes a task and is self-contained
3. Use typedef to define new types as necessary.
4. Each output has a heading and labels to identify information.
5. Must test if data file exists before reading data.
6. main is used as a driver function, no input/output or calculations are done in main, only what is specified.
7. Output is sent to file(s)
Specific Requirements:
- documentations including specifications.
- all arrays are defined with new typedef for the array.
- arrays are passed to pointers and pointer arithmetic is used in processing.
- const is used where appropriate in the parameters to protect data.
- number 1 above is implemented correctly.
- number 2 above is implemented correctly and data is printed out.
- number 3 above is implemented correctly.
- in number 3 above, a counter is used to count number of comparisons.
- number 4 above is implemented correctly and data is printed out.
- number 5 above is implemented correctly and data is printed out.
- binary search function is implemented correctly and returns the position of the given id number for number 5 above to use.
- output is labeled neat and clear.
This is the data given (data.text)
211692
Ahmed, Marco
47 12.50
240885
ATamimi, Trevone
30 15.00
281393
Choudhury, Jacob
10 10.00
272760
De la Cruz, Edward
25 22.25
199593
Edwards, Faraj
60 11.75
256109
Edwards, Bill
45 30.50
246779
Gallimore, Christian
8 15.00
270081
Lopez, Luis
18 16.00
114757
Mora, Sam
37 13.20
270079
Moses, Samuel
48 9.50
193280
Perez, Albert
52 17.75
252830
Rivas, Jonathan
32 23.25
252830
Robinson, Albert
39 11.00
276895
Miranda, Michael
28 18.00
280515
Robinson, Iris
45 22.25
Explanation / Answer
As specified in the program that each and every piece of information is stored in the file. So, we will process the file using
file handling technique.
#include <iostream.h>
#include <stdlib.h>
int main()
{
char ch, file_name[50];
FILE *file;
cout<<"Enter the name of file you wish to see ";
gets(file_name);
file = fopen(file_name,"r"); // opening in read mode
if( file == NULL )
{
perror("Error occurred while opening the file. ");
exit(EXIT_FAILURE);
}
read_data(file);
}
void read_data( File *file)
{
int size = 1024; // define the size of character array
int * id = new int[size];
char *name = new char[size];
int* hours= new int[size];
float* rate= new float[size]; // allocating an array
float* gross= new float[size];
int position = 1; //this will be used incremently to fill characters in the array
int token =0;
if(file.is_open())
{
//file opened successfully so we are here
cout << "File Opened successfully!!!. Reading data from file into array" << endl;
//this loop run until end of file (eof) does not occur
while(!file.eof() && position < size)
{
if(position==2)
file.get(name[position]);
else if(position==3)
{
file.get(hours[position];
float result= 0.0;
while(file>>token){
rate[i]=token;
}
}
file.get(integer[position]); //reading first character from file to array
position++;
}
}
//Now that we have read the text and store the variables in the token we will execute the array to provide the functionality.
for(int i=0;i<15;i++)
{
gross[i]= hours[i]*rate[i];
}
display(gross,name,id);
sort(id);
}
void display( float gross[],char name[],int id[])
{
for(i=0i<15;i++)
{
if(gross[i]>500)
{
cout<< id[i];
cout<<name[i];
}
}
}
void sort(int id[])
{ int min=0;
for(int i=0;i<15;i++)
{
for( int j=i+1;j<15;j++)
{
if(id[i]<id[j]){
count++
id[i]=min;
}
}
swap (id[i], id[min]);
}
cout<<"Number of comparisons are"<<count;
sort_display(id);
}
void sort_display(int id[]){
for (int i=0;i<15;I++)
cout<<id[i];
}
Below is the code I can come up with. here the function needs are maintained.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.