How to do this program in C? Part 3.2-3.9 [closed]

北战南征 提交于 2019-12-11 15:33:55

问题


Are multiple conditions, as in multiple if else statements needed for the intersection rectangles to be printed correctly?

Step 3: Two rectangles intersect if they have an area in common Two rectangles do not overlap if they just touch (common edge, or common corner)

Two rectangles intersect(as specified above)if, and only if,

i) max(xmin1, xmin2) < min(xmax1, xmax2) and

ii) max(ymin1, ymin2) < min(ymax1, ymax2)

Your output is to be formatted. As shown below where a rectangle is shown as its lower left coordinates (xmin, ymin) and top right corner coordinates (xmax, ymax). Where the coordinates are coordinates in a Cartesian plane.

Sample output:

enter two rectangles: 

1 1 4 4

2 2 5 5

rectangle 1: (1,1)(4,4) 

rectangle 2: (2,2)(5,5) 

intersection rectangle: (2,2)(4,4)  

and

enter two rectangles: 

1 1 4 4

5 5 10 10

rectangle 1: (1,1)(4,4) 

rectangle 2: (5,5)(10,10) 

these two rectangles do not intersect 

Code:

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

int readRect (int *w, int *x, int *y, int *z){
return scanf("%d%d%d%d",w,x,y,z);
}

int minInt(int x1, int x2){
return x1, x2;
}

int maxInt(int y1, int y2){
    return y1, y2;
}

int main (void){

int a,b,c,d,e,f,g,h;
printf(">>enter two rectangles:\n");

readRect(&a,&b,&c,&d);
readRect(&e,&f,&g,&h);
printf("rectangle 1:(%d,%d)(%d,%d)\n",a,b,c,d);
printf("rectangle 2:(%d,%d)(%d,%d)\n",e,f,g,h);

if(maxInt(a,e) < minInt(c,g) && maxInt(b,f) < minInt(d,g)){
        printf("intersection rectangle: (%d,%d)(%d,%d)\n",?,?,?,?);
}
else {
         printf("these rectangles do not intersect\n");
}

return EXIT_SUCCESS;
}

回答1:


Your function for max and min is wrong.
1. you are not comparing the parameter passed inside these functions for maximum/minimum of two.
2. You can't return two values from a function.

You should do like this;

int minInt(int x1, int x2){
    if(x1 < x2)     
        return x1;
    else 
        return x2;
}

int maxInt(int x1, int x2){
    if(x1 > x2)     
        return x1;
    else 
        return x2;
} 

And change your printf printing the intersection rectangle to

printf("intersection rectangle: (%d,%d)(%d,%d)\n", maxInt(a,e), maxInt(b,f), minInt(c,g), minInt(d,h) );



回答2:


step 1 - The culprit is "\n" in scanf. If you remove that it will work Let me know if you need any specific help in Step 2 or Step 3.




回答3:


First thing:

return scanf("%d%d%d%d\n",w,x,y,z);

should be

return scanf("%d %d %d %d",w,x,y,z);

Then you can enter your list of numbers as a space separated list, and it will scan them correctly.

The other parts of your question, you would have to attempt to solve yourself, make your problem more specific, and raise as new questions.



来源:https://stackoverflow.com/questions/19352614/how-to-do-this-program-in-c-part-3-2-3-9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!