477 - Points in Figures: Rectangles and Circles

All about problems in Volume 4. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Post Reply
Sneeze
New poster
Posts: 13
Joined: Thu Jan 30, 2003 4:04 am

477 - Points in Figures: Rectangles and Circles

Post by Sneeze »

My program works well in my computer, but I always get a "Inavlid memory reference" crash message. I can't figure out why. Maybe someone could help me?

---------------------------------------------------------------------------

Code: Select all

#include<stdio.h>
#include<stdlib.h>
#define RECTANGLE 1
#define CIRCLE 2

struct FIGURE
{
 int shape;
 double x1, y1, x2, y2, r;
}figure[12];

void main()
{
 int i, j, number_of_figures, number_of_points, temp;
 double x[12], y[12], a, b;
 char c1;

 /*initialize*/
 i=0;
 scanf("%c", &c1);

 /*read in*/
 while(c1!='*')
 {
  if(c1=='r')
  {
   figure[i].shape=RECTANGLE;
   scanf("%lf %lf %lf %lf",
    &figure[i].x1, &figure[i].y1, &figure[i].x2, &figure[i].y2);
   i++;
  }
  else if(c1=='c')
  {
   figure[i].shape=CIRCLE;
   scanf("%lf %lf %lf", &figure[i].x1, &figure[i].y1, &figure[i].r);
  i++;
  }
  scanf(" %c", &c1);
 }
 number_of_figures=i;

 /*read in points*/
 i=0;
 scanf("%lf %lf", &x[i], &y[i]);
 while(x[i]!=9999.9 || y[i]!=9999.9)
 {
  i++;
  scanf("%lf %lf", &x[i], &y[i]);
 }
 number_of_points=i;

 /*Analyze & output*/
 temp=0;
 for(i=0; i<number_of_points; i++, temp=0){
 for(j=0; j<number_of_figures; j++)
 {
  if(figure[j].shape==RECTANGLE)
  {
      if(x[i]>figure[j].x1 && x[i]<figure[j].x2 && y[i]<figure[j].y1 && y[i]>figure[j].y2)
      {
          temp++;
          printf("Point %d is contained in figure %d\n", i+1, j+1);
      }
  }
  else if(figure[j].shape==CIRCLE)
  {
      a=x[i]-figure[j].x1;
      b=y[i]-figure[j].y1;
      if(a*a+b*b<figure[j].r*figure[j].r)
      {
          temp++;
          printf("Point %d is contained in figure %d\n", i+1, j+1);
      }
  }
 }
 if (!temp)
     printf("Point %d is not contained in any figure\n", i+1);
 }
}
lendlice
New poster
Posts: 22
Joined: Thu Nov 21, 2002 10:50 am

477 W.A

Post by lendlice »

Code: Select all

//477
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
main()
{
 double x[11],y[11],x1[11],y1[11],in1,in2;
 int i=0,i1=0,j=0,tr=0,a=0,b=0,t;
 char r[11],n;
 while(scanf("%c",&r[i])==1)
 {
  if(r[i]=='*')
   break;
  if(r[i]=='r')
    scanf("%lf%lf%lf%lf%*c",&x[i],&y[i],&x1[i],&y1[i]);
  else if(r[i]=='c')
    scanf("%lf%lf%lf%*c",&x[i],&y[i],&x1[i]);
  i++;
 }
 while(scanf("%lf%lf",&in1,&in2))
 {
  if(in1==9999.9&&in2==9999.9)
     break;
  j++;
  for(i1=0;i1<i;i1++)
  {
   if(r[i1]=='r')
   {
    if(x[i1]<in1&&in1<x1[i1]&&y[i1]>in2&&in2>y1[i1])
    {
     printf("Point %d is contained in figure %d\n",j,i1+1);
     tr=1;
    }
   }
   else if(r[i1]=='c')
   {
    a=fabs(in1-x[i1]);
    b=fabs(in2-y[i1]);
    t=a*a+b*b;
    t=sqrt(t);
    if(t<x1[i1])
    {
     printf("Point %d is contained in figure %d\n",j,i1+1);
     tr=1;
    }
   }
  }
  if(tr==0)
     printf("Point %d is not contained in any figure\n",j);
  tr=0;
 }
}
Anyone can tell me.
What's wrong with my code.
frankhuhu
New poster
Posts: 30
Joined: Tue Jul 20, 2004 5:22 am
Contact:

