squares around the rectangles

hey guys

so the question is :



(imagine all this in a big page full of 1*1 squares )

it give 4 numbers in input w1 h1 , w2 h2 and im supposed to put them(two rectangles) on each other .
(first turn the rectangles to 1*1 squares in ur mind)the left bottom corner(square) of the first rectangle is (1.1) and the right top corner (square) is( w1 h1 )
and the left bottom corner(square) of the second rectangle is (h1+1 . 1) and the top right corner (square) is (w2 h1+h2) . 


i need to find the surrounding squares around the new shape that it makes after putting them on each other 

like if it gives me : 2 1 2 1

    +   +   +

+  +  2.2   +

+  1.1 2.1  +

+  +   +    +

then it needs to cout 12
backward ( L ) is the shape of the numbers; 

or if its 2 2 1 2 
it needs to print out 16


i really need help on this ;
TY IN ADVANCE



***UPDATE :

so it cin 2 1 2 1 :


[first rectangle (inside squares)  coordinates ] :

w1 = 2  h1 = 1     left bottom square (1 . 1)       | right top square (w1 , h1)

[second rectangle (inside squares) coordinates ] :

w2 = 2  h2 = 1     left bottom square (h1+1 . 1) | right top square (w2 , h1+h2)


h1+1 = 2 == > (2.1) which is equal to the right top square (w1 , h1) of the first rectangle



      2.2
1.1 2.1

[it makes a backward L shape (in this case)]


	+	+	+
+	+	2.2	+
+	1.1	2.1*	+
+	+	+	+




each ' + ' is in a 1*1 square (in above , left , right , or bottom of the square in shape if theres no other number on the sides)

so the total squares that surrounds the shape is [ 12 ]



*****ACTUAL ASSIGNMENT :

here is the actual assignment :

 

Consider two rectangles w1 * h1w1 ∗ h1 and w2 * h2w2 ∗ h2. Place the second rectangle on the first rectangle according to the following rules:

The left side of both rectangles should be aligned.
There is no distance between the two rectangles.

For easier understanding of the problem, suppose the rectangular dimensions of integers. This way, each rectangle can be divided into 1 x 1 squares. That is, the first rectangle will contain w1 square in length and h1 square in width.
 If the square coordinates of the bottom left corner of the first rectangle (1,1), the square coordinates of the top right corner of the first rectangle (w1, h1),

 the bottom left corner coordinates of the second rectangle (square) (1, h1 + 1), and the square coordinates of the right top square of the second rectangle (w2, h1 + h2).

After the two rectangles are aligned, a polygon is created, the sides of which are parallel to the axis of the coordinates. This polygon is surrounded by 1 x 1 squares.

Write a program that calculates and prints the number of squares that will form a polygon by drawing the sides of two rectangles.

Input: 
4 integers w1, h1, w2, h2 that are the lengths of the widths of the first rectangle and the second rectangle, respectively.

1 ≤ w1 , h1 , w2 , h2 ≤ 10

Output :
Print the number of squares surrounding the polygon.


 

Last edited on
Edit your post and add [output] squares here [/output] to what you want so that it's formatted correctly.

What have you tried so far?
http://www.cplusplus.com/doc/tutorial/
http://www.cplusplus.com/doc/tutorial/control/
Reading your description, with input 2 1 2 1, I get this diagram
+ + +
2 2 +
1 1 +

With input 2 1 2 1, we have:
w1=2 h1=1
w2=2 h2=1

For rectangle 1, the bottom left is at (1,1). Since the width is 2 and the height is 1, the top left corner is at 2,1.

For rectangle 2, you said the bottom left corner is at (h1+1, 1) = (2,1). Is this right or did you mean (1,h1+1) so that the second rectangle is always right above the first one?

Anyway, I followed your description to the letter and showed the second rectangle at 2,1 with width 2 and height 1.

IS THIS CORRECT? I want to make sure we understand this part of the problem before moving on.
then it needs to cout 12
backward ( L ) is the shape of the numbers;

or if its 2 2 1 2
it needs to print out 16
I don't understand how it gets 12 and 16 for the output. How is it supposed to compute these?

Can you post the actual assignment? If it's isn't in English, run it through google translate and post.
hey dhayden ;

ive updated the question ,
Thanks for the updated question. It really helps a lot.

