Poster Notes: This program is actually done, thank you very much to the one who
ID: 3698326 • Letter: P
Question
Poster Notes: This program is actually done, thank you very much to the one who helped me. The only thing that are missing is comments. I am really lost on linked list. The part that needs most comment on is the linked list parts. Please help me on the commenting, with focus on the linked list parts. the commenting on other parts is also appreciated. I would appreciate the help I can get with this, thanks.
Here is the question:
Use the linked list operation to write a program to enter data for staff members name and salary values (unidentified number of items, so use some sort of sentinel value). Calculate the average of salary values and display it. Sort the staff members names in alphabetical order using any sorting technique you want.
Here is the code:
#include<iostream>
#include <malloc.h>
#include<string.h>
using namespace std;
struct staff
{
char name[10];
float salary;
};
int main()
{
struct staff *emp;
emp=(struct staff*)malloc(sizeof(struct staff));
char temp[10];
float sum=0,avg;
int i,j,n;
cout<<"Enter the no.of details ";
cin>>n;
// Example: abc 99.3
cout<<"Enter "<<n<<" staff names and their salary values ";
for(i=0;i<n;i++)
{
cin>>emp[i].name;
cin>>emp[i].salary;
sum=sum+emp[i].salary;
}
avg=sum/n;
cout<<"Average salary "<<avg<<" ";
cout<<"Before sorting Names"<<" ";
for(i=0;i<n;i++)
{
cout<<emp[i].name<<" ";
}
for(i=0;i<n;i++)
{
for(j=i;j<n-1;j++)
{
if(strcmp(emp[i].name,emp[j+1].name)>0)
{
strcpy(temp,emp[i].name);
strcpy(emp[i].name,emp[j+1].name);
strcpy(emp[j+1].name,temp);
}
}
}
cout<<"after sorting"<<" ";
for(i=0;i<n;i++)
{
cout<<emp[i].name<<" ";
}
return 0;
}
Explanation / Answer
#include<iostream>
#include <malloc.h>
#include<string.h>
using namespace std;
// create a struct data type to store staff informations as a single unit
struct staff
{
char name[10];
float salary;
};
int main()
{
struct staff *emp;
emp=(struct staff*)malloc(sizeof(struct staff)); // allocating memory for a pointer of staffs
char temp[10];
float sum=0,avg;
int i,j,n;
cout<<"Enter the no.of details ";
cin>>n;
// Example: abc 99.3
cout<<"Enter "<<n<<" staff names and their salary values ";
for(i=0;i<n;i++)
{
cin>>emp[i].name; //read the name into emp, which is of type staff
cin>>emp[i].salary; //read the salary into emp, which is of type staff
sum=sum+emp[i].salary; //summation of the salary
}
avg=sum/n; // finding avg of salary
cout<<"Average salary "<<avg<<" ";
cout<<"Before sorting Names"<<" ";
for(i=0;i<n;i++)
{
cout<<emp[i].name<<" "; // iterating over all of the staff structs and print its name
}
for(i=0;i<n;i++)
{
for(j=i;j<n-1;j++)
{
if(strcmp(emp[i].name,emp[j+1].name)>0) // compare the names of adjacent staff structs in order to sort the staffs
{
strcpy(temp,emp[i].name);
strcpy(emp[i].name,emp[j+1].name);
strcpy(emp[j+1].name,temp);
}
}
}
cout<<"after sorting"<<" ";
for(i=0;i<n;i++)
{
cout<<emp[i].name<<" "; // printing the name after sorting
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.