Home › Forums › C Programming › Exercise regarding Structures : Need help
- This topic has 0 replies, 1 voice, and was last updated 16 years, 2 months ago by shubham123.
- AuthorPosts
- August 15, 2008 at 6:24 am #2123shubham123ParticipantHello guys, I’m new here.
I’m a Computer Science college freshman and we have an exercise about structures, here are the instructions:
Exercise on structures: CHAPTER 8
Deadline on Aug 16, 2008 (until 11:59pm)Define a structure for a rectangle in the cartesian plane whose sides lay parallel to the x- and y- axes, as well as for a point in the same plane (which the rectangle may reuse). Afterwards, define separate functions that take &&&references&&& to such rectangles and then return the following:
– is it a valid rectangle? (0 if not, any other integer if it is) – midpoint (returns a Point)
– area (returns a float/double)
– perimeter (returns a float/double)Define a function that accepts two references to rectangles as parameters, and returns 0 if they don’t overlap, or any other integer if they do.
The driver program must behave similar to the following: %% Provide the x and y coordinates of the lower-left vertex: -12 78 %% (-12,78) defined for lower left vertex. %% … the other vertex is entered
%% Here are the points you’ve defined. %%
(-12,78) : lower left corner (?) <<<--- verify %% (1,16) : upper right corner (?)%% The points you’ve entered doesn’t appear to belong to a valid rectangle. Enter another? (Y/N): Y%% … the vertices are once again requested
%% Here are the points you’ve defined. %%
(1,2) : lower left corner %%
(5,3) : upper right corner %%Here are some statistics regarding your rectangle:
%% The midpoint is at: (3.0,2.5)
%% The area is: 4.0 sq. units %% The perimeter is: 8.0 units
%% This is the first rectangle you’ve defined. If you define another, then we can test whether the two overlap. %% Enter another? (Y/N): Y
%% … define another rect %% … the stats are displayed
%% … (if you have already entered at least two valid rectangles, then the program will tell if the last two did overlap.)
%% … (for example: This rectangle overlaps with the last rectangle, whose lower-left vectex is at (x1,y1) and upper-right vertex is at (x2,y2) )
%% … ask whether the user wants to enter another or not
%% … (and so on.) NOTES: the functions should only accept the values inputted through the parameters. Don’t ask for input inside their bodies. That’s the driver’s job.I’ve already completed the program and I think I did it right. But I have a problem because it doesn’t go to the fuction that checks if the two rectangles entered overlap. overlap() :chomp:
and i have another problem, when the user inputs points for the second time and so on, it still displays the statistics (median, perimeter and area) even though the points are invalid. :confused:
here’s my code:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270<br />#include <stdio.h><br />#include <stdlib.h><br />#include <math.h><br /><br />//Jenielle D.G. Gabriel<br />//BSCS 2008-25410<br />//CS11 Lab 1-c<br />//Exercise: Chap 8<br />//13AUG08<br /><br /><br />typedef struct {<br />int llx, lly;<br />int urx, ury;<br />}rect;<br /><br />rect *prev, *next;<br /><br />void case_one(void);<br />void getprev(void);<br />void getnext(void);<br />void verify_rect(int a,int b,int c,int d);<br />void midpoint(float a, float b,float c,float d);<br />void area (float a,float b,float c,float d);<br />void perimeter(float a,float b, float c,float d);<br />void stats(void);<br />void stats2(void);<br />void overlap(void);<br /><br />int flag;<br />int choice;<br /><br />main(){<br /><br />getprev();<br /><br />verify_rect(prev->llx,prev->lly,prev->urx,prev->ury);<br /><br />if(flag == 0){<br />stats(); // first rect<br /><br />printf("n[1]Enter anothern");<br />printf("n[2]exitn");<br />scanf("%d", &choice);<br /><br />switch(choice){<br /><br />case 1: getnext(); //2nd rect<br />verify_rect(next->llx,next->lly,next->urx,next->ury);<br /><br />if(flag == 0){<br />stats2();<br />}<br /><br />else {<br />printf("nThe points do not form a valid rectangle.");<br />printf("n[1]Enter anothern");<br />printf("n[2]exitn");<br />scanf("%d", &choice);<br /><br />switch(choice){<br /><br />case 1: free(next); case_one(); break;<br />case 2: abort(); break;<br />default:printf("1/2");<br /><br />}<br />}<br /><br />case 2: abort(); break;<br />default: printf("1/2");<br /><br />}<br /><br /><br />overlap();<br />}<br /><br />else if(flag == 1){<br /><br />printf("nThe points do not form a valid rectangle.");<br />printf("n[1]Enter anothern");<br />printf("n[2]exitn");<br />scanf("%d", &choice);<br /><br />switch(choice){<br /><br />case 1: free(prev); main(); break;<br />case 2: abort(); break;<br />default:printf("1/2");<br />}<br /><br />}<br /><br /><br />}<br /><br /><br /><br /><br />void getprev(void){<br /><br />prev = (rect*)malloc(sizeof(rect));<br /><br />printf ("Please enter the coodinates of the LOWER LEFT and UPPER RIGHT of the rectangle.n Separate each with a spacent");<br />scanf("%d %d %d %d", &(prev->llx), &(prev->lly), &(prev->urx), &(prev->ury));<br />printf("Here are the points you've enterednt UPPER LEFT(%d,%d)nt LOWER RIGHT(%d,%d)",<br />prev->llx, prev->lly, prev->urx, prev->ury);<br />}<br /><br /><br />void getnext(void){<br /><br />next = (rect*)malloc(sizeof(rect));<br /><br />printf ("Please enter the coodinates of the LOWER LEFT and UPPER RIGHT of the rectangle.n Separate each with a spacent");<br />scanf("%d %d %d %d", &(next->llx), &(next->lly), &(next->urx), &(next->ury));<br />printf("Here are the points you've enterednt UPPER LEFT(%d,%d)nt LOWER RIGHT(%d,%d)",<br />next->llx, next->lly, next->urx, next->ury);<br /><br />}<br /><br /><br /><br />void verify_rect (int a,int b,int c,int d){<br /><br /><br />if( a<c && b<d){<br></c>flag = 0;<br />}<br /><br />else{<br />flag = 1;<br />}<br />}<br /><br /><br />void midpoint(float a,float b, float c,float d){<br /><br />printf("nMidpoint: (%.1f,%.1f)", (float)(a + b) / 2, (float)(c + d)/2);<br />}<br /><br />void area (float a,float b, float c,float d){<br />float l,w;<br />float length, width;<br /><br />l =(float) pow ((a-b),2);<br />w =(float) pow ((c-d),2);<br /><br />length=(float) pow (l,.5);<br />width=(float) pow (w,.5);<br /><br />printf("nArea: %.1f", (float)(length*width));<br />}<br /><br />void perimeter (float a,float b, float c,float d){<br />int l,w;<br />int length, width;<br />l =(float) pow (((float)a-(float)b),2);<br />w =(float) pow (((float)c-(float)d),2);<br /><br />length=(float) pow ((float)l,.5);<br />width=(float) pow ((float)w,.5);<br /><br />printf( "nPerimeter: %.1f", (float)((2*length)+(2*width)) );<br />}<br /><br />void stats(void){<br /><br />midpoint(prev->llx, prev->urx, prev->lly, prev->ury);<br />area(prev->llx, prev->urx, prev->ury, prev->lly);<br />perimeter(prev->llx, prev->urx, prev->ury, prev->lly);<br /><br />}<br /><br /><br />void case_one(void){<br />getnext();<br />verify_rect(next->llx,next->lly,next->urx,next->ury);<br /><br />if(flag == 0){<br />stats2();<br />}<br /><br />else {<br />printf("nThe points do not form a valid rectangle.");<br />printf("n[1]Enter anothern");<br />printf("n[2]exitn");<br />scanf("%d", &choice);<br /><br />switch(choice){<br /><br />case 1: free(next); case_one(); break;<br />case 2: abort(); break;<br />default:printf("1/2");<br />}<br />}<br /><br />void stats2(void){<br /><br />midpoint (next->llx, next->urx, next->lly, next->ury);<br />area (next->llx, next->urx, next->ury, next->lly);<br />perimeter (next->llx, next->urx, next->ury, next->lly);<br /><br />}<br /><br /><br />void overlap(void){<br /><br />if( ( ((prev->llx < next->llx) && (next->llx < prev->urx))<br />&&<br />((prev->lly < next->lly) && (next->lly < prev->ury)) )<br />||<br />( ((prev->llx < next->llx) && (next->llx < prev->urx))<br />&&<br />((prev->lly < next->ury) && (next->ury < prev->ury)) )<br /><br />||<br /><br />( ((prev->llx < next->urx) && (next->urx < prev->urx))<br />&&<br />((prev->lly < next->lly) && (next->lly < prev->ury)) )<br /><br />||<br /><br />( ((prev->llx < next->urx) && (next->urx < prev->urx))<br />&&<br />((prev->lly < next->lly) && (next->lly < prev->ury)) )<br /><br />){<br />printf("This rectangle OVERLAPS with the last rectangle with: n lower left vertex: (%d,%d) n upper right vertex: (%d,%d)",prev->llx, prev->lly, prev->urx, prev->ury);<br /><br />printf("n[1]Enter anothern");<br />printf("n[2]exitn");<br />scanf("%d", &choice);<br />switch(choice){<br /><br />case 1: free(prev);<br />next = prev;<br />free(next);<br />case_one();<br />break;<br />case 2: abort(); break;<br />default:printf("1/2");<br />}<br />}<br /><br />else{<br />printf("This rectangle DOES NOT OVERLAP with the last rectangle with: n lower left vertex: (%d,%d) n upper right vertex: (%d,%d)",prev->llx, prev->lly, prev->urx, prev->ury);<br /><br />printf("n[1]Enter anothern");<br />printf("n[2]exitn");<br />scanf("%d", &choice);<br /><br />switch(choice){<br /><br />case 1: free(prev);<br />next = prev;<br />free(next);<br />case_one();<br />break;<br />case 2: abort(); break;<br />default:printf("1/2");<br />}<br />}<br /><br />}<br /><br />This is due tomorrow night. i’d really appreciate your prompt response. :D
here’s my YM:hautehavoc
gtalk:jenielle.gabriel
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.