Question

Locked

Can anyone help me modify this C program in the way I'm being asked??

By vineo76 ·
I will admit that this is homework. I am currently taking a 6 week C Programming class at a community college. I was really hung up and stuck on arrays when I was assigned this. Now it is late and I just want to get it done. I really want to learn C. But I also want to pass this class. I wrote the original code; now I just need to modify it. Please help!!

Write a method that prompts the user for hours worked, rate and name. Use parameter passing, and pass by reference.


Write a method that calculates the gross, base and overtime pay, pass by reference.


Write a method that calculates tax, taking as input the gross pay, returning the tax owed.


Calculate the total amount paid (gross pay) for all 5 people. Use the return value from the method that calculates the gross pay.


Write a print method that generates the output, including the total amount paid, in addition to the data for each employee.


#include <stdio.h>


main()


{

char name[25];

float rate = 0;

float hours = 0;

float basePay = 0;

float overTime = 0;

float grossPay = 0;

float netPay = 0;

int i;


for(i = 0; i < 5; i++) {


printf("\nWhat is your name? ");

scanf("%s", &name);

printf("\nHow much is your hourly pay? ");

scanf("%f", &rate);

printf("\nHow many hours did you work this week? ");

scanf("%f", &hours);


basePay = rate * hours;

grossPay = rate * 40 + (basePay - overTime);

overTime = (basePay - (1.5 * rate));

netPay = grossPay - (0.20 * grossPay);

if (hours > 40){

printf("\nPay for %s\n", name);

printf("\nHourly rate:\t $%.2f\n", rate);

printf("\nHours worked:\t $%.2f\n", hours);

printf("\nGross pay:\t $%.2f\n", grossPay);

printf("\nTaxes paid:\t $%.2f\n", 0.20 * grossPay);

printf("\nBase pay:\t $%.2f\n", basePay);

printf("\nOvertime pay:\t $%.2f\n", overTime);

printf("\nNet pay:\t $%.2f\n", netPay);


}

else {

printf("\nPay for %s\n", name);

printf("\nHourly rate:\t $%.2f\n", rate);

printf("\nHours worked:\t $%.2f\n", hours);

printf("\nGross pay:\t $%.2f\n", basePay);

printf("\nTaxes paid:\t $%.2f\n", 0.20 * basePay);

printf("\nBase pay:\t $%.2f\n", basePay);

printf("\nOvertime pay:\t $0.00\n");

printf("\nNet pay:\t $%.2f\n", basePay -(basePay * 0.20));


}


}

}

This conversation is currently closed to new comments.

3 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Collapse -

Too much missing

by Tony Hopkinson In reply to Can anyone help me modify ...

Not a sniff of an array, a struct would have been good and and not one function other than main.

You've got the basic interaction right but you've no design.

Define an employee struct and then make name , netpay etc member's of it.

Change you code to work with that.

Then write functions getEmployeeData, CalulcalteTax, CalculateGrossPay,PrintEmployeeData and pass in a reference to an intance of struct and call them instead of the linear run through you are doing now.

Then define an array of employee, stick a loop in and call the functions for each of the five, and store calculateGrossPay for the tptal thing, though personally I think that's a poor design.

What they are really looking for is not so much arrays, more likely to be a linked list a program like thisanyway , but that you've got and InputFunction, a print function, a calc function. Modularity..

HtHs

But I can't just do all the above for you, you'll learn nothing and just be one more person with a programming qualification they shouldn't have.

Collapse -

Example, the rest is up to you.....

Example (Sample input & output for one person)
Enter name: Glenn
Enter hourly rate: 2.00
Enter hours worked: 50

Pay to: Glenn
Hours worked: 50.0
Hourly rate: $ 2.00
Gross pay: $110.00
Base pay: $ 80.00
Overtime pay: $ 30.00
Taxes paid: $ 22.00
Net pay: $ 88.00


-------------------------