477 Runtime Error (SIGSEGV) Can anyone help me? Thanks!!

Post by frankhuhu »

Why this code is Runtime Error? I can't find any mistakes. Need help!!!

Code: Select all

#include <iostream.h>

class Rect
{
public:
	double x1,y1,x2,y2;
};

class Circle
{
public:
	double x,y,r;
};

class Point
{
public:
	double x,y;
};

bool cmp1 (double x,double y,Rect& a)
{
	if (x>a.x1 && x<a.x2 && y>a.y2 && y<a.y1) return true;
	else return false;
}
bool cmp2 (double m,double n,Circle& a)
{
	double tmp=(m-a.x)*(m-a.x)+(n-a.y)*(n-a.y);
	if ( tmp<(a.r)*(a.r) ) return true;
	else return false;
}

int main()
{
	char shape;
	char figure[100];
	Circle ci[100];
        Rect re[100];
	Point po[100];
	int numfigure=1,numpoint=1,num=1;

	while (cin>>shape)
	{
		if (shape=='*') break;
		if (shape=='r')
		{
			figure[num]='r';
			cin>>re[numfigure].x1>>re[numfigure].y1>>re[numfigure].x2>>re[numfigure].y2;
			numfigure++;
			num++;
		}
		if (shape=='c')
		{
			figure[num]='c';
			cin>>ci[numfigure].x>>ci[numfigure].y>>ci[numfigure].r;
			numfigure++;
			num++;
		}
	}

	while (cin>>po[numpoint].x>>po[numpoint].y)
	{
		if (po[numpoint].x==9999.9 && po[numpoint].y==9999.9) 
			break;
	    int flag=0;
		for (int j=1;j<=numfigure;j++)
		{
			if (figure[j]=='r')
			{
				if (cmp1( po[numpoint].x,po[numpoint].y,re[j] )==true)
				{
					cout<<"Point "<<numpoint<<" is contained in figure "<<j<<endl;
					flag=1;
				}
			}

			if (figure[j]=='c')
			{
				if (cmp2( po[numpoint].x,po[numpoint].y,ci[j] )==true)
				{
					cout<<"Point "<<numpoint<<" is contained in figure "<<j<<endl;
					flag=1;
				}
			}
		}
		if (flag==0) cout<<"Point "<<numpoint<<" is not contained in any figure"<<endl;
	    numpoint++;
	}


return 0;
}
..
A great helper
Posts: 454
Joined: Thu Oct 18, 2001 2:00 am
Location: Hong Kong

Post by .. »

I think you should not assume the number of input points

Code: Select all

   Point po[100];
It is meaningless to use an array to store all the input points.
Just use one variable (instead of an array) to store the input point

Code: Select all

   Point po;
My signature:
  • Please make discussion about the algorithm BRFORE posting source code.
    We can learn much more in discussion than reading source code.
  • I HATE testing account.
  • Don't send me source code for debug.
frankhuhu
New poster
Posts: 30
Joined: Tue Jul 20, 2004 5:22 am
Contact:

Post by frankhuhu »

Thanks a lot !
But in what condition will runtime error occour?
..
A great helper
Posts: 454
Joined: Thu Oct 18, 2001 2:00 am
Location: Hong Kong

Post by .. »

So many possible case:
Array index out of bound
Recursion too many level (system stack overflow)
..........
My signature:
  • Please make discussion about the algorithm BRFORE posting source code.
    We can learn much more in discussion than reading source code.
  • I HATE testing account.
  • Don't send me source code for debug.
frankhuhu
New poster
Posts: 30
Joined: Tue Jul 20, 2004 5:22 am
Contact:

Post by frankhuhu »

