Question

  • Creator
    Topic
  • #2213851

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

    Locked

    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

    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));

    }

    }

    }

All Answers

  • Author
    Replies
    • #3028533

      Clarifications

      by vineo76 ·

      In reply to Can anyone help me modify this C program in the way I’m being asked??

      Clarifications

    • #3028494

      Too much missing

      by tony hopkinson ·

      In reply to Can anyone help me modify this C program in the way I’m being asked??

      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.

    • #3028493

      Example, the rest is up to you…..

      by peconet tietokoneet ·

      In reply to Can anyone help me modify this C program in the way I’m being asked??

      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
      #include
      #include
      #include
      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[i]);
      printf("Enter hourly rate: ");
      scanf("%lf",&rate[i]);
      printf("Enter hours worked: ");
      scanf("%lf",&hours[i]);
      //if the inputs are -1 break out of the loop
      if(hours[i]==-1||rate[i]==-1||strcmp(name[i],"-1")==0)
      {
      exit(0);
      }
      printf("Pay to: %s \n",name[i]);
      printf("Hourly rate: %.2f\n",rate[i]);
      printf("Hours worked: %.2f\n",hours[i]);

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

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

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

      ………………………………………

      #include
      #include
      #include
      #include
      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[i]=calctax(grossSalary[i]); //calculating tax
      netPay[i]=grossSalary[i]-tax[i]; //calculating net pay
      total+=grossSalary[i];
      }
      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[i]);
      printf("Enter hourly rate: ");
      scanf("%lf",&rate[i]);
      printf("Enter hours worked: ");
      scanf("%lf",&hours[i]);
      //if the inputs are -1 break out of the loop
      if(hours[i]==-1||rate[i]==-1||strcmp(name[i],"-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[i]>40)
      {
      grossSalary[i]=((hours[i]-40)*(1.5*rate[i])+(rate[i]*40)); //gross pay
      baseSalary[i]=rate[i]*40; //base salary incase of overtime
      overtimePay[i] = ((hours[i]-40)*(rate[i]*1.5)); //calculating overtime pay
      }
      else
      {
      grossSalary[i]=hours[i]*rate[i];
      baseSalary[i]=hours[i]*rate[i];
      overtimePay[i] =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[i]);
      printf("Hourly rate: %.2f\n",rate[i]);
      printf("Hours worked: %.2f\n",hours[i]);
      printf("Gross Pay: $%.2f\n",grossSalary[i]);
      printf("Base Pay: $%.2f\n",baseSalary[i]);
      printf("Overtime Pay: $%.2f\n",overtimePay[i]);
      printf("Taxes paid: $%.2f\n",tax[i]);
      printf("Net Pay: $%.2f\n\n",netPay[i]);
      }
      printf("\n\nTotal pay: $%.2f\n\n",total);
      }

Viewing 2 reply threads