Menu

A big problem for me!!!

#10lincoln
2007-11-03
2012-09-26
  • #10lincoln

    #10lincoln - 2007-11-03

    Can you solve the question written below?

    Question :

    a program in C to compute a company's weekly salary. The program will allow the salary clerk (user) to input the employee’s ID number (int), hours worked (int) and the salary per hour (float) for 8 employees.

    The program should compute and display the ID, regular salary, overtime salary, and total salary for each employee.

    Salaries are computed as follows:
    • Regular salary: If the hours worked are 40 or less, regular salary is computed as the salary per hour multiplied by the hours worked; if more than 40 hours are worked, regular salary is computed as the salary per hour multiplied by 40.

    • Overtime salary: If the hours worked are 40 or less, overtime salary is zero; if more than 40 hours are worked, overtime salary is computed as the number of hours worked above 40 multiplied by 70% the salary per hour.

    • Total salary: Total salary is the sum of regular salary and overtime salary.

    The program will continue to execute for 7 employees. Then at the end the program, display the largest overtime salary and the ID of that employee.

    Your program should have a while loop and if/else statements.

    Examples: Assuming the salary per hour for an employee is 25.

    If an employee worked for 30 hours per week then the regular salary is 30*25= 750 and the overtime salary is 0.

    If an employee worked 45 hours the regular salary is 2540 = 1000 and the overtime salary is 70/10025*(45-40) = 87.5

     
    • Osito

      Osito - 2007-11-03

      Show us what you've got so far...

       
    • #10lincoln

      #10lincoln - 2007-11-03

      I tried to do but nothing.

       
    • #10lincoln

      #10lincoln - 2007-11-03

      Actually I didn't save the project file.

       
      • Osito

        Osito - 2007-11-03

        That's OK, as long as you saved your .c file. If not, I guess you're starting over? Write something and post it here. You'll find a number of people willing to help, but no one is going to do your entire assignment for you.

         
    • #10lincoln

      #10lincoln - 2007-11-03

      At the end i found it.
      now can you tell me whatis going wrong with it?
      i cand find the worker(ID number) who has the biggest overtime salary.

      include <stdio.h>

      int main(void)
      {
      int counter=1;
      int ID1;
      int ID2;
      int whour; /Work Hour/
      float rsalary; /Regular Salary/
      float largest; /Overtime Salary 1/
      float osalary; /Overtime Salary 2/
      float tsalary; /Total Salary/
      float sperhour; /Salary Per Hour/
      printf("Welcome to C!\n");
      printf("Enter your ID number.\n");
      scanf("%d",&ID1);
      printf("Enter your work hours.\n");
      scanf("%d",&whour);
      printf("Enter your salary per hour.\n");
      scanf("%f",&sperhour);
      if(whour>40)
      {
      osalary=0.7sperhour(whour-40);
      rsalary=sperhourwhour;
      tsalary=rsalary+osalary;
      printf("User ID : %d.\n",ID1);
      printf("Your regular salary is %f.\n",rsalary);
      printf("Your overtime salary is %f.\n",osalary);
      printf("Your total salary is %f.\n",tsalary);
      }
      if(whour<=40)
      {
      rsalary=sperhour
      whour;
      tsalary=rsalary;
      printf("User ID : %d.\n",ID1);
      printf("Your regular salary is %f.\n",rsalary);
      printf("Your overtime salary is 0.\n");
      printf("Your total salary is %f.\n",tsalary);
      }
      while(counter<=7)
      {
      counter=counter+1;
      printf("Enter your ID number.\n");
      scanf("%d",&ID2);
      printf("Enter your work hours.\n");
      scanf("%d",&whour);
      printf("Enter your salary per hour.\n");
      scanf("%f",&sperhour);
      if(whour>40)
      {
      largest=0.7sperhour(whour-40);
      rsalary=sperhourwhour;
      tsalary=rsalary+largest;
      printf("User ID : %d.\n",ID2);
      printf("Your regular salary is %f.\n",rsalary);
      printf("Your overtime salary is %f.\n",largest);
      printf("Your total salary is %f.\n",tsalary);
      }
      if(whour<=40)
      {
      rsalary=sperhour
      whour;
      tsalary=rsalary;
      printf("User ID : %d.\n",ID2);
      printf("Your regular salary is %f.\n",rsalary);
      printf("Your overtime salary is 0.\n");
      printf("Your total salary is %f.\n",tsalary);
      }
      if (largest>osalary)
      osalary=largest;
      }
      if(osalary>largest)
      {
      printf("The biggest overtime salary is %f.\nWorker's ID : %d.\n",osalary,ID2);
      }
      if(largest>osalary)
      {
      printf("The biggest overtime salary is %f.\nWorker's ID : %d.\n",largest,ID1);
      }

      return 0;
      

      }

       
      • Osito

        Osito - 2007-11-03

        Is largest supposed to be the largest salary? It looks like you set it every time:

        largest=0.7sperhour(whour-40);

        What you need is a variable that you initialize to -1, and each time through the loop when a new user is entered, you compare their calculated salary to that variable, and if the new salary is higher, set the variable to the new salary:

        float max_salary = -1.0; // initialize to out-of-range value

        ...

        // check against current max salary
        if(rsalary > max_salary) {max_salary = rsalary ;}

        Then at the end you can print out max_salary. You can do the same thing if you need the highest overtime salary, or total salary, by adding additional floats to keep track of those maximums.

         
        • Osito

          Osito - 2007-11-03

          Also, I noticed it said you needed an if/else, but you only have an if. You should convert these two statements to an if/else:

          if(whour>40)
          {
          ...
          }
          if(whour<=40)
          {
          ...
          }

          Only one of those cases will ever be true, so you could do this:

          if(whour>40)
          {
          ...
          }
          else // whour is less than or equal to 40
          {
          ...
          }

           
    • #10lincoln

      #10lincoln - 2007-11-04

      ok.tanks for help.but another problem is finding the worker's ID number(worker who has the biggest overtime salary)

       
    • #10lincoln

      #10lincoln - 2007-11-04

      I edited what went wrong.Now the only problem is to find the worker's ID which is related to biggest overtime salary.

      include <stdio.h>

      int main()
      {
      int counter=1;
      int ID1;
      int ID2;
      int whour; /Work Hour/
      float rsalary; /Regular Salary/
      float largest; /Overtime Salary 1/
      float osalary; /Overtime Salary 2/
      float tsalary; /Total Salary/
      float sperhour; /Salary Per Hour/
      printf("Welcome to C!\n");
      printf("Enter your ID number.\n");
      scanf("%d",&ID1);
      printf("Enter your work hours.\n");
      scanf("%d",&whour);
      printf("Enter your salary per hour.\n");
      scanf("%f",&sperhour);
      if(whour>40)
      {
      rsalary=sperhourwhour;
      largest=(float)0.7
      sperhour(whour-40);
      tsalary=rsalary+largest;
      printf("User ID : %d.\n",ID1);
      printf("Your regular salary is %f.\n",rsalary);
      printf("Your overtime salary is %f.\n",largest);
      printf("Your total salary is %f.\n",tsalary);
      }
      else
      {
      rsalary=sperhour
      whour;
      tsalary=rsalary;
      printf("User ID : %d.\n",ID1);
      printf("Your regular salary is %f.\n",rsalary);
      printf("Your overtime salary is 0.\n");
      printf("Your total salary is %f.\n",tsalary);
      }
      while(counter<=7)
      {
      counter=counter+1;
      printf("Enter your ID number.\n");
      scanf("%d",&ID2);
      printf("Enter your work hours.\n");
      scanf("%d",&whour);
      printf("Enter your salary per hour.\n");
      scanf("%f",&sperhour);
      if(whour>40)
      {
      rsalary=sperhourwhour;
      osalary=(float)0.7
      sperhour(whour-40);
      tsalary=rsalary+osalary;
      printf("User ID : %d.\n",ID2);
      printf("Your regular salary is %f.\n",rsalary);
      printf("Your overtime salary is %f.\n",osalary);
      printf("Your total salary is %f.\n",tsalary);
      }
      else
      {
      rsalary=sperhour
      whour;
      tsalary=rsalary;
      printf("User ID : %d.\n",ID2);
      printf("Your regular salary is %f.\n",rsalary);
      printf("Your overtime salary is 0.\n");
      printf("Your total salary is %f.\n",tsalary);
      }
      if(osalary>largest)
      largest=osalary;
      }

      printf("The biggest overtime salary is %f.\n",largest);
      return 0;
      }

       
      • Osito

        Osito - 2007-11-04

        So just save off a copy of the ID any time you update the largest overtime salary.

         
    • #10lincoln

      #10lincoln - 2007-11-04

      can you explain it clearlier please

       
      • Osito

        Osito - 2007-11-04

        I was intentionally a little vague so you would have to actually think for yourself. The point of homework is to make you learn, not see how well you can find answers using an internet forum. Although these days that doesn't seem to be the case any more. I guess I'm old-fashioned. A 34-year-old dinosaur. Scary.

        Every time that osalary is larger than largest, you could save not only the new osalary value in largest, but the current id in largest_id. That way it will be there at the end of your program, for printing purposes.

         
    • Wayne Keen

      Wayne Keen - 2007-11-04

      Consider the following pseudocode segment that looks for a maximum in an array,
      and where that maximum occurs...

      my_max = -99999.999;
      for (i = 0; i <= n; i++)
      {
      if (x[i] > my_max)
      {
      my_max = x[i];
      imax = i;
      }
      }

      my_max is the maximum value, imax is the index at which that value occurs.

      Wayne

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.