Thanks Guru
I finally got AC; :)
Fuad Hassan EWU
New poster
Posts: 38
Joined: Tue Jul 17, 2007 3:21 pm
Location: East West University

wa

Post by Fuad Hassan EWU »

AC
Eagle er moto daana meley urbo
ishtiaq ahmed
Learning poster
Posts: 53
Joined: Sat Jul 29, 2006 7:33 am
Location: (CSE,DU), Dhaka,Bangladesh

477 Wrong Ans

Post by ishtiaq ahmed »

I always get wa with this code. Anybody help me?

Code: Select all

cut after AC.
No venture no gain

with best regards
------------------------
ishtiaq ahmed
hfluz
New poster
Posts: 2
Joined: Tue Apr 21, 2009 11:11 pm

477 - Points in Figures: Rectangles and Circles

Post by hfluz »

I've no idea why i'm getting wrong answer.
I'm sure my code is right for the rectangles, i tested it at the exercise 476.
But i cannot find what is wrong with the circle part. Someone could help me?
Thanks

Code: Select all

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

#include <math.h>
using namespace std;

double positivo(double num) //like abs()
{
	if(num < 0) return num * -1;
	else return num;
}

double elevar(double num, int expoente) //like pow()
{
	double a = 1;
	for(int i = 0; i < expoente; i++)
		a = a * num;
	return a;
}

class retangulo{
	public:
	double x1;
	double y1;
	double x2;
	double y2;
};

class circle{
	public:
	double x;
	double y;
	double r;
	bool contem(double a, double b)
	{
		int distancia = sqrt(elevar(positivo(x-a),2) + elevar(positivo(y-b),2));
		if(distancia < r) return true;
		else return false;
	}
};

int main()
{
	retangulo r[1000];
	circle circ[1000];
	int contr = 0, contp = 0, contc = 0, cont = 0;
	char T[1000];
	bool contained = false;
	double a, b, c, d, x, y;
	while(cin >> T[cont] && T[cont] != '*')
	{
		if(T[cont] == 'r')
		{
			cin >> a >> b >> c >> d;
			r[contr].x1 = a;
			r[contr].y1 = b;
			r[contr].x2 = c;
			r[contr].y2 = d;		
			contr++;
		}
		else
		{
			cin >> a >> b >> c;
			circ[contc].x = a;
			circ[contc].y = b;
			circ[contc].r = c;
			contc++;
		}
		cont++;
	}
	
	
	while(cin >> a >> b && (a != 9999.9 && b != 9999.9))
	{

		int  k = 0, l = 0; //k is rectangle counter and l is circle counter
		for(int j = 0; j < cont; j++)
		{

			if(T[j] == 'r')
			{
				if(a > r[k].x1 && a < r[k].x2)
				{
					if(b > r[k].y2 && b < r[k].y1)
					{
						cout << "Point " << contp + 1 << " is contained in figure " << j + 1 << endl;
						contained = true;
					}
				}
				k++;
			}
			else if(T[j] == 'c')
			{
				if(circ[l].contem(a,b))
				{ 
					cout << "Point " << contp + 1 << " is contained in figure " << j + 1 << endl;
					contained = true;
				}
				l++;				
			}
		}
		if(!contained) cout << "Point " << contp + 1 << " is not contained in any figure" << endl;
		contained = false;
		contp++;
	}


	return 0;
}

hfluz
New poster
Posts: 2
Joined: Tue Apr 21, 2009 11:11 pm

Re: 477 wrong answer

Post by hfluz »

Nobody?
nafeeur_kuet
New poster
Posts: 6
Joined: Sat Dec 20, 2014 5:19 am

Re: 477 wrong answer

Post by nafeeur_kuet »

hfluz wrote:Nobody?
You have to clear it is circle or rectangle when you giving the ans. When you test with "c" (circle) it must to clear during testing. Like this

Code: Select all

if(type[i]=='r')
{
     -->>Rectangle testing
}
else
{
   -->>Circle testing......
}
I think you are clear. :D
Post Reply

Return to “Volume 4 (400-499)”