C program 003





Two non-overlapping fields need to be fenced together to guard from cattle. The fence is always a single rectangle, which cover the two fields exactly.

Given the left bottom coordinate, length and width of the two fields, write a program to find dimension of the fence. Print “Invalid Input” if the fields overlap.
 

Input Format:
The 1st line of the input consists of 4 integers separated by a space that correspond to x, y, l and w of the first rectangle.
The 2nd line of the input consists of 4 integers separated by a space that correspond to x, y, l and w of the second rectangle.
Output Format:
Output consists of 4 integers that correspond to x, y, l and w of the Union rectangle.

Sample Input 1:
0 2 4 3
4 0 2 8
Sample Output 1:
0 0 6 8
 
Sample Input 2:
0 2 4 3
3 0 2 8
Sample Output 2:
Invalid Input


Solution Code :


#include<stdio.h>
int main()
{
    int a1,b1,l1,w1,a2,b2,l2,w2,a3,b3,l3,w3;
    scanf("%d%d%d%d",&a1,&b1,&l1,&w1);
    scanf("%d%d%d%d",&a2,&b2,&l2,&w2);
    l1+=a1,l2+=a2,w1+=b1,w2+=b2;
    if((a2>a1 && a2<l1)||(a1>a2 && a1<l2))
    printf("Invalid Input");
    else
    {
        if(a1<a2)
        a3=a1;
        else
        a3=a2;
        if(b1<b2)
        b3=b1;
        else
        b3=b2;
        if(l1>l2)
        l3=l1;
        else
        l3=l2;
        if(w1>w2)
        w3=w1;
        else
        w3=w2;
        l3=l3-a3;
        w3=w3-b3;
        printf("%d %d %d %d",a3,b3,l3,w3);
    }
    return 0;
}

Recommended

Post a Comment