1

Im trying to create a program that takes in 4 values (xmin, ymin, xmax, ymax) for 2 rectangles, then calculate the (xmin, ymin, xmax, ymax) of the rectangle created by the two rectangles. Basically the only part I'm having trouble is with the intersection part.

EDIT: Clarifying, It gives me an answer, it is just not the right answer. Punching in 1 1 4 4 and 2 2 5 5 gives me (1,1) (5,5), not the (2,2) (4,4) Im supposed to get.

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

int readRect(int *xmin, int *ymin, int *xmax, int *ymax);
int maxInt(int *val1, int *val2);
int minInt(int *val1, int *val2);

int main(){
int x = 0;
int y = 0;
int a,b,c,d;
int xmin1, ymin1, xmax1, ymax1;
int xmin2, ymin2, xmax2, ymax2;


for(;;){
printf(">>enter two rectangles:\n");
x = readRect(&xmin1, &ymin1, &xmax1, &ymax1);
y = readRect(&xmin2, &ymin2, &xmax2, &ymax2);
    if (y == x){
        if (x != 4){
            break;
        }else {
            printf("Rectangle 1: (%d, %d) (%d, %d)\n", xmin1, ymin1, xmax1, ymax1);
            printf("Rectangle 2: (%d, %d) (%d, %d)\n", xmin2, ymin2, xmax2, ymax2);
        }
        a = maxInt(&xmin1, &xmin2);
        b = minInt(&xmax1, &xmax2);
        c = maxInt(&ymin1, &ymin2);
        d = minInt(&ymax1, &ymax2);
        printf("%d %d %d %d\n", a,b,c,d);
        if ((a < b) && (c < d)) {
            printf("Intersection rectangle: (%d,%d)(%d,%d)\n", a,c,b,d);
        } else {
            printf("These two rectangles do not intersect\n");
        }
    }

}
return EXIT_SUCCESS;
}

int readRect(int *xmin, int *ymin, int *xmax, int *ymax){
int noItemsRead;
fflush(stdout);
noItemsRead = scanf("%d %d %d %d", xmin, ymin, xmax, ymax);

return noItemsRead;
}

int minInt(int *val1, int *val2){
if (val1 < val2){
    return *val1;
} else {
    return *val2;
}

}

int maxInt(int *val1, int *val2){
if (val1 > val2){
    return *val1;
} else {
    return *val2;
}
}
finnw
  • 47,861
  • 24
  • 143
  • 221
Jake Cooper
  • 109
  • 1
  • 3
  • 9

2 Answers2

2

Every thing is fine except your functions maxInt and minInt. Replace if (val1 < val2) by if (*val1 < *val2) and if (val1 > val2) to if (*val1 > *val2)

int minInt(int *val1, int *val2){
    if (*val1 < *val2){
        return *val1;
    } 
    else {
        return *val2;
    }

}

int maxInt(int *val1, int *val2){
     if (*val1 > *val2){
         return *val1;
     } 
    else {
         return *val2;
}
haccks
  • 104,019
  • 25
  • 176
  • 264
  • I effectively have that, I have just used a, b, c, d to make it easier on the eyes. Changing that line still yields the same wrong answer. Punching in 1 1 4 4, 2 2 5 5 yields (1,1) (5,5) as opposed to the correct answer, which is (2,2) (4,4). – Jake Cooper Oct 15 '13 at 20:02
  • 1
    Ya, just caught this! Thanks. Pointer problems. – Jake Cooper Oct 15 '13 at 20:12
  • I answered same [question](http://stackoverflow.com/a/19353303/2455888) yesterday!. Do you guys have same assignment? – haccks Oct 15 '13 at 20:16
  • 1
    Haha, creeped the guy and he has some other questions from the same assignments as me. Seems like they're definetly in my first year CompuSci course! Small world :). Thanks for the help haccks. – Jake Cooper Oct 15 '13 at 21:31
  • While this fixes the problem, it seems a trifle pointless to be passing the values by pointer in the first place. It would be better to revise the function to `int maxInt(int val1, int val2) { return (val1 > val2) ? val1 : val2; }` (possibly qualified with `static inline` and defined like this in place of the prototype declaration). – Jonathan Leffler Nov 11 '13 at 00:55
  • @JonathanLeffler; OP wanted to pass pointer rather than values itself :) – haccks Nov 11 '13 at 17:04
  • 1
    The comment was aimed half at the OP ("you don't need to use pointers") and half at you ("by all means show the code using pointers, but do recommend not using pointers when they are unnecessary", maybe with some guidelines on when they are a good idea — large structures, etc.). It's a suggested addition to the answer. Anyway, the point is now discussed in comments; it doesn't need further battering. – Jonathan Leffler Nov 11 '13 at 18:09
1

Fixed my problem, it happens in the following lines, if I change:

int minInt(int *val1, int *val2){
if (val1 < val2){
    return *val1;
} else {
    return *val2;
}

}

int maxInt(int *val1, int *val2){
if (val1 > val2){
    return *val1;
} else {
    return *val2;
}
}

TO

int minInt(int *val1, int *val2){
if (*val1 < *val2){
    return *val1;
} else {
    return *val2;
}

}

int maxInt(int *val1, int *val2){
if (*val1 > *val2){
    return *val1;
} else {
    return *val2;
    }
}

Had to add the points when doing the logical comparison.

Jake Cooper
  • 109
  • 1
  • 3
  • 9