#include <stdio.h>
#include<stdlib.h>
#include <conio.h>
#include<string.h>
int main()
{int i;
double rate[5], hours[5], tax[5], baseSalary[5],overtimePay[5],grossSalary[5],netPay[5];
char name[5][10];
for(i=0;i<5;i++)
{printf("Enter name: ");
scanf("%s",&name);
printf("Enter hourly rate: ");
scanf("%lf",&rate);
printf("Enter hours worked: ");
scanf("%lf",&hours);
//if the inputs are -1 break out of the loop
if(hours==-1||rate==-1||strcmp(name,"-1")==0)
{
exit(0);
}
printf("Pay to: %s \n",name);
printf("Hourly rate: %.2f\n",rate);
printf("Hours worked: %.2f\n",hours);

if(hours>40)
{
grossSalary=((hours-40)*(1.5*rate)+(rate*40)); //gross pay
baseSalary=rate*40; //base salary incase of overtime
overtimePay = ((hours-40)*(rate*1.5)); //calculating overtime pay
tax=grossSalary*.2; //calculating tax
netPay=grossSalary-tax; //calculating net pay
}

else
{
grossSalary=hours*rate;
baseSalary=hours*rate;
overtimePay =0;
tax=grossSalary*.2; //calculating tax
netPay=grossSalary-tax; //calculating net pay
}


printf("Gross Pay: $%.2f\n",grossSalary);
printf("Base Pay: $%.2f\n",baseSalary);
printf("Overtime Pay: $%.2f\n",overtimePay);
printf("Taxes paid: $%.2f\n",tax);
printf("Net Pay: $%.2f\n\n",netPay);
}
getch();
return 0;
}


.............................................


#include <stdio.h>
#include<stdlib.h>
#include <conio.h>
#include<string.h>
void input(double[],double[],char[][10]);
void calcpay(double[],double[],double[],double[],double[]);
double calctax(double);
void print(double [],double[],double[],double[],double[],double[],double[],double,char[][10]);
int main()
{int i;
double rate[5], hours[5], tax[5], baseSalary[5],overtimePay[5],grossSalary[5],netPay[5];
double total=0;
char name[5][10];
input(hours,rate,name);
calcpay(hours,rate,baseSalary,overtimePay,grossSalary);
for(i=0;i<5;i++)
{tax=calctax(grossSalary); //calculating tax
netPay=grossSalary-tax; //calculating net pay
total+=grossSalary;
}
print(rate,hours,tax,baseSalary,overtimePay,grossSalary,netPay,total,name);
getch();
return 0;
}
void input(double hours[],double rate[],char name[][10])
{int i;
for(i=0;i<5;i++)
{printf("Enter name: ");
scanf("%s",&name);
printf("Enter hourly rate: ");
scanf("%lf",&rate);
printf("Enter hours worked: ");
scanf("%lf",&hours);
//if the inputs are -1 break out of the loop
if(hours==-1||rate==-1||strcmp(name,"-1")==0)
{
exit(0);
}
}


}

void calcpay(double hours[],double rate[],double baseSalary[],
double overtimePay[],double grossSalary[])
{int i;
for(i=0;i<5;i++)
if(hours>40)
{
grossSalary=((hours-40)*(1.5*rate)+(rate*40)); //gross pay
baseSalary=rate*40; //base salary incase of overtime
overtimePay = ((hours-40)*(rate*1.5)); //calculating overtime pay
}
else
{
grossSalary=hours*rate;
baseSalary=hours*rate;
overtimePay =0;
}
}
double calctax(double gross)
{return gross*.2;
}

void print(double rate[],double hours[],double tax[],double baseSalary[],
double overtimePay[],double grossSalary[], double netPay[],
double total,char name[][10])
{int i;
printf("\n\n");
for(i=0;i<5;i++)
{printf("Pay to: %s \n",name);
printf("Hourly rate: %.2f\n",rate);
printf("Hours worked: %.2f\n",hours);
printf("Gross Pay: $%.2f\n",grossSalary);
printf("Base Pay: $%.2f\n",baseSalary);
printf("Overtime Pay: $%.2f\n",overtimePay);
printf("Taxes paid: $%.2f\n",tax);
printf("Net Pay: $%.2f\n\n",netPay);
}
printf("\n\nTotal pay: $%.2f\n\n",total);
}

Back to Peripheral Forum
3 total posts (Page 1 of 1)  

Related Discussions

Related Forums