Switch cases and Array's

A few assignments back I made a leap year program with a switch case involved in which I divided the months into a monthName variable and the number of days into a monthDay (or something to that extent).....I figured out how to do that with switch cases, but now Im alittle stuck as to how to do a switch case with the current assignment I am working on. I have 3 (1 X 8) one-dimensional array's (Net Pay, Taxes Paid, and Gross Pay) that I want to to store data into. I have to create a switch for the taxes paid and have a chart to follow with numerical data, as followed....

0-100 is 0%
101-200 is 10%
201-300 is 20%
301-400 is 30%
401-500 is 40%
501-above(which I just named 999) is 50%

I know I have to convert the percentage of taxes to decimal format, but I really have no idea how to write the switch case code that will go into an array. I am alittle stuck with the situation, and could use some guidance.

The switch does not suite to this task. Switch uses equality comparison. You could have each value in 0-500 as separate case (and default for the 501-), but you don't want that.

An if..else[ if..else] chain does allow you to do use "less than" comparison.

Yet another method is to have the limits in an array and to use std::lower_bound to find element and compute the percentage from the element index.

Shortest is to compute a nice number and then use couple if-clauses to handle the edge cases.
We had to use only what we have learned so far, and we have not gone over finding elements. I originally used a line of if statements, but there's an added 25 points if we can only use 100 lines. I ended up using ternary statements instead. Also, I know include the std line is the proper way, but since the beginning of class we have learned to include using namespace std; instead of std::....
Could you show the if-statement version? (Or the ternary.)
1
2
3
4
5
6
7
8
9
10
for(int ctr0=0; ctr0 <= 7; ctr0++)
 {
    (Gpay[ctr0] > 0 && Gpay[ctr0] <= 100) ? TxRate = 0.00 : x=1;
    (Gpay[ctr0] > 101 && Gpay[ctr0] <= 200) ? TxRate = 0.10 : x=1; 
    (Gpay[ctr0] > 201 && Gpay[ctr0] <= 300) ? TxRate = 0.20 : x=1;
    (Gpay[ctr0] > 301 && Gpay[ctr0] <= 400) ? TxRate = 0.30 : x=1;
    (Gpay[ctr0] > 401 && Gpay[ctr0] <= 500) ? TxRate = 0.40 : x=1;
    (Gpay[ctr0] > 501 ) ? TxRate = 0.50 : x=1;
	      taxPD[ctr0] = Gpay[ctr0] * TxRate
}


Here you go buddy,
Gpay[ctr0] = Gross pay array
The conditions are self explained
Then I have a conversion that converts the Gross Pay to TxRate from 0% to 50% (hence the decimal format)...

about the if statement. It's pretty much the same....If you know nothing about ternary statements it's just a shortened version of using if and else statements.
That is kind of misusing the ternary operator. The ternary returns a value. For example, line 5 returns either 0.2 or 1. The 'x' does not seem to have any role, so why is it set at all?

If
1
2
3
4
5
6
7
8
9
10
11
for(int ctr0=0; ctr0 <= 7; ctr0++)
{
    if ( Gpay[ctr0] <= 100 ) TxRate = 0.0; // I presume that 0% tax is ok for negatives too
    // by now we know that Gpay cannot be <=100
    else if ( Gpay[ctr0] <= 200 ) TxRate = 0.1;
    // by now we know that Gpay cannot be <=200
    else if ( Gpay[ctr0] <= 300 ) TxRate = 0.2;
    else TxRate = 0.3; // for all Gpay > 300, but you get the idea

    taxPD[ctr0] = Gpay[ctr0] * TxRate
}

Same with ternaries:
1
2
3
4
5
6
7
for(int ctr0=0; ctr0 <= 7; ctr0++)
{
    TxRate = ( Gpay[ctr0] <= 100 ) ? 0.0 : ( Gpay[ctr0] <= 200 ) ? 0.1 :
                    ( Gpay[ctr0] <= 300 ) ? 0.2 : 0.3;

    taxPD[ctr0] = Gpay[ctr0] * TxRate
}

And then
1
2
3
4
5
6
7
for(int ctr0=0; ctr0 <= 7; ctr0++)
{
    int foo = (Gpay[ctr0] - 1) / 100;
    if ( foo < 0 ) foo = 0;
    else if ( 3 < foo ) foo = 3;
    taxPD[ctr0] = Gpay[ctr0] * foo * 0.1; // TxRate == foo * 0.1
}

Man, if i could give you a huge I would.....

I figured x would have no value at all, and I am pretty sure my instructor would of caught that too.....It's crazy really because the tutor I saw about this didnt even say anything about it. He actually recommended I use a ternary statement to make the code smaller.
Topic archived. No new replies allowed.