Home Forums C Programming Exercise regarding Structures : Need help

Viewing 0 reply threads
  • Author
    Posts
    • #2123
      shubham123
      Participant
      Hello 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:


      #include
      #include
      #include

      //Jenielle D.G. Gabriel
      //BSCS 2008-25410
      //CS11 Lab 1-c
      //Exercise: Chap 8
      //13AUG08


      typedef struct {
      int llx, lly;
      int urx, ury;
      }rect;

      rect *prev, *next;

      void case_one(void);
      void getprev(void);
      void getnext(void);
      void verify_rect(int a,int b,int c,int d);
      void midpoint(float a, float b,float c,float d);
      void area (float a,float b,float c,float d);
      void perimeter(float a,float b, float c,float d);
      void stats(void);
      void stats2(void);
      void overlap(void);

      int flag;
      int choice;

      main(){

      getprev();

      verify_rect(prev->llx,prev->lly,prev->urx,prev->ury);

      if(flag == 0){
      stats(); // first rect

      printf("n[1]Enter anothern");
      printf("n[2]exitn");
      scanf("%d", &choice);

      switch(choice){

      case 1: getnext(); //2nd rect
      verify_rect(next->llx,next->lly,next->urx,next->ury);

      if(flag == 0){
      stats2();
      }

      else {
      printf("nThe points do not form a valid rectangle.");
      printf("n[1]Enter anothern");
      printf("n[2]exitn");
      scanf("%d", &choice);

      switch(choice){

      case 1: free(next); case_one(); break;
      case 2: abort(); break;
      default:printf("1/2");

      }
      }

      case 2: abort(); break;
      default: printf("1/2");

      }


      overlap();
      }

      else if(flag == 1){

      printf("nThe points do not form a valid rectangle.");
      printf("n[1]Enter anothern");
      printf("n[2]exitn");
      scanf("%d", &choice);

      switch(choice){

      case 1: free(prev); main(); break;
      case 2: abort(); break;
      default:printf("1/2");
      }

      }


      }




      void getprev(void){

      prev = (rect*)malloc(sizeof(rect));

      printf ("Please enter the coodinates of the LOWER LEFT and UPPER RIGHT of the rectangle.n Separate each with a spacent");
      scanf("%d %d %d %d", &(prev->llx), &(prev->lly), &(prev->urx), &(prev->ury));
      printf("Here are the points you've enterednt UPPER LEFT(%d,%d)nt LOWER RIGHT(%d,%d)",
      prev->llx, prev->lly, prev->urx, prev->ury);
      }


      void getnext(void){

      next = (rect*)malloc(sizeof(rect));

      printf ("Please enter the coodinates of the LOWER LEFT and UPPER RIGHT of the rectangle.n Separate each with a spacent");
      scanf("%d %d %d %d", &(next->llx), &(next->lly), &(next->urx), &(next->ury));
      printf("Here are the points you've enterednt UPPER LEFT(%d,%d)nt LOWER RIGHT(%d,%d)",
      next->llx, next->lly, next->urx, next->ury);

      }



      void verify_rect (int a,int b,int c,int d){


      if( a flag = 0;
      }

      else{
      flag = 1;
      }
      }


      void midpoint(float a,float b, float c,float d){

      printf("nMidpoint: (%.1f,%.1f)", (float)(a + b) / 2, (float)(c + d)/2);
      }

      void area (float a,float b, float c,float d){
      float l,w;
      float length, width;

      l =(float) pow ((a-b),2);
      w =(float) pow ((c-d),2);

      length=(float) pow (l,.5);
      width=(float) pow (w,.5);

      printf("nArea: %.1f", (float)(length*width));
      }

      void perimeter (float a,float b, float c,float d){
      int l,w;
      int length, width;
      l =(float) pow (((float)a-(float)b),2);
      w =(float) pow (((float)c-(float)d),2);

      length=(float) pow ((float)l,.5);
      width=(float) pow ((float)w,.5);

      printf( "nPerimeter: %.1f", (float)((2*length)+(2*width)) );
      }

      void stats(void){

      midpoint(prev->llx, prev->urx, prev->lly, prev->ury);
      area(prev->llx, prev->urx, prev->ury, prev->lly);
      perimeter(prev->llx, prev->urx, prev->ury, prev->lly);

      }


      void case_one(void){
      getnext();
      verify_rect(next->llx,next->lly,next->urx,next->ury);

      if(flag == 0){
      stats2();
      }

      else {
      printf("nThe points do not form a valid rectangle.");
      printf("n[1]Enter anothern");
      printf("n[2]exitn");
      scanf("%d", &choice);

      switch(choice){

      case 1: free(next); case_one(); break;
      case 2: abort(); break;
      default:printf("1/2");
      }
      }

      void stats2(void){

      midpoint (next->llx, next->urx, next->lly, next->ury);
      area (next->llx, next->urx, next->ury, next->lly);
      perimeter (next->llx, next->urx, next->ury, next->lly);

      }


      void overlap(void){

      if( ( ((prev->llx < next->llx) && (next->llx < prev->urx))
      &&
      ((prev->lly < next->lly) && (next->lly < prev->ury)) )
      ||
      ( ((prev->llx < next->llx) && (next->llx < prev->urx))
      &&
      ((prev->lly < next->ury) && (next->ury < prev->ury)) )

      ||

      ( ((prev->llx < next->urx) && (next->urx < prev->urx))
      &&
      ((prev->lly < next->lly) && (next->lly < prev->ury)) )

      ||

      ( ((prev->llx < next->urx) && (next->urx < prev->urx))
      &&
      ((prev->lly < next->lly) && (next->lly < prev->ury)) )

      ){
      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);

      printf("n[1]Enter anothern");
      printf("n[2]exitn");
      scanf("%d", &choice);
      switch(choice){

      case 1: free(prev);
      next = prev;
      free(next);
      case_one();
      break;
      case 2: abort(); break;
      default:printf("1/2");
      }
      }

      else{
      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);

      printf("n[1]Enter anothern");
      printf("n[2]exitn");
      scanf("%d", &choice);

      switch(choice){

      case 1: free(prev);
      next = prev;
      free(next);
      case_one();
      break;
      case 2: abort(); break;
      default:printf("1/2");
      }
      }

      }

      This is due tomorrow night. i’d really appreciate your prompt response. :D

      here’s my YM:hautehavoc
      gtalk:jenielle.gabriel

Viewing 0 reply threads
  • The forum ‘C Programming’ is closed to new topics and replies.