Write a program that reads a pair of numbers representing the width and height of a rectangle and prints its area an perimeter. Here is an example of how a console session with this program should look like:
$ ./rectangle
1 2
1x2 rectangle, area = 2, perimeter = 6
3 6
3x6 rectangle, area = 18, perimeter = 18
10 1
10x1 rectangle, area = 10, perimeter = 22
Tip: use control+D (^D) to simulate the end of file and exit the program.
Each line of input contains two numbers w and h where -1 000 ≤ w, h ≤ 1 000
For each line of input there should be one line of output in the following format:
<W>x<H> rectangle, area = <AREA>, perimeter = <PERIMETER>
Replace <W>
, <H>
, <AREA>
and <PERIMETER>
by
the width, height, area and perimeter of the given rectangle respectively.
1 2
3 6
10 1
1x2 rectangle, area = 2, perimeter = 6
3x6 rectangle, area = 18, perimeter = 18
10x1 rectangle, area = 10, perimeter = 22
area
and perimeter
functionsIn order to get a full score,
your program should be implemented using functions area
and perimeter
.
They should receive a rectangle
object.
Please refer to the information for your chosen language.
struct rectangle {
int width;
int height;
};
int area(struct rectangle rectangle);
int perimeter(struct rectangle rectangle);
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
...
def perimeter(self):
...
data Rectangle = Rectangle { width :: Int
, height :: Int
}
area :: Rectangle -> Int
perimeter :: Rectangle -> Int
Alternatively, you can declare the rectangle type as:
data Rectangle = Rectangle Int Int
class rectangle
{
public:
int width;
int height;
int area();
int perimeter();
};
class Rectangle
{
public int Width { get; set; }
public int Height { get; set; }
public Rectangle(int width, int height);
public int Area();
public int Perimeter();
}
public class Rectangle
{
public int width;
public int height;
public Rectangle(int width, int height);
public int area();
public int perimeter();
}
rectangle = {width: ..., height: ...}
function area(rectangle)
function perimeter(rectangle)
rectangle = {width = ..., height = ...}
function area(rectangle)
function perimeter(rectangle)
class Rectangle
attr_reader :width
attr_reader :height
def initialize(width, height)
@width = width
@height = height
end
def area
...
end
def perimeter
...
end
end
(define (area rectangle) ...)
(define (perimeter rectangle) ...)
; where a rectangle is represented by (cons w h)
(define (area rectangle) ...)
(define (perimeter rectangle) ...)
;;; where a rectangle is represented by (cons w h)
rectangle = list(width = ..., height = ...)
area <- function (rectangle)
perimeter <- function (rectangle)
area
and perimeter
functions
over the required rectangle
type
Copyright © 2020-2023 Rudy Matela
This text is available under the CC BY-SA 4.0 license.
Originally available on cscx.org/rectangle