C++:Problem13

Problem:
Write a C++ program that will prompt for and input the side of a square and output a picture of the square, the area of the square and the perimeter of the square. A sample run is below.

Enter a number between 0 and 20: -3
Sorry, the number must be between 0 and 20.
Enter a number between 0 and 20: 4
Here is a square with side 4:
* * * *
* * * *
* * * *
* * * *
Its area is 16 and its perimeter is 16.
Required: Your program must have 4 functions besides the function main(). It must have a function that prompts for inputs the side of the square, a function that calculates the area of the square, a function that calculates the perimeter of the square, and a function that outputs the picture of the square.

Objective:
• To practice writing programs with functions.
• call-by-value
• call-by-reference

Display:
Enter a number between 0 and 20: -1
Sorry, the number must be between 0 and 20.
Enter a number between 0 and 20: 22
Sorry, the number must be between 0 and 20.
Enter a number between 0 and 20: 5
Here is a square with side 5
*****
*****
*****
*****
*****
Its area is 25 and its perimeter is 20

Code:
#include
using namespace std;
//function declaration
void input_side(int& lenght );
//purpose: none
//preconditon: input side of a square
//postcondtion: none

void display_square(int lenght);
//purpose: to display the picture of the square
//preconditon: input side of the square
//postcondtion: return the display of the square

int area ( int lenght);
//purpose: to calculate the area of the square
//preconditon: input side s of the square
//postcondtion: return the area of the square

int perimeter( int lenght);
//purpose: to calculate the perimeter of the square
//preconditon: input side s of the square
//postcondtion: return the perimeter of the square

int main()
{
//Input variables
int side

//Prompt the user to input
cout << "Enter a number between 0 and 20: ";
cin >> side;

//Using while loop that ask the user to enter the input again if the user
//failed in inputting it.
while (side > 20 || side <= 0)
{
cout << "Sorry, the number must be between 0 and 20.";
cout << "\nEnter a number between 0 and 20: ";
cin >> side;
}
input_side(side);
display_square(side);
cout << "Its area is "<< area (side)
<< " and its perimeter is "
<< perimeter(side) << endl;

return 0;
}
// Function Definition
void input_side(int& lenght)
{
cout << "Here is a square with side " << lenght << endl;
}
// Function Definition
void display_square(int lenght)
{
int i=0, j=0;
for(j=0;j{
for(i=0;icout << "*";
cout << endl;
}
}
// Function Definition
int area (int lenght)
{
return lenght*lenght;
}
// Function Definition
int perimeter(int lenght)
{
return lenght*4;
}