Hello, and welcome.
In this video, I want to provide a demonstration of the solution of previous challenge.
Basically, a solution to a program written in c
that will print the perimeter and area of a rectangle.
So I'm going to start code blocks up, and I'm going to first create a new project.
I'm going to get a meaningful name. I think I'm going to name it rectangle,
and it's going to be put inside of our
regular directory path. I can create a new project by just going up to file new
project.
I'm going to select the console application that means it's just output
to the console to the screen, it's not a graphical application or anything.
I'm going to hit go. I'm going to select c
standard stuff. I'm going to name this project rectangle.
It's going to create a project file for me. It's going to put it in my usual download area.
I'm going to hit next, and I'm going to create debug and release configurations.
And I'm going to say finish.
This is going to auto-generate a c source file for me called main.c.
I'm going to go ahead and use this. All this does is put template code in there
to print out some information, I don't need to do that.
I am going to keep the two standard includes.
I could add comments at the top of the file, this is good practice.
For this demonstration, I'm not necessarily going to do it. But if i want to add a multi-line comment,
I would just put in you know my author, typical things author.
And I'll do it, I guess, for learning purposes here.
Purpose of
this program is going to
calculate the area of
the area of a rectangle.
And then I could put in the date as well, but i want to end that
multi-line comment with an asterisk forward slash.
You'll see my two include directives here on lines 5 and 6.
I'm including the standard io for input, output
because I want to use the printf function.
Standard libraries also includes because it has other important stuff in there, but
we may not use it. So this program is going to calculate the area.
And I mentioned in the requirements that we need to create some variables.
In order to create variables, we have to specify the data type
and we also specify the name of the variable and then we also may want to initialize them.
We need to create variables for the width and the height, and they should be of type double.
So the way that we do that is we just say double, that's a data type.
We say name of our variable we want to get a meaningful name, so we're just going to say double width
for our first one.
And we're going to initialize that to some value, anything we want.
We're going to say 32.5, 32.3. We're going to end it with a semicolon.
We're going to create another variable for the height.
Here we're going to set another value to it.
The way that we can assign values to variables is using this equals operator.
That's assigning a value to a variable.
Here we'll just say 15.3.
Now remember, we could create these variables on the same line actually,
but we can't initialize them on the same line. I could put a comma here and say height,
but they won't be initialized both to that value. I would have to initialize them on another line.
So I'm just going to create both variables on separate lines and
assign them initialize them on the same line as well.
I could override these later and actually
assign different values if I wanted.
But I created to my two variables. I also have to create variables for
my perimeter and area.
Now I don't know what these are going to be equal to yet because they're going to be calculated.
So I'm just going to create these and I'm going to initialize them to 0.
And we could say 0.0 since it's a double.
The same with the area.
I'm going to also initialize this to 0.0.
It's always good habit to initialize all of your variables.
Now that
Now that we have our variables, we have to actually do our calculations.
We haven't learned fully how to use operators yet,
but it's very easy to do mathematical equations operators in a program.
You use the traditional operators that you would use in any math class.
In this case, I want to assign values to perimeter
for a calculation. So I'm going to say perimeter
and I'm going to assign it using the equal symbol.
And I'm just going to say the perimeter is going to be equal
to 2.0 times using the asterisk
and then in parentheses because I want to have precedence.
I'm just going to say height plus width.
And then I'm going to end that statement with a semicolon,
that's assigning new data to our perimeter variable,
mainly 32.3 plus 15.3 times 2.
And I'm going to assign area equal to
the width times the height.
That's how you calculate the area of a rectangle. Use the asterisk for multiplication,
and you just specify the variable names.
So width and height are variables that we want to multiply.
The last thing I want to do in my program is I want to display this output to see if it's correct.
I can use the printf function to display output.
And when I want to display the value of variables, I have to provide format specifiers.
But remember, the printf function first takes this first argument.
Anything inside double quotes will be displayed to the screen.
So here, I just want to say
and we could print out all three variables or we could just print out the perimeter.
Let's say perimeter
is and then after the is I want to print out the value of the variable.
So I have to use the format specifier.
We can use a format specifier by doing a percent symbol.
After the percent, we have to specify the type of the data.
Here, we're displaying doubles which are floats, so we can say percent f.
And then the data that we want to display has to be separated by a comma
and then the name. Here, we just want to display the perimeter,
then we want to end it with a semicolon.
We also want to display the area. So we're going to use printf again.
Inside here, we're just going to say area is.
And again, we're going to say percent f for a variable.
And then we're going to have the comma we're going to say area.
And this is basically the program. Now you definitely want to
make changes, do different things.
And last line here we're returning 0, we'll explain that later when we talk about functions.
But for now just know, it's standard. So now I'm going to hit ctrl s or I'm going to save all.
I'm going to go up and build this. Hopefully, there will be no compile errors. There isn't.
Now I'm going to run it. The one thing
that I didn't add were line feed. So it's going to be difficult to read this.
So let me use an escape sequence to say backslash n.
After I print out perimeter is, I want to add a line feed.
Remember, escape sequences are actions that are
interpreted by the compiler to do something
and they're represented through these weird character sequences.
In this case, backslash n represents a line feed, backslash t would represent a tab.
So let's resave build it again.
And this time let's run it and the output should be formatted a bit better.
So when we run it, what we're going to see here
is perimeter is 95.2000,
area is 494.19000.
And that's these two calculations and it looks correct because
2 times 32 plus 15.3 that would be 2 times
47.6 which is
95.2. So that's correct.
Width times height would be 32.3 times 15.3.
I can't do that in my head. But if I put a calculator, I'm sure it'd be 494.1900.
You also notice they're printed on separate lines because you use the backslash n
and they're formatted nicely.
Now say I want to print out more than one value of a variable,
say I want to print out width and height.
I can say perimeter is and I can actually add more format specifiers. I can actually say
width is
and then add a format specifier for
the width and then put a comma.
And then say height is
and then put a format specifier. So for each percent f,
we have to have a comma outside of the double quotes to represent that variable.
So the first percent f is going to be a width, so that has to be my first one.
The second one is going to be the height, the third one is going to be perimeter.
So hopefully, this makes sense. See what I did here. I'm printing out multiple variables on a single printf.
And so you can put multiple percent.
They just get mapped to whatever the commas are.
So percent f is going to be mapped to width,
the first percent f. The second percent f is going to be mapped to height.
The third one is going to be mapped to perimeter.
And now again, I save this and I build it.
And for output, we should now see,
in addition to perimeter being displayed, also the width and the height.
So when I run this,
we'll see that the width is displayed as 32.300,
height is 15.3000,
perimeter is 95.2. So we printed three variables.
Maybe we want to get rid of some of those 0s.
We could actually add in .2,
percent .2 to format it to two decimal spots let's see if
if this will work.
So let's see what this does. The height and the width I want it to be more precise,
compiled. So let's see if it runs.
And when I run it, it actually displays 32.30,15.30
because I specified percent
2 dot 2f, that's only displaying it to 2 decimal points.
So you can play around with these format specifiers.
And this is the basics. We created some variables.
We defined the data types for those variables.
We assigned data values to them using the equal symbol,
did some calculations and displayed the output.
Now if we were getting really
ambitious, we could actually instead of hard coding these to 32.3,
we could add in some parameters to our int
and pass data.
And then we'd have to do something like this,
and we'd have to actually assign what was ever passed in.
We'd actually convert it to a double and we'd have to do something like this actually,
convert it to a double first.
But this is getting more advanced for what we're learning here. But this would
actually take in two parameters and assign them.
So be a little different, we'd have to then select our project
and we'd have to set the program arguments. Here we could just
specify them being is like 44 and
28.
Now let's see what happens here. Now we build and we run.
It should use 44 and 28
for our calculations. And it does use 44 and 28,
and it does a different calculation there.
But that's getting ahead of ourselves.
Let's just not use program arguments, but you can verify that's how to do it
and just assign hard-coded values to whatever we want again.
And I'll provide this source and zip file
to you so you can take a look at the source code if you're confused.
We should also probably providing single line comments in here.
We could say something like declaring variables.
Again, comments are nice because they
help document the code. We could say performing
perimeter
calculation and then the last one could be displaying output.
Again, a lot of this is intuitive.
But if somebody's completely new, they might get confused so you provide the comments.
So a lot of things that again we focused on here. Hopefully, this helped
with your understanding of variables, data types,
using printf to display variables, format specifiers
and escape sequences. Thank you.