Number of rectangles from unit squares | Matlab Examples

Matlab Tutorials | Examples

Practice 13:

Number of rectangles from unit squares

Assume you are given an m×n rectangle of unit squares. Within this rectangle, many rectangles and squares can be formed by joining the unit squares.
Consider the 2×3 rectangle below.


There are 18 rectangles (some of them being squares) that you can construct using it:
A, AB, ABC, AD, ABDE, ABCDEF, B, BC, BE, BCEF, C, CF, D, DE, DEF, E, EF, F
Write a MATLAB program that gets the values m and n from the user. If either m or n is not a positive integer number, then the program should ask for that number again, until an acceptable value is given. The program should then count the number of rectangles/squares within the given m×n rectangle using loops, and display this number.

Solutions:

 

Leave a Reply

Your email address will not be published. Required fields are marked *