Hello, and welcome. In this demonstration, I want to provide a solution
for our last challenge. So I want to provide a solution
to our c program that is taking a number of minutes
and converting it to what that representation is in days
and how many years that is.
Not necessarily practical program,
but it will demonstrate a few key concepts
that you guys can learn mainly using arithmetic operators,
getting input from the keyboard.
And also you do have to use the cast operator for this which
I might not have mentioned in the challenge, but
hopefully you guys figured this out because when you do the --
don't need to use the cast operator
you shouldn't need to initially I was thinking a solution needed a modulus operator. But
if you do a modulus, it only takes
2 ints I think.
So anyways, let's get started. Let's start up cold blocks,
and let's create a project, go through the basic stuff that we're pretty good at by this point.
So I'm not going to go into it in detail, you can just watch.
But I'm going to create a new project and it's going to be a console project
and definitely going to be c
and what I'm going to name this one is again you always want to get good meaningful names.
I'm going to say minutes conversion.
So it's like a minutes conversion app.
And now I'm just going to modify our main.c file.
So let's get rid of this. And we're going to keep the include statements.
And like always, you'll want to add comments, make sure you add the comments,
just because I don't do it doesn't mean you shouldn't do it.
It's standard practice. It's just for efficiency and time purposes, I leave it out.
What I like to do at the very top of the
program is to clear my variables that I need to use and then initialize them.
So let's start off that way. I'm going to create an integer variable for the minutes,
the user is going to enter. And I'm just going to call that minutes entered. I'll initialize it to 0.
I'm going to create a double for years. This actually could be a long as well
depending on how much calculation you want to do because you could actually
get into a pretty big decimal point number.
So long might even be a better option. But I'm just going to use a double for now,
initialize that 0.0
and days equal to that 0.0.
And now I want to get input from the user.
In order to get input from the user,the first thing you do is you have to tell the user to enter the input.
So I'm going to display some output saying
please enter the number of minutes.
And it's going to put a colon there and a space and
end it with a semicolon.
Next is I'm going to actually get the input.
Here is where you'd probably say get input from user as a comment.
How do we get input from the user? We use the scanf function.
So remember this from a while back, just say scanf, similar to the printf.
And the first parameter though argument
is the type of data that you want to read in to convert.
Remember, everything is read in as a string or text.
So you have to convert it to how you're going to store it.
So you put it in double quotes just like the printf. You use a format specifier.
Here. I'm going to just say I want to store it as a decimal.
So I'm going to say d an integer,
not a decimal integer, sorry.
And then the next argument is what variable you're going to store it.
In I'm going to store it in minutes entered.
Now remember that scan f
and I actually just have scan there, scanf
for every single data type except a string,
you have to provide the ampersand. The address sub-operator.
So we have to provide that. We're reading in a number so we have to do the ampersand.
Remember that from the lecture. Then I want to add this with a semicolon.
And at this point, you should probably test.
Make sure you can get input from the user. And you probably just have a printf to display what you entered.
But since I know that this is correct, I'm going to move on.
I'm going to need to do a calculation on line 15.
Mainly, I want to store the minutes in years.
So what I'm going to do is create another variable that I forgot about
which will store the minutes in year.
Because this is going to make it easier. So here I'm just going to say minutes and year.
You'll notice that the convention I'm using here for naming variables is camelCase.
It's where you have lowercase
for the first ward and then every word after that you uppercase the first character.
I didn't explain that earlier so I thought I'd mention it.
So the first calculation I want to set my minutes and year
because this is how I'm going
to calculate my years. So minutes and years actually going to be equal to --
and I like to use parentheses just to organize,so everything's clear. It makes it more readable.
It's going to be 60, because 60 is the number of
minutes in an hour. And I'm going to multiply by that by number of hours in a day which is 24.
I'm going to multiply that by the number of
days in a year, which is 365.
And so this calculation is going to give me how many minutes are in a single year.
And I can now use this to calculate my years.
So here on the next line, I can say years equal -- so we're using assignment
operators here, the equals.
And then what we're doing in those parentheses on line 16 is we're using the asterisk
which is a multiplication. So pretty neat.
Years is just going to be equal to -- and again I want to use parentheses.
The minutes entered which was done by the user
divided by the minutes in the year.
So if you entered 5000 minutes and there's 20000 minutes in a year,
5000 divided by 20000 is going to give you how many years there are which would
be 0 point something.
To calculate the days,
here I'm just going to
say minutes entered
divided by 60 and 60.0
because there's what there's
60 minutes in an hour.
And then once i have the hours, I can then divide that
by the number of days which is 24.0.
And I could have put this in another calculation if I wanted to.
But again, I want to make sure that we do this in the correct order.
So first I'm going to divide minutes enter divided by 60.
You don't have to worry about this actually because
we're doing division here, so the associativity will be left to right and I'd actually
don't need these parentheses. But I always like adding parentheses because it makes thingsmore readable.
So let's add that last one.
That's kind of redundant. I don't even need one here. So let's just leave it out,
only the first ones. And this should display our days.
Now that I've done my calculations and use some of the operators.
I want to do a printf to just print out my data.
Here I'm going to say print out the minutes first, percent d minutes
is approximately
percent f years and percent f days.
And then I'm going to add a line feed.
So what this is doing is we're using printf to display output of variables
and the percent symbols remember our markup specifiers.
The first thing I want to print out is minutes, so I have to put minutes after my first comma when it's entered.
The second one is the year so I want to say years because that's percent f years.
And the third is days
something like that.
I should now compile and run, and they should do my conversion for me.
So let's go ahead and build. I got no mornings, no errors.
Let's run and I'm not even sure
how many minutes are in a year. So I'm not sure how to test this,
but let's just put in 5000 minutes to start with.
When I run the program, it asks me to enter the number of minutes, I'm going to put 5000.
And if you didn't put in a number, you might have a crash because
you're converting it to a number in your scanf function.
I hit return, this says 5000 minutes is approximately
0.009513 years
and 3.47222 days,
which seems to make sense because what 60 minutes in an hour times 24,
that's going to be roughly 2400 or so for one day
so 2400 and 2400,
no 60 times. I'm sorry 60 times 24
is going to be roughly 1200.
So 1200 goes into 5000 how,
many times for each day probably about 3.4 around there.
So roughly this looks to be correct.
So you can run this multiple times, put in some big numbers what you're going to notice though.
We're only storing doubles here. So if you run it and you try to put in a too big of a number,
say like 600 billion or something,
you may get incorrect --
so you may have incorrections here. So this is showing you the data types problems.
So I'm storing in an int. Int is only 8 bytes. I entered this huge number.
That can't be stored in it. So I when i go to print it out,
it's printing on a completely different number, 184298154.
That's because what I'm entering is bigger than an int.
So you may want to consider if you wanted this program to be even more
precise and you want to enter larger numbers to change some of the data types,
change the ints to doubles or even the doubles to longs.
So this is saying 350 years and 127000 days or so.
So a nice little conversion app that, again, tries to
provide you with some understanding of
data types as well as operators.
We could have a lot of different conversion apps. Another nice app that
you that you want to write for more practice would be Fahrenheit
to Celsius app.
Write an app that allows the user to enter Fahrenheit and then convert that to Celsius.
I mean there's many, many different scenarios you come up. But this will force you to use the operators.
And we could also even do a casting here
if we wanted to. If we didn't like that the days was too precise
and maybe we want to change the double to an int.
And we want to make it a little less precise. We could actually say
cast this to an it by just providing the parentheses
and then doing int.
And this would actually take that number for days.
And if it's decimal 3.549 or something, it would convert it to 3.
Maybe that would be better for the user, up to you.
But let's keep it for now as a double.
But that's how you learn. You play around with the program, try different scenarios,
try different input, make sure that everything's correct. Thank you.