Swap Salary
You are tasked with creating a program that can update employee salary records. Specifically, you need to implement a feature that allows for a conditional salary swap. Given a table of employees and their salaries, you will adjust the salaries based on a specific rule. This is a common data manipulation task that is fundamental in database management and application development.
Problem Description
You will be given a table named Employees with two columns:
id: An integer representing the unique identifier for each employee.salary: A decimal number representing the employee's salary.
Your task is to update the salary column such that all employees with an odd id have their salary multiplied by 2, and all employees with an even id have their salary divided by 2.
Key Requirements:
- The update should be applied to all rows in the
Employeestable. - The
idcolumn should remain unchanged. - The
salarycolumn will be modified based on the parity of theid.
Expected Behavior:
- If an employee's
idis odd, theirsalaryshould becomesalary * 2. - If an employee's
idis even, theirsalaryshould becomesalary / 2.
Edge Cases:
- What if the
salaryis 0? (It should remain 0 after the operation). - What if the
salaryis negative? (The operation should still apply, e.g., -10 / 2 = -5).
Examples
Example 1:
Input:
Employees table:
| id | salary |
|---|---|
| 1 | 10000 |
| 2 | 20000 |
| 3 | 30000 |
| 4 | 40000 |
Output:
Employees table:
| id | salary |
|---|---|
| 1 | 20000 |
| 2 | 10000 |
| 3 | 60000 |
| 4 | 20000 |
Explanation:
- Employee with
id1 (odd) has salary 10000 -> 10000 * 2 = 20000. - Employee with
id2 (even) has salary 20000 -> 20000 / 2 = 10000. - Employee with
id3 (odd) has salary 30000 -> 30000 * 2 = 60000. - Employee with
id4 (even) has salary 40000 -> 40000 / 2 = 20000.
Example 2:
Input:
Employees table:
| id | salary |
|---|---|
| 5 | 5000 |
| 6 | 6000 |
Output:
Employees table:
| id | salary |
|---|---|
| 5 | 10000 |
| 6 | 3000 |
Explanation:
- Employee with
id5 (odd) has salary 5000 -> 5000 * 2 = 10000. - Employee with
id6 (even) has salary 6000 -> 6000 / 2 = 3000.
Example 3 (Edge Case):
Input:
Employees table:
| id | salary |
|---|---|
| 7 | 0 |
| 8 | -1000 |
Output:
Employees table:
| id | salary |
|---|---|
| 7 | 0 |
| 8 | -500 |
Explanation:
- Employee with
id7 (odd) has salary 0 -> 0 * 2 = 0. - Employee with
id8 (even) has salary -1000 -> -1000 / 2 = -500.
Constraints
- The
idcolumn is an integer and is guaranteed to be unique for each employee. - The
salarycolumn is a decimal number (e.g., float, double, decimal type) and can be positive, zero, or negative. - The number of employees in the table can range from 1 to 1000.
- The values in the
salarycolumn can range from -1,000,000 to 1,000,000. - The solution should be efficient, aiming for a single pass or minimal operations over the table.
Notes
Consider how to check for the parity (even or odd) of an integer. A common way is to use the modulo operator.
Pseudocode Hint:
You can use a conditional statement (IF-THEN-ELSE) to determine which operation to perform on the salary based on the id.