Here is a program that reads the input and prints out the rectangles. This will help you visualize them. Play around with it an little and see if you can figure out a formula for the number of '+' symbols that surround the two rectangles.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<iostream>

using namespace std;

const int MAX_DIM = 22;

int main()
{
    char board[MAX_DIM][MAX_DIM];
    int h1,w1, h2,w2;
    cin >> w1 >> h1 >> w2 >> h2;
    int bottom=1;
    int left = 1;

    // initialize the board
    for (int y=0; y<MAX_DIM; ++y) {
	for (int x=0; x<MAX_DIM; ++x) {
	    board[x][y] = '+';
	}
    }
    
    // Fill in the first rectangle
    for (int y=0; y<h1; ++y) {
	for (int x=0; x<w1; ++x) {
	    board[x+left][y+bottom] = '1';
	}
    }

    // Fill in the second rectangle
    bottom = h1+1;
    for (int y=0; y<h2; ++y) {
	for (int x=0; x<w2; ++x) {
	    board[x+left][y+bottom] = '2';
	}
    }
    
    for (int y=0; y<MAX_DIM; ++y) {
	for (int x=0; x<MAX_DIM; ++x) {
	    cout << board[x][MAX_DIM-y-1] << ' ';
	}
	cout << '\n';
    }
}


I haven't worked out the math yet, but I'm thinking that you need to figure out how many would surround each rectangle if they weren't next to each other (this part is pretty easy) and then subtract something to account for the fact that they DO touch.
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
   int w1, h1, w2, h2;
   cout << "Input w1 h1 w2 h2: ";   cin >> w1 >> h1 >> w2 >> h2;
   cout << "Number of border squares = " << 2 * ( h1 + h2 + 2 ) + w1 + w2 + abs( w2 - w1 ) << '\n';
}



Graphical version:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

const char A = '1', B = '2', E = '.';

int main()
{
   int w1, h1, w2, h2;
   cout << "Input w1 h1 w2 h2: ";   cin >> w1 >> h1 >> w2 >> h2;

   int H = h1 + h2 + 2;
   int W = max( w1, w2 ) + 2;
   vector<string> grid( H, string( W, ' ' ) );

   for ( int x = 0; x <= w1 + 1; x++ )
      for ( int y = 0; y <= h1 + 1; y++ ) grid[H-1-y][x] = E;
   for ( int x = 0; x <= w2 + 1; x++ )
      for ( int y = 0; y <= h2 + 1; y++ ) grid[H-1-h1-y][x] = E;

   for ( int x = 1; x <= w1; x++ )
      for ( int y = 1; y <= h1; y++ ) grid[H-1-y][x] = A;
   for ( int x = 1; x <= w2; x++ )
      for ( int y = 1; y <= h2; y++ ) grid[H-1-h1-y][x] = B;

   int border = 0;
   for ( string s : grid )
   {
      cout << s << '\n';
      border += count( s.begin(), s.end(), E );
   }
   cout << "Border squares: " << border << '\n';
}


Input w1 h1 w2 h2: 2 1 2 1
....
.22.
.11.
....
Border squares: 12


Input w1 h1 w2 h2: 2 2 1 2
... 
.2. 
.2..
.11.
.11.
....
Border squares: 16

Last edited on
hey guys TY SO MUCH for helping it worked i just can not thank u enough

i just have one last favor to ask , can u explain how u figured it out ??


#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
   int w1, h1, w2, h2;
   cout << "Input w1 h1 w2 h2: ";   cin >> w1 >> h1 >> w2 >> h2;
   cout << "Number of border squares = " << 2 * ( h1 + h2 + 2 ) + w1 + w2 + abs( w2 - w1 ) << '\n';
}


how did u know what to do ? can u explain its math ? how ur formula works ?
Last edited on
Rectangle 2 is always directly above rectangle 1 and their left edges are on the same line.

Lets draw that picture:
Input w1 h1 w2 h2: 3 2 1 2

cec 
b2b 
b2ffb
a111a
a111a
cdddc

Border squares:

How many a are there? 2*h1
How many b are there? 2*h2
How many c are there? 2*2
How many d are there? w1
How many e are there? w2
How many f are there? Difference of w1 and w2
Topic archived. No new replies allowed.