Question & Answer: Jones,Frank 2 3 8 3635 Smith, Tiny 883082 Turk, Tom 9104700 e Jackson,Jim 5 6 56 5 6 5 Catan,Doug 7 387257…..

Write a C++ program that will need to read from a file of employee data for how many hours each employee worked for a week. Then for each of the employees, a total of the hours worked is made and then the employees are sorted into descending order based on their total hours using bubble sort. Cannot use structures or vectors.

There needs be 3 function calls total in main that:

Open and read the file into the array and add the total hours worked for each employee

Sort the array based on the total hours

Write out the output

Input file looks like this:

Jones,Frank 2 3 8 3635 Smith, Tiny 883082 Turk, Tom 9104700 e Jackson,Jim 5 6 56 5 6 5 Catan,Doug 7 387257

Output should look something similar to this:

Jones,Frank 2 3 8 3635 Smith, Tiny 883082 Turk, Tom 9104700 e Jackson,Jim 5 6 56 5 6 5 Catan,Doug 7 387257

Expert Answer

 

/* This is the old one

Here is your code try and get the logic

#include <iostream>
#include <fstream>
using namespace std;
int main()
{

int c;
int sa[20][6];
int i,j,n;
char name[20][20];

ifstream myfile;
myfile.open(“file.txt”);    // your file
myfile>>n;
int sum[2][n];           // we create a two dimensional array to store one is for total hour and another for person
// ie sum[0][n] is for saving TTL
// and sum[1][n] is for link we use it for sorting
for(i=0;i<n;i++)    // geting form file
{
sum[0][i]=0;
sum[1][i]=i;
myfile>>name[i];
for(j=0;j<6;j++)
{
myfile>>sa[i][j];
sum[0][i]+=sa[i][j];

}

}

for (i = 0; i < n; ++i)    // sorting according to the sum that is total working hours
{
for (j = 0; j < n-i-1; ++j)
{
// Comparing consecutive data and switching values if value at j < j+1.
if (sum[0][j] < sum[0][j+1])
{
sum[0][j] = sum[0][j]+sum[0][j+1];
sum[0][j+1] = sum[0][j]-sum[0][j + 1];
sum[0][j] = sum[0][j]-sum[0][j + 1];

sum[1][j] = sum[1][j]+sum[1][j+1];             // this will set the link or swap link too
sum[1][j+1] = sum[1][j]-sum[1][j + 1];
sum[1][j] = sum[1][j]-sum[1][j + 1];

}
}

}

//display

cout<<“Employee Weekly Hours:n”;
cout<<“Name:ttMtTtWtTtFtStTTLn”;

for(i=0;i<n;i++)         // we have sorted sum so
{
c=sum[1][i];         // here c will hold the link who has the larger value, ie sum[0][i] will have high value
cout<<name[c]<<“t”;   // print using c
for(j=0;j<6;j++)
{
cout<<sa[c][j]<<“t”;
}
cout<<sum[0][i]<<“n”;
}

myfile.close();

return 0;
}

*/

Here is the new code the old one is also there

#include <iostream>
#include <fstream>
using namespace std;

int sa[20][6];
char name[20][20];
const int m=2;

void bubble_sort(int sum[][m],int n)
{
int i,j;

for (i = 0; i < n; ++i)    // sorting according to the sum that is total working hours
{
for (j = 0; j < n-i-1; ++j)
{
// Comparing consecutive data and switching values if value at j < j+1.
if (sum[j][0] < sum[j+1][0])
{
sum[j][0] = sum[j][0]+sum[j+1][0];
sum[j+1][0] = sum[j][0]-sum[j + 1][0];
sum[j][0] = sum[j][0]-sum[j + 1][0];

sum[j][1] = sum[j][1]+sum[j+1][1];             // this will set the link or swap link too
sum[j+1][1] = sum[j][1]-sum[j + 1][1];
sum[j][1] = sum[j][1]-sum[j + 1][1];

}
}
}
}

int total_working_hr(int sa[20][6],int n)
{
int sum=0,i;
for(i=0;i<6;i++)
{
sum+=sa[n][i];
}

return sum;
}
int readfile()
{
int n,i,j;
ifstream myfile;
myfile.open(“file.txt”);    // your file
myfile>>n;
for(i=0;i<n;i++)    // geting form file
{

myfile>>name[i];
for(j=0;j<6;j++)
{
myfile>>sa[i][j];
//sum[i][0]+=sa[i][j];

}

}
myfile.close();
return n;
}

int main()
{
int i,j,n,c;
n=readfile();        // Calling the first function
int sum[n][2];           // we create a two dimensional array to store one is for total hour and another for person
// ie sum[0][n] is for saving TTL
// and sum[1][n] is for link we use it for sorting

for(i=0;i<n;i++)    // Setting the link
sum[i][1]=i;

for(i=0;i<n;i++)
{
sum[i][0]=total_working_hr(sa,i);       // Calling the second function
}
bubble_sort(sum,n);     // Calling the 3 rd function
//display

cout<<“Employee Weekly Hours:n”;
cout<<“Name:ttMtTtWtTtFtStTTLn”;

for(i=0;i<n;i++)         // we have sorted sum so
{
c=sum[i][1];         // here c will hold the link who has the larger value, ie sum[0][i] will have high value
cout<<name[c]<<“t”;   // print using c
for(j=0;j<6;j++)
{
cout<<sa[c][j]<<“t”;
}
cout<<sum[i][0]<<“n”;
}

return 0;
}

The o/p is same

the file.txt

the out put

Still stressed from student homework?
Get quality assistance from academic writers!