Hello, and welcome.
We have another challenge for you.
This challenge assignment is going to test your ability,
your understanding of data types and variables more or less.
What I would like you to do
is I want you to write a program
that will display the perimeter in the area of a rectangle.
And so the actual requirements for this
are you're going to need to create four variables of type double.
You're going to need to create a variable to store the width of the rectangle
a variable to store the height of the rectangle,
a variable to store the perimeter of the rectangle and a variable to store the area of the rectangle.
So we should understand how to create variables and then
also how to initialize and assign variables by now using the assignment operator.
But the first thing we need to do is create the variables.
So you need to have four variables of type double.
You're going to need to create a c project, which we'll get to in a second.
But also the program needs to perform some calculations.
We have not actually learned how to do calculations yet
because we haven't talked about operators. So I'm going to give this code to you.
It's pretty simple to create calculations in a c program.
You just use operators like mathematical operators.
So for example, in c, if you want to add two variables together, use a plus.
If you want to multiply two variables against one another,
you use an asterisk.
And so to calculate the perimeter of a rectangle,
you have to actually add the height and the width
and then multiply that by two.
It's very easy to do. Like I said, I'll give you this code.
The main focus of this challenge
is for you to understand how to create variables, how to use them and how to define data types.
Area is also going to need to include a calculation.
Here, you're just going to multiply the width times the height using the asterisk.
I didn't want to just create a simple challenge that didn't make sense.
So I wanted to make it more relatable. And so I thought a rectangle perimeter and area.
That's pretty good, has some calculations in it, but you won't have to worry about that.
So also
what you need as the output for this program is you're going to display the height,
the width and the parameter variables in the correct format.
And so the other thing that you're going to learn through this challenge is how to use the printf function
to display the value of variables.
So you're going to need to understand format specifiers,
you're going to understand an escape sequence for the backslash n,
so you print out line feeds.
So the first display should be the perimeter.
The second display should be the area with the height and the width as well.
So for some tips for this assignment or hints,
you're going to need to create a project in code blocks.
You should use a meaningful name for this project.
Since this project in this program is
basically creating the perimeter and area of a rectangle,
you could name the project rectangle.
That's really the simplest thing.
When you create the project in code blocks, remember that by default,
the code blocks is automatically going to create a c source file for you.
It's going to be named main.c.
You can go ahead and use that for this project. You don't need to delete it.
In our first challenge, we deleted it because I wanted to make
make sure you know how to create your own c source files.
But in this case, use the advantage of them already creating it for you.
So main.c is just going to be an empty template with a main function in it.
Remember, the main function is the entry point.
After you have the main.c file, you're going to need to edit it.
And as I mentioned, you need to create some variables
and the variables have to be of type double.
So you should at least create a couple variables for height and width
of type double and you should initialize them.
Initialize means that you set an initial value to them.
You can initialize things using the equals operator, you end it with a semicolon.
You'll want to initialize this to anything you want, something useful.
We're not getting input from the user for this program,
but you could if you wanted to. If you felt comfortable, you could pass in command line arguments,
but I didn't think that you guys are ready for that yet.
So you can just hard code the variables to anything 50, 100,
23.5, 92.7.
Remember, they're double so they can contain decimals.
But you can just hard code these variables to a particular value.
And they should be something other than zero, so you won't see decent output.
As far as the other variables, the perimeter and area values,
those are going to be calculated,
so you don't want to assign any data to them hard coded.
You can just initialize them to 0.0.
And then later inthe program, you can assign values to them based on your calculations.
So here is the code that you would use for the perimeter calculation and the area calculation.
If you created two variables of type double named perimeter and area,
all you would need to do is say equals to calculate the perimeter 2.0 times
height plus width inside parenthesis ending with a semicolon.
To calculate the area, you would just assign area,
variable, use the equal symbol,
equals operator width and then the asterisk for times and then height and then the semicolon.
So you can copy and paste the perimeter and area of things right here
in your program after you've declared and initialized all your variables.
This is really the only logic of the program,
just do a couple calculations.
After we've done those calculations, you then want to display
the value of the two perimeters or two variables,
perimeter and area and also height and width you should display as well.
So you're going to need to use the printf function,
and you're going to have to use the correct format specifier to display the required output.
Format specified for doubles, I mentioned,
is %f, so you want to use that.
You can play around with it as well and use like
%f2 to see how the precision changes for the number of decimal points.
And again, use meaningful names for everything.
After you're done doing that, you should compile and link and then test, run the program.
And again, play around with this program, try different things, try changing
variables
data types, try printing out
different variables, using different data type format specifiers.
If you're feeling really, really ambitious, add command line arguments to the program
and the command line arguments could be the width and the height.
Read those in from the main function and use those to do your calculations for perimeter
and area.
And again, in our previous thing I gave you the code, width and height,
I'm assuming that you created two variables called height and width,
all lowercase or else this code won't work,
and those are common names for this
actual functionality.
So that's about it. Again, I'll provide another demonstration
of how you complete this program along with the source code,
so good luck.
And lastly, also don't forget to analyze the output
to confirm it's correct. If it's incorrect, you have to go back,
do your editing, do your saving and do your rebuilding.