Home > Blockchain >  How to find the largest area among N rectangles
How to find the largest area among N rectangles

Time:02-04

The main task is to generate 3 rectangles using two points (top left and bottom right of each) and determinate the largest area among them.

Partially, I have already completed the task using this code:

#include <stdio.h>
#include <time.h>

struct Point {
    int x;
    int y;
};
struct Rectangle {
    struct Point topLeft;
    struct Point botRight;
};

int Area (struct Rectangle r) {

    int length, breadth;
    length = r.botRight.x - r.topLeft.x;
    breadth = r.topLeft.y - r.botRight.y;

    return length * breadth;
}
int main() {

    srand(time(NULL));

    struct Rectangle r1, r2, r3;
   
    r1.topLeft.x = -50   rand() % 101;
    r1.topLeft.y = -50   rand() % 101;
    r1.botRight.x = -50   rand() % 101;
    r1.botRight.y = -50   rand() % 101;

    while (r1.botRight.x <= r1.topLeft.x) {
        r1.botRight.x = -50   rand() % 101;
    }
    while (r1.topLeft.y <= r1.botRight.y) {
        r1.topLeft.y = -50   rand() % 101;
    }
    printf("\t----------RECTANGLE 1----------\n");
    printf("\tTop left point is x = %d y = %d\n", r1.topLeft.x, r1.topLeft.y);
    printf("\tBottom right point is x = %d y = %d\n", r1.botRight.x, r1.botRight.y);
    printf("\tArea is %d\n", Area(r1));
    
    r2.topLeft.x = -50   rand() % 101;
    r2.topLeft.y = -50   rand() % 101;
    r2.botRight.x = -50   rand() % 101;
    r2.botRight.y = -50   rand() % 101;

    while (r2.botRight.x <= r2.topLeft.x) {
        r2.botRight.x = -50   rand() % 101;
    }
    while (r2.topLeft.y <= r2.botRight.y) {
        r2.topLeft.y = -50   rand() % 101;
    }
    printf("\t----------RECTANGLE 2----------\n");
    printf("\tTop left point is x = %d y = %d\n", r2.topLeft.x, r2.topLeft.y);
    printf("\tBottom right point is x = %d y = %d\n", r2.botRight.x, r2.botRight.y);
    printf("\tArea is %d\n", Area(r2));
    
    r3.topLeft.x = -50   rand() % 101;
    r3.topLeft.y = -50   rand() % 101;
    r3.botRight.x = -50   rand() % 101;
    r3.botRight.y = -50   rand() % 101;

    while (r3.botRight.x <= r3.topLeft.x) {
        r3.botRight.x = -50   rand() % 101;
    }
    while (r3.topLeft.y <= r3.botRight.y) {
        r3.topLeft.y = -50   rand() % 101;
    }
    printf("\t----------RECTANGLE 3----------\n");
    printf("\tTop left point is x = %d y = %d\n", r3.topLeft.x, r3.topLeft.y);
    printf("\tBottom right point is x = %d y = %d\n", r3.botRight.x, r3.botRight.y);
    printf("\tArea is %d\n", Area(r3));
    printf("\t-------------------------------\n");
    
    if (Area(r1) >= Area(r2) && Area(r1) >= Area(r3))
        printf("\tRectangle 1 has a biggest area --> %d\n", Area(r1));


    if (Area(r2) >= Area(r1) && Area(r2) >= Area(r3))
        printf("\tRectangle 2 has a biggest area --> %d\n", Area(r2));


    if (Area(r3) >= Area(r1) && Area(r3) >= Area(r2))
        printf("\tRectangle 3 has a biggest area --> %d\n", Area(r3));

But I'm very confused about how to do this task if there are, for example, 50 rectangles?)

There is what i've already done: I've created the function of generatig the points of each rectangle (genRec) in order to generate 50 rectangles.

#include <stdio.h>
#include <time.h>

struct Point {
    int x;
    int y;
};
struct Rectangle {
    struct Point topLeft;
    struct Point botRight;
};

int Area(struct Rectangle r) {

    int length, breadth;
    length = r.botRight.x - r.topLeft.x;
    breadth = r.topLeft.y - r.botRight.y;

    return length * breadth;
}
int genRec();

int main() {

    srand(time(NULL));

    for (int i = 1; i < 51; i  ) {
        printf("\t----------RECTANGLE %d----------\n" , i);
        genRec();

    }

}
int genRec() {
    struct Rectangle  rec;
   
    rec.topLeft.x = -50   rand() % 101;
    rec.topLeft.y = -50   rand() % 101;
    rec.botRight.x = -50   rand() % 101;
    rec.botRight.y = -50   rand() % 101;

    while (rec.botRight.x <= rec.topLeft.x) {
        rec.botRight.x = -50   rand() % 101;
    }
    while (rec.topLeft.y <= rec.botRight.y) {
        rec.topLeft.y = -50   rand() % 101;
    }
    printf("\tTop left point is x = %d y = %d\n", rec.topLeft.x, rec.topLeft.y);
    printf("\tBottom right point is x = %d y = %d\n", rec.botRight.x, rec.botRight.y);
    printf("\tArea is %d\n", Area(rec));

    return 0;
}

But, i have no idea how to compare the areas of each rectangle easily, without eternal comparing using "if" because Area(rec) is an only one variable, but with differnt numbers.

I will be glad for any help, don't judge harshly, I'm just a begginer)

