Hello, and welcome.
I would like to provide you with a solution to our
challenge on calculating the weekly pay.
So I’m going to provide a nice demonstration here. Bring up code blocks,
and let's close our project that we currently have open
and open up and create a new project for
this application.
So I’m going to create a console application so I’m going to be in c.
And I’m going to name this calculate weekly pay.
So I’m going to have my main.c file
which is going to give me some nice starting point.
But there's a couple things I’m going to do in this solution that may be a little different.
But it's going to introduce you to a couple concepts.
We'll talk about them, but we're going to be using a lot of constants in this program,
mainly for our pay rate and our tax rates.
So I’m going to use something that we haven't seen yet which is called the define.
It's a preprocessor directive to create constants.
And so the way that we know it's pre-processor directive is we have the pound symbol.
So I’m just going to say pound define. This is a way to create constants.
I’m going to create 5 constants for all of our
data in this program. We have a pay rate which is 12.00.
So the way the define works is use the defined keyword
followed after the pound and then usually you provide a name,
usually it's all caps. That's standard convention and then a space and then the value.
No equals assignment here. And this is creating a constant variable essentially.
So we're going to create one for pay rate, tax rate for 300.
Actually we'll say tax rate for 150 or for 300 and under
and that's going to be a tax rate of 0.15.
You'll notice that the data that you assign can be floating points integers,
you can even assign strings. It doesn't matter the type of the data here.
Tax rate for 150
can be 0.20. and I usually do the defines outside of the main at the top of the file.
And also don't forget to add your comments at the top and throughout the source code.
And then I’m going to create a last tax rate for the rest
which is 0.25.
Last thing I want to do is I’m going to create a constant for my overtime,
just going to call overtime and say 40. This will just represent
when we want to check for greater than 40.
It's never a good idea to have magic numbers
inside variables because now
if anything change say the overtime change to 45, I only have to change it in one spot.
right at the top and my program is going to work the exact same.
if I didn't have this constant
and I used a magic number in my comparisons or my variables,
I’d have to change it everywhere I’m using 40.
So get in the habit of using constants and not using something called magic numbers.
I also like to declare my variables right at the top of the main.
These are local variables to our main function.
So I want to put a comment declaring variables.
The variables I need that are locally I’m going to create an integer for hours,
I’m going to initialize that to 0.
The hours is going to be entered by the user,
and then I’m going to create some doubles that are going to be displayed as output,
mainly the gross pay which I’ll initialize to 0.0 because it's a double.
And then taxes, I’m going to also initialize the 0.0
ending with a semicolon, don't forget the semicolons and then the net pay.
Because these are my 3 outputs that I want.
So I’m going to create my 3 outputs at the top along with my input hours.
First thing I want to do is I want to get my input, so I’m going
to tell the user to enter something. I’m going to say enter
say please enter the number
of hours worked this week,
put a colon, little space and a semicolon.
Then I’m going to use my scanf function getting the input.
I’m just going to say scanf. I want to read an integer. So I’m going to say percent d.
I’m going to put a comma, and I’m going to do hours and then
because it's not a string or characters,
I have to put the ampersand in front of it and end it with a semicolon.
That's going to read in. And at this point what, I should do is I should test,
work ismall chunks and test constantly.
if I keep writing code try to compile, I could have a problem anywhere,
work in small areas.
But I know this works. So I’m going to move on to demonstrate how to do this.
Like I said the first thing I’m going to do is I’m going to calculate the gross pay.
In the logic here, we'll need an if statement.
Basically, if the hours is greater than 40,
we want to have an overtime rate.
So what I’m going to do is I’m going to say if the hours
are less than or equal to 40,
then all I have to do is create my calculation
very easily because it's just rate of pay times hours.
So we have our if keyword on line 24,
and we have our hours.
We're doing comparison. We're using a relational operator,less than equals. If ours is less than or equal to 4,
we're not going to use a bracket because we're going to have to use a single statement.
We're just going to say gross pay. We're going to set a variable
it’s going to be equal to hours times our constant, pay rate
and that's it.
Now if it's greater than or equal to 40,
we want to do something else. So if that condition evaluates to false,
we want to have an else, and this is where we use the else.The else is going to have multiple
lines in its block, so I’m going to use a bracket, it's going to start
on line 27 with the starting bracket 29 with the ending.
Mismatching your brackets or not having brackets are a common error that your compiler may find.
So make sure you match those brackets. The IDE is very helpful because it highlights them.
If you have multiple brackets, you can always go to another.
If you go to the bottom bracket, it will highlight telling you where the starting bracket was.
So on this tape, if it's greater than 40,
first , I want to set my gross pay equal to 40 times the pay rate
because those first 40 hours are going to be 12 dollars an hour.
And then everything after that is going to be more.
So I’m going to create a local variable called overtime pay,
and I’m going to set that equal to the hours
minus 40. So if I typed in 40 it'd be 5 here.
and then once I have those hours I want to multiply that from the pay rate
which is 12 dollars an hour,
but I want to add 1.5 of that, which would be
18 dollars an hour,
then I end it with a semicolon.
at this point my overtime pay is going to be 5 times 18,
90 dollars if I typed in 45 as a user.
and then the first 40 hours are just going to be 40 times 12
which is
what we say 570. if we did 40 times 12 would be
480.
Now the last thing we have to do is we have to add the gross paid to the overtime pay.
So I probably could have used a better name than gross pay because we don't have our grace pay yet.
but I’m going to use a nice little operator,
our assignment operator plus equals,
which is going to do our overtime pay.
what this is saying is gross pay equals gross pay plus overtime pay.
but we're doing it in shorthand notation.
So now we have our gross pay, and we're done for gross pay.
So we would go ahead and test this, print out the gross pay and make sure it's correct.
but since I know this works, I’m going to move on to the next step.
the next step is to calculate the taxes.
So to calculate the taxes, remember, we have to do certain things
depending on how much money you make. So we're going to have to look at the gross pay.
And so if the gross pay is less than or equal to 300,we're going to do something.
if the gross pay gross 300 and 450,
we're going to do something. and if it's greater than 450, we're going to do something else.
we will have a little duplicated code here.
it's okay for now because we're just demonstrating if statements.
you might be able to write this a little cleaner.
but what I’m going to do is I’m going to say first check the first condition where if the gross pay
is less than or equal to 300. and in that case,
it's very easy to calculate the taxes.
the taxes are just going to be gross pay
times the tax rate for 300.
the tax rate for 300 remember
is anything from 300 another is taxed at what 15% I think it was.
So that's all we have to do if you made less than
or equal to 300.
Again, the other thing you should notice here is the indentation.
whenever you have blocks of code, and even though I only have a single statement, I’m going to put a
bracket there.
and then I indicate my blocks, makes it much easier to read,
don't forget about indentation in new lines.
and typically here you want to have space as opposed to tabs, but that's another discussion.
now it can also be something else.
So on line 34 we asked the question
if the gross pay is less than or equal to 300, do something on line 36.
we use the if keyword, we put the condition Boolean expression inside parentheses.
but what if it's not 300 or less, then
we want to do something else. Specifically, if the gross pay
is greater than 300
and so here I’m using a logical operator ampersand ampersand
where I’m asking essentially 2 questions. if the gross pay is greater than 300 and
the gross pay is less than equal to 450,
then I want to do something else.
So what's this saying is if my gross pay is anywhere between 300 and 450.
now if it's between 300 and 450,
I first want to calculate my taxes
just like I did before.
for the first 300, I want to multiply it by the tax rate for 300.
because the first 300 are taxed at that rate.
for the other amount, I want to
do the tax rate at 150, which is point 20%.
So first, I have to find out
how much we made over 300,
but I can actually do this in one line. I’m going to do the taxes
plus equals, so this is saying taxes equal taxes,
which are 300 times the tax rate 300 already,
taxes plus something. and what I want to add to that first
taxes on line 40 that was calculated,I want to add the additional amount
and it would be the gross pay
minus 300
and I've already determined that I’m not above 450.
So gross pay minus 300 times
the tax rate for 150.
So see what I did there.
if I made anywhere amount of money between 300 and 450 on line 38,
then I want to get the taxes for the first 300, which is 300 times the tax rate 300
and I want to add that to how or much my gross pay was over 300
times that tax rate for 150.
so now we've met 2 conditions, the last condition we want to check is if we made more than 450.
So basically, we can just have an else if gross pay is greater than 450
or really just an else.
but for clarity sake I’m going to put the else if in there,
and I’m going to say else gross pay is greater than 450.
I don't really need to do this because I can just have an else without an if,
but I want to demonstrate the else if.
in this case, I do want to calculate the taxes for the first 300,
so I can copy code here.
I also want to calculate the taxes
for the 150, so I’m going to do a plus equals
for the next 150, so 300 to 450. So this will be 150
times tax rate 150.
And then last thing I want to do is I want to tax the rest at a certain rate.
So here I’m going to say taxes plus equal, again plus equals is taxes taxes plus.
and here I’m going to say gross pay
minus 450 because I don't want to
apply the taxes to the entire pay,
just what's above 450.
and I want to multiply that by tax rate equals rest,
which is our constant variable we defined above.
and that's it. I’m done calculating the taxes.
and again, you would test this after each step and make sure it's correct
compile and test.
but you'll notice the indentation. this is easy to read. I’m doing comments, all of that.
Last thing I want to do is calculate the net pay.
this is going to be simple because the net pay is just going to be equal to
the gross pay minus the taxes,
very simple.
and then I want to display my output.
and remember the requirements were to display the gross pay,
the taxes
and the net pay.
So we can use printf here.
and I’m just going to copy and paste this in for efficiency sake, so we don't take up too much time.
Sometimes students get mad when I copy and paste,
and they say you're not supposed to copy and paste in programming. Well, that's true to a degree.
if you copy and paste something and you fully understand that code, it's more efficient.
the problem with copying and pasting is a lot of developers copy and paste,
they have no clue how that code works, and then they're just hoping it works.
but in this case, I know how the printout statements work and I just want to show you what's happening here.
So I don't have to wait waste time typing in the keyboard.
So on line 54, we're going to display the gross pay.
it's a float. we know how to display value of variables.
and the only thing peculiar in that is I’m formatting the output to 2 decimal spots,
so you should notice the %.2f
and then the backslash n is a line feed for an escape sequence.
So I’m formatting everything to 2 decimal spots because when we display money,
we have cents and we don't have a percentage of cents.
and then I've displayed the taxes same format and the net pay.
So pretty nice there.
and again, what you should notice in this program is the indentation.
it's very easy to read. I’m using constant variables at the top.
I’m initializing my variables. why I declare them?
everything's nice and neat. I’m providing comments. I’m
writing a bunch of if else statements.
Hopefully, this was all clear. the logic is not too tough to follow, but when
you do have the source code with you because you can download
it so you can analyze this more. but let's see what happens when we build and compile
like let's make sure nothing's wrong.
When I build it says I have a warning unused.
It looks like here.
I have missing something expected a semicolon before a bracket that doesn't make sense.
have missing something expected a semicolon before a bracket
that doesn't make sense. So I think what I’m missing here is,
well, I can't have an else. it has to be else if,
it doesn't make sense, I forgot the if. So else if.
see what happens there is I had an else and then below that I had an else if.
that doesn't make sense. you can't have an else and then an else if.
So now let's see what happens here.
now I get 0 errors and 0 warnings.
let's run this
and my virus scanner.
when I run this, first thing it's going to do is it's going to say please enter the number of hours work this week.
I’m going to put in something higher than 45, so it demonstrates overtime.
I'll say 45 or 4 higher than 40. 45 and it says I made 570 dollars,
which is right because. everything for up to 40 dollars is 12 dollars
an hour which is we calculated to be 400 and 80.
and then the 5 hours are 18 dollars an hour, which is 90 plus 480 is 570.
the taxes this week were 105 dollars, and the net pay was 465.
nice even numbers. you can go through those calculations to make sure they're correct.
you can try running this many, many times and seeing different data output.
let's run it with something less than
40. let's just say 30 hours this week,
I’m not going to get any overtime pay. it's just going to be a straight 30 times 12
which is 360.
For the first 300, I’m going to be taxed at a rate of 15%.
and that's going to be 45 dollars
and then for the 60 dollars, I’m going to be taxed at a rate of 20% and that's going to be
12 dollars. So 45 plus 12, 57 that looks correct.
and the net pay this week is 303, 360 minus 57.
So it looks to be working pretty good.
you'll want to test this more with earnings greater than 450.
the other important thing in testing is you want to test all the boundaries.
you don't just want to provide one test. everything works.
you want to provide good cases and bad cases.
So in this case, this example, we have many different cases for this program
we have a case where you make more than 40 hours.
So you have overtime pay. we have a case when your earnings
is 300 or less, a case when your earnings is 300 to 450
and a case when your earnings is greater than 450.
So for this program you should at least run
4 test cases or 5 test cases probably,
it depends because you really want to calculate the overtime as well.
You could probably get away with 4 cases
because you probably make less than 300 dollars, less than overtime pay.
But what you really want to test is overtime and then the 3 tax rates.
I hope this was helpful. I hope you fully understand what I demonstrated.
I hope you understand all the stuff with the if statements and how I make
my code readable and use meaningful names. Thank you.