USe data structures in the fallowing program. At least 2. Use the code below and
ID: 3726653 • Letter: U
Question
USe data structures in the fallowing program. At least 2.
Use the code below and add data structures to it.
#include <iostream>
using namespace std;
int main()
{
string fname[20];
char minitial[20];
string lname[20];
double hours[20];
double rate[20];
double gross[20],stax[20],ftax[20],ufees[20],net[20];
double totalgross=0,avggross,overtime;
char op='y';
int i=0;
while(op=='y')
{
cout<<" Enter details of employee"<<i+1<<":";
cout<<" Enter First Name : ";
cin>>fname[i];
cout<<" Enter Middle Inital : ";
cin>>minitial[i];
cout<<" Enter Last Name : ";
cin>>lname[i];
l1 : cout<<" Enter Hours worked : ";
cin>>hours[i];
if(hours[i]<0 || hours[i]>60)
{
cout<<" Invalid data : Hours worked";
goto l1;
}
l2:cout<<" Enter Rate : ";
cin>>rate[i];
if(rate[i]<0 || rate[i]>50)
{
cout<<" Invalid data : Rate";
goto l2;
}
cout<<" Would you like to continue : (y or n)";
cin>>op;
i++;
}
int j = i;
for(i=0;i<j;i++)
{
if(hours[i]>40)
{
overtime = (hours[i]-40)*rate[i]*1.5;
hours[i] = 40;
}
else
overtime = 0;
gross[i] = (rate[i]*hours[i])+overtime;
stax[i] = gross[i]*6/100;
ftax[i] = gross[i]*12/100;
ufees[i] = gross[i]*2/100;
net[i] = gross[i]-(stax[i]+ftax[i]+ufees[i]);
totalgross += gross[i];
}
cout<<" Data housing corp.week payroll";
cout<<" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
cout<<" First Name MI Last Name Rate Hours Gross Pay State tax Federal Tax Union fees Net Pay ";
for(i=0;i<j;i++)
{
cout<<fname[i]<<" ";
cout<<minitial[i]<<" ";
cout<<lname[i]<<" ";
cout<<rate[i]<<" ";
cout<<hours[i]<<" ";
cout<<gross[i]<<" ";
cout<<stax[i]<<" ";
cout<<ftax[i]<<" ";
cout<<ufees[i]<<" ";
cout<<net[i]<<" ";
}
cout<<" Total gross pay "<<totalgross;
avggross = totalgross/j;
cout<<" Average gross pay "<<avggross;
}
Explanation / Answer
#include<iostream>
using namespace std;
/* defines the structure of a single linked list node*/
typedef struct list_node {
string fname;
char minitial;
string lname;
double hours;
double rate;
double gross,stax,ftax,ufees,net,totalgross;
struct list_node *next; // pointer to next node in the list
}node;
/* create new node */
node *getNewNode(void) {
node *new_node = new node;
int overtime;
// taking user input and calculaating amount
cout<<" Enter details of employee"":";
cout<<" Enter First Name : ";
cin>>new_node->fname;
cout<<" Enter Middle Inital : ";
cin>>new_node->minitial;
cout<<" Enter Last Name : ";
cin>>new_node->lname;
l1 : cout<<" Enter Hours worked : ";
cin>>(new_node->hours);
if(new_node->hours<0 || new_node->hours>60)
{
cout<<" Invalid data : Hours worked";
goto l1;
}
l2:cout<<" Enter Rate : ";
cin>>new_node->rate;
if(new_node->rate<0 || new_node->rate>50)
{
cout<<" Invalid data : Rate";
goto l2;
}
if(new_node->hours>40)
{
overtime = (new_node->hours-40)*new_node->rate*1.5;
new_node->hours = 40;
}
else
overtime = 0;
new_node->gross = (new_node->rate*new_node->hours)+overtime;
new_node->stax = new_node->gross*6/100;
new_node->ftax = new_node->gross*12/100;
new_node->ufees = new_node->gross*2/100;
new_node->net = (new_node->gross)-((new_node->stax)+(new_node->ftax)+(new_node->ufees));
(new_node->totalgross) += (new_node->gross);
new_node->next = NULL;
return new_node;
}
/* displays the list elements */
void displayList(node *head) {
cout << "Displaying List : ";
cout<<" Name MI Last Name Rate Hours Gross_Pay State_tax f_tax ufee Net ";
while (head != NULL) {
cout<<head->fname<<" ";
cout<<head->minitial<<" ";
cout<<head->lname<<" ";
cout<<head->rate<<" ";
cout<<head->hours<<" ";
cout<<head->gross<<" ";
cout<<head->stax<<" ";
cout<<head->ftax<<" ";
cout<<head->ufees<<" ";
cout<<head->net<<" ";
head = head->next;
}
}
/* insert a node at the beginning of the list */
node *insertNodeBeg(node *head) {
node *ptr = getNewNode();
if (head == NULL) { // if list is empty
head = ptr;
}
else {
ptr->next = head;
head = ptr;
}
return head;
}
int main() {
node *head = NULL;
char op='y';
while(op == 'y')
{
// head = insertNodeBeg(head); // 7
head = insertNodeBeg(head); // 9 -> 7
cout<<" enter y to continew ";
cin>>op;
}
displayList(head);
return 0;
}
/*
sample OUTPUT:
Enter details of employee:
Enter First Name : joy
Enter Middle Inital : k
Enter Last Name : happy
Enter Hours worked : 20
Enter Rate : 3.5
enter y to continew
y
Enter details of employee:
Enter First Name : thomas
Enter Middle Inital : k
Enter Last Name : dev
Enter Hours worked : 13
Enter Rate : 2.5
enter y to continew
n
Displaying List :
Name MI Last Name Rate Hours Gross_Pay State_tax f_tax ufee Net
thomas k dev 2.5 13 32.5 1.95 3.9 0.65 26
joy k happy 3.5 20 70 4.2 8.4 1.4 56
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.