Assume the input data is structured as follows: first there is a non-negative integer specifying the number of employee timesheets to be read in. This is followed by data for each of the employees. The first number for each employee is an integer that specifies their pay per hour in cents. Following this are 5 integers , the number of hours they worked on each of the days of the workweek. Given this data, and given that an int variable total has been declared , write a loop and any necessary code that reads the data and stores the total payroll of all employees in total . Note that you will have to add up the numbers worked by each employee and multiply that by that particular employee’s pay rate to get the employee’s pay for the week– and sum those values into total.

5
Assume the input data is structured as follows: first there is a non-negative integer specifying the number of employee timesheets to be read in. This is followed by data for each of the employees. The first number for each employee is an integer that specifies their pay per hour in cents. Following this are 5 integers , the number of hours they worked on each of the days of the workweek. Given this data, and given that an int variable total has been declared , write a loop and any necessary code that reads the data and stores the total payroll of all employees in total . Note that you will have to add up the numbers worked by each employee and multiply that by that particular employee’s pay rate to get the employee’s pay for the week– and sum those values into total.

LANGUAGE: C++

CHALLENGE:

Assume the input data is structured as follows: first there is a non-negative integer specifying the number of employee timesheets to be read in. This is followed by data for each of the employees. The first number for each employee is an integer that specifies their pay per hour in cents. Following this are 5 integers , the number of hours they worked on each of the days of the workweek. Given this data, and given that an int variable total has been declared , write a loop and any necessary code that reads the data and stores the total payroll of all employees in total . Note that you will have to add up the numbers worked by each employee and multiply that by that particular employee’s pay rate to get the employee’s pay for the week– and sum those values into total.

SOLUTION:


int employees;
int a,b;
int wage=0;
int hours=0;

total=0;

cin >> employees;

for (a=1; a<=employees; a++){
    cin >> wage;
    for (b=1; b<=5; b++){
        cin >> hours;
        total += hours * wage;
    }
}

5 comments

  • Winston says:

    Dear Matthew,

    i have a question about the challenge.
    The question is:

    What is the difference between this (your) solution and

    int employees;
    int a,b;
    int wage=0;
    int hours=0;

    total=0;

    cin >> employees;

    for (a=1; a<=employees; a++){
    for (b=1; b> wage;
    cin >> hours;
    total += hours * wage;
    }
    }

    The test table gives a error in total…
    What is the difference or logical error in the second solution?

    Thanks in advance…

    Greetz Winston

    • Your syntax has errors and that is the first major issue. The inner (for loop) has no closing parenthesis ending the parameters.

      The routine being defined inside the inner (for loop) also needs an open bracket identifying the beginning of what you expect to be performed during each iteration.

      Once you get that corrected, I trust that the mathematical issue’s will identify themselves.

  • Ben Large says:

    Here’s the C version:

    int employees;
    int a,b;
    int wage=0;
    int hours=0;

    total=0;

    scanf(“%d”, &employees);

    for (a=1; a<=employees; a++){
    scanf("%d", &wage);
    for (b=1; b<=5; b++){
    scanf("%d", &hours);
    total += hours * wage;
    }
    }

  • Matthew Welch says:

    Here’s the Java version:
    int employees;
    int a,b;
    int wage=0;
    int hours=0;

    total=0;

    employees = stdin.nextInt();

    for (a=1; a<=employees; a++){
    wage = stdin.nextInt();
    for (b=1; b<=5; b++){
    hours = stdin.nextInt();
    total += hours * wage;
    }
    }

  • stan says:

    where is java solution?

Add comment