0

So I am to determine the base salary for an employee based on an inputed number of years the employee has worked for the company.

I have to do this using switch structures and I am given ranges as follows:

Number of years worked____Base Salary

 0 – 5                  $ 9,500 
 6 – 11                 $12,700 
12 – 17                 $15,300 
18 – 29                 $22,600 
 >= 30                  $26,800

So, how do I do ranges for the cases if I don't want to right case for all of the numbers?

It isn't too much of a hassle with this one, but then I have to figure out commission based on sales and that has ranges like $0-3,999.99 and $16,000-23,999.99.

1
  • Take a look at C4.5 learning algorithm. It can generate a decision tree, which is very easy to rewrite into switch/if statements. Commented Oct 7, 2013 at 20:53

3 Answers 3

2

If you are forced to use switch statement then consider the fact that multiple cases can be chained together:

switch (years) {
  case 0:
  case 1:
  case 2:
  ..
    return 9500;
  case 6:
  case 7:
  ..
}

But an if statement seems more suitable to the problem:

if (years >= 0 && years <= 5)
  ..
else if (years >= 6 && years <= 11)
  ..
Sign up to request clarification or add additional context in comments.

2 Comments

I am not allowed to use if statements at all unfortunately.
I wrote out the years switch like that, but then for commission the range is just too large to do that.
1

So for part one, you just need to declare a switch where multiple cases follow one code path. Like this:

int baseSalary
switch (yearsWorked)
{
  case 0:
  case 1:
  case 2:
  case 3:
  case 4:
  case 5:
    baseSalary = 9500;
    break;

  case 6:
  case 7:
  case 8:
  case 9:
  case 10:
  case 11:
    baseSalary = 12700;
    break;

  ... etc ...
}

For the second part, a switch for every single number in a range of thousands is pretty unfeasible, but with a bit of smart division, it can be made equally easy. If you divide 2000 by 1000, you get 2, and if you divide 2500 by 1000, you also get 2 (with remainder 500). Using this, you can generate a switch statement:

int sales = 2100;
int salesRange = sales / 1000; // (salesRange = 2)

int commission
switch (salesRange)
{
  case 0:  // $0-999 sales
  case 1:  // $1000-1999 sales
  case 2:  // $2000-2999 sales
  case 3:  // $3000-3999 sales
    commission = <some number here>;
    break;

  ... etc ...
}

That being said, this assumes that "have to use a switch" is part of a school assignment or similar. As the other people have mentioned, you're better off using if statements with a range (e.g. if (sales >= 0 && sales <= 3999)) than using a switch for this kind of thing.

2 Comments

The assignment had us first do it using if statements and then with cases. The if's was much easier.
I figured as much. In reality you wouldn't use a switch for this kind of data (switches are generally limited to a specific set of values), but it's a useful learning exercise :)
0

If you need dynamical creation of your ranges w/o hardcoding, you can use this snippet

struct BaseSalary
{
    int requiredExperience; // years in company
    int amount;             // salary amount
};

struct SortCondition
{
    bool operator()(const BaseSalary& lhs, const BaseSalary& rhs)
    {
        return lhs.requiredExperience < rhs.requiredExperience;
    }
};

struct SearchCondition
{
    bool operator() (const BaseSalary& s, int exp)
    {
        return s.requiredExperience < exp;
    }
};

// Fill base salary list in any appropriate way

BaseSalary s1;
s1.requiredExperience = 3; // [0 - 3]
s1.amount = 3500;

BaseSalary s2;            // (3 - 7]
s2.requiredExperience = 7;
s2.amount = 7000;

BaseSalary s3;              // (7-10]
s3.requiredExperience = 10;
s3.amount = 11000;


std::vector<BaseSalary> salaries;
salaries.push_back(s2);
salaries.push_back(s3);
salaries.push_back(s1);

// Sort salaries in ascending order of experience

std::sort(salaries.begin(), salaries.end(), SortCondition());


// Get base salary based on experience

int exp_to_search = 5;

std::vector<BaseSalary>::iterator it = std::lower_bound(salaries.begin(), salaries.end(), some_exp, SearchCondition());
if(it == salaries.end())
{
    // > 10 years
}
else
{
    int amount = it->amount;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.