Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need to sort using if statements. I also havent been able to figure out how to a

ID: 3566736 • Letter: N

Question

Need to sort using if statements. I also havent been able to figure out how to add the current date and time.

Write a C program that prompts the user to enter the name of a data file that contains some data regarding some employees. The file contains data that you will store in a struct format defined by:

struct emp
{
int id_num; /* employee number */
float salary; /* employee salary */
char first_name[20]; /* employee first name */
char last_name[30]; /* employee last name */
}

I called mine: struct emp jim[100]; (you may use any name you wish)

I will let you decide the appropriate prompts and edit messages.
You will read the data from the data file and store the info in an array.
Assume no more than 100 records will be in the data file.
You may use the data below for testing purposes with the attached file.
Once the program opens the file, and reads the data into the array, the program
should then SORT THE DATA IN ASCENDING ORDER BY ID NUMBER, OR NAME OR BALANCE (by user choice), and output the Employee data in report form of your design with alignment, or exactly as follows BELOW.

The report will also be directed to an output text file (named report5.txt) with the same output as printed to the screen.

Hints/requirements:
? Use strcat to merge the last name with the first name with a comma between
? Program without sort options is a 10 point deduction.
? Program with no output to report5.txt is a 10 point deduction
? Without current date and time stamp is a 2 point deduction
? No data validation is required (assume it was validated before writing to the file)
? You must open the file in rb mode: in_file_ptr = fopen(file_name, "rb")
? I read the file: fread (&jim[x] , sizeof(jim[x]),1 ,in_file_ptr);

The sample data file contains the following data (in a binary format)

121 512.00 Jon Smith
123 1022.00 John Doe
163 2023.00 Jim Beam
183 3932.00 Jack Daniels
323 922.00 Bill Bailey

Sample I-O below - user responses are in coral:

Welcome to Employee Center

Enter File Name: newdata.dat

5 employees

Sort by: Employee Number(N), Name(A), or Balance(B): a

Employee Salary Report Wed Aug 13 10:42:26 2014
Employee No. Name             Salary
323                Bailey, Bill       922.00
163                Beam, Jim      2023.00
183                Daniels, Jack 3932.00
123                Doe, John       1022.00
121                Smith, Jon       512.00

(Report has been sent to report5.txt)

Thank you for using the program

Explanation / Answer

#include<stdio.h>
#include<string.h>
#include<time.h>

struct emp{
int id_num;
float salary;
char first_name[20];
char last_name[30];
};


int main(){
struct emp jim[100];
char fileName[20];
int x = 0,i,j,intTemp;
char choice;
float floatTemp;
char charTemp[30];
time_t now;

printf("Welcome to Employee Center ");
printf("Enter File Name : ");
scanf("%s",fileName);

FILE *ptr = fopen(fileName, "rb");

while(!feof(ptr)){
fscanf(ptr,"%d",&(jim[x].id_num));
fscanf(ptr,"%f",&(jim[x].salary));
fscanf(ptr,"%s",&(jim[x].first_name));
fscanf(ptr,"%s",&(jim[x].last_name));
x++;
}
fclose(ptr);

printf("%d employees ",x);

printf("Sort by: Employee Number(N), Name(A), or Balance(B): ");
fflush(stdin);
scanf("%c",&choice);

switch(choice){
case 'N' :
case 'n' :
for(i = 0; i< x; i++){
for(j = 0; j < x; j++){
if(jim[i].id_num < jim[j].id_num){
intTemp = jim[i].id_num;
jim[i].id_num = jim[j].id_num;
jim[j].id_num = intTemp;

floatTemp = jim[i].salary;
jim[i].salary = jim[j].salary;
jim[j].salary = floatTemp;

strcpy(charTemp, jim[i].first_name);
strcpy(jim[i].first_name, jim[j].first_name);
strcpy(jim[j].first_name, charTemp);

strcpy(charTemp, jim[i].last_name);
strcpy(jim[i].last_name, jim[j].last_name);
strcpy(jim[j].last_name, charTemp);
}
}
}
break;
case 'A' :
case 'a' :
for(i = 0; i< x; i++){
for(j = 0; j < x; j++){
if(strcmp(jim[i].first_name, jim[j].first_name) > 0){
intTemp = jim[i].id_num;
jim[i].id_num = jim[j].id_num;
jim[j].id_num = intTemp;

floatTemp = jim[i].salary;
jim[i].salary = jim[j].salary;
jim[j].salary = floatTemp;

strcpy(charTemp, jim[i].first_name);
strcpy(jim[i].first_name, jim[j].first_name);
strcpy(jim[j].first_name, charTemp);

strcpy(charTemp, jim[i].last_name);
strcpy(jim[i].last_name, jim[j].last_name);
strcpy(jim[j].last_name, charTemp);
}
}
}
break;
case 'B' :
case 'b' :
for(i = 0; i< x; i++){
for(j = 0; j < x; j++){
if(jim[i].salary < jim[j].salary){
intTemp = jim[i].id_num;
jim[i].id_num = jim[j].id_num;
jim[j].id_num = intTemp;

floatTemp = jim[i].salary;
jim[i].salary = jim[j].salary;
jim[j].salary = floatTemp;

strcpy(charTemp, jim[i].first_name);
strcpy(jim[i].first_name, jim[j].first_name);
strcpy(jim[j].first_name, charTemp);

strcpy(charTemp, jim[i].last_name);
strcpy(jim[i].last_name, jim[j].last_name);
strcpy(jim[j].last_name, charTemp);
}
}
}
break;
default :
printf("Invalid option! ");
}
ptr = fopen("report5.txt", "w");
time(&now);
fprintf(ptr,"Employee Salary Report %s ",ctime(&now));
fprintf(ptr,"Employee No. Name Salary ");
printf(" Employee Salary Report %s ",ctime(&now));
printf("Employee No. Name Salary ");
for(i = 0; i< x; i++){
fprintf(ptr,"%d %s %s %f ",jim[i].id_num, jim[i].first_name, jim[i].last_name, jim[i].salary);
printf("%3d %s %s %5f ",jim[i].id_num, jim[i].first_name, jim[i].last_name, jim[i].salary);
}
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote