Exercises I don't get it!

1) Develop a C++ program that will determine the gross pay for each of several employees. The company pays "straight-time" for the first 40 hours worked by each employee amd pays "time-and-a half" for all hours worked in excess of 40 hours. You are given a list of employees of the company, the number of hours each employee worked last night and the hourly rate of each employee. Your program should input this information for each employeee and should determine and display the employee's gross pay.

Enter hours worked (-1 to end): 39
Enter hourly rate of the worker ($00.00): 10.00
Salary is: $390.00

Enter hours worked (-1 to end): 40
Enter hourly rate of the worker ($00.00): 10.00
Salary is: $400.00

Enter hours worked (-1 to end): 41
Enter hourly rate of the worker ($00.00): 10.00
Salary is: $415.00



I tried to found the math expression for this one "Hours * Hourly rate = Salary", but it didn't apply for "41 hours"...


2) Write a C++ program that utilizes looping and the tab escape sequence \t to print the following table of values:


N    10*N    100*N    1000*N

1    10      100      1000
2    20      200      2000
3    30      300      3000
4    40      400      4000
5    50      500      5000


I only know how to do "N 1-5". Idk about the rest!
Last edited on
You need to come up with a way of expressing the idea that the first 40 hours get one salary rate, and all hours after that get a different rate.

If I tell you that someone worked 50 hours, how many hours of that is at the first pay rate and how many is at the second pay rate? How did you work that out?
Hmmm.........
I'm clueless. 0.0
1
2
3
4
5
for (n=1;n<6;n++)
    {
        //Display one row at a time
        cout<<n<<"\t"<<n*10<<"\t"<<n*100<<"\t"<<n*1000<<"\t\n";
    }
The first 40 hours get paid at one rate. All following hours get paid at another rate.

If someone works 50 hours, how many hours of that are paid at the first rate? If you can't tell me that, you're really going to struggle as a programmer.
Hi, I am new/beginner, and I am self-taught as it is a hobby/interest.

Thank you for sharing this exercise, I have come up with a solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>

using namespace std;

namespace global
{
    bool executing_code ;
}

void employee() ;

int main()
{
    global::executing_code = true ;
    while ( global::executing_code )
    {
        employee() ;
    }
    return 0 ;
}

void employee()
{
    double salary ;
    int hours ;
    int overtime ;
    double rate ;
    int work_limit = 40 ; // adjustable in the source code if desired

    cout << "Enter hours worked (-1 to end): " ;
    cin >> hours ;
    if (hours < 0) { global::executing_code = false ; return ; }
    cout << "Enter hourly rate of the worker ($00.00): " ;
    cin >> rate ;
    overtime = hours - work_limit ;
    if (overtime > 0)
    {
        salary = ( work_limit * rate ) + ( overtime * rate * 1.5 ) ;
    }
    else { salary = hours * rate ; }
    cout << "Salary is: $" << salary << "\n\n" ;
    return;
}


I am not entirely sure if this is the correct use for a namespace, but it was a quick bandage to figure out how to make the employee() function change a variable that is used outside of this function. Note that it is my first time using a namespace for anything particular.
you don't need a namespace, just doing bool executing_code outside of any scope/function/class/etc will make it global, for extra safety you can then refer to it with ::executing_code, so if you make a second variable called executing_code by accident in one of your functions, it will use the global one instead of the local one.
@Moschop: Is it 500? I just times it by 50 hours * hourly rate which is 10.00.
I'm not moschops, just to make that clear, because you know, we are so alike.

they get paid 10$ an hour for the first 40 hours, then every hour after that they get paid 10$ * 1.5

so first let's figure out 40 hours, 10$ * 40 hours = 400, easy.

15$ because 10 * 1.5 is 15.

now the other 10 hours, 15$ * 10 hours = 150, easy.

400 + 150 = 550$

Last edited on
Hmmm.... where did you get the 1.5?
amd pays "time-and-a half" for all hours worked in excess of 40 hours.


time = 1 hour, 1 hour + .5 hour = 1.5 hour, 1.5 hour * 10 = 15;
Last edited on
Topic archived. No new replies allowed.