CodePudding user response:

Your issue is that you can't just:

  1. Generate the "lower" coordinate (in a given dimension, either X or Y).
  2. then loop on the "upper" coordinate (in the given dimension) until it is larger.

If the "lower" is ever the maximum, the program will loop infinitely because the "upper" can never be greater than the "lower".

One solution is to generate a pair of numbers (e.g. lo and hi) and loop if lo >= hi. The key difference is the loop will regenerate the lo value on each iteration.

In other words, if the range is bad, reject both lower and upper numbers and try again.

A helper function to generate a pair can help.

Below is the refactored code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

struct Point {
    int x;
    int y;
};
struct Rectangle {
    struct Point topLeft;
    struct Point botRight;
};

int
Area(const struct Rectangle *r)
{
    int length, breadth;

    length = r->botRight.x - r->topLeft.x;
    if (length <= 0)
        exit(1);

    breadth = r->topLeft.y - r->botRight.y;
    if (breadth <= 0)
        exit(2);

    return length * breadth;
}

int genRec(void);

int
main(void)
{

    srand(time(NULL));

    for (int i = 1; i < 51; i  ) {
        printf("\t----------RECTANGLE %d----------\n", i);
        genRec();

    }

    return 0;
}

void
genPair(int *lo,int *hi)
{
    while (1) {
        *lo = -50   rand() % 101;
        *hi = -50   rand() % 101;
        if (*lo < *hi)
            break;
    }
}

int
genRec(void)
{
    struct Rectangle rec;

    genPair(&rec.topLeft.x,&rec.botRight.x);
    genPair(&rec.botRight.y,&rec.topLeft.y);

    printf("\tTop left point is x = %d y = %d\n",
        rec.topLeft.x, rec.topLeft.y);
    printf("\tBottom right point is x = %d y = %d\n",
        rec.botRight.x, rec.botRight.y);
    printf("\tArea is %d\n", Area(&rec));

    return 0;
}

As to determining the maximum area of all the rectangles, you calculate the area with Area but just print it.

We can return this value to main. It can retain/remember the maximum area and its corresponding rectangle index.

One issue with the original code in this regard is that genRec uses a function scoped variable rec. We want to have this be a pointer argument so main can populate an array.

Here is the refactored code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

struct Point {
    int x;
    int y;
};
struct Rectangle {
    struct Point topLeft;
    struct Point botRight;
    int area;
};

int
Area(const struct Rectangle *r)
{
    int length, breadth;

    length = r->botRight.x - r->topLeft.x;
    if (length <= 0)
        exit(1);

    breadth = r->topLeft.y - r->botRight.y;
    if (breadth <= 0)
        exit(2);

    return length * breadth;
}

int genRec(struct Rectangle *rec);

int
main(void)
{

    srand(time(NULL));

    int maxarea = -1;
    int maxidx = -1;
    struct Rectangle rec[50];

    for (int i = 0; i < sizeof(rec) / sizeof(rec[0]); i  ) {
        printf("\n");
        printf("\t----------RECTANGLE %d----------\n", i   1);
        int area = genRec(&rec[i]);

        if (area > maxarea) {
            maxarea = area;
            maxidx = i;
        }
    }

    printf("\n");
    printf("\tMaximum area is %d in rectangle %d\n",maxarea,maxidx   1);

    return 0;
}

void
genPair(int *lo,int *hi)
{
    while (1) {
        *lo = -50   rand() % 101;
        *hi = -50   rand() % 101;
        if (*lo < *hi)
            break;
    }
}

int
genRec(struct Rectangle *rec)
{

    genPair(&rec->topLeft.x,&rec->botRight.x);
    genPair(&rec->botRight.y,&rec->topLeft.y);

    printf("\tTop left point is x = %d y = %d\n",
        rec->topLeft.x, rec->topLeft.y);
    printf("\tBottom right point is x = %d y = %d\n",
        rec->botRight.x, rec->botRight.y);

    int area = Area(rec);
    printf("\tArea is %d\n", area);

    return area;
}
  • Related