# quiz question and answer

• ### Question

• did any one get question in quiz regarding finding the perimeter of a rectilinear polygon
Monday, February 26, 2007 7:18 PM

• i can post the source code if anybody is interested
Monday, February 26, 2007 7:21 PM
• #include<math.h>

#include<stdio.h>

#include<conio.h>

int length=0;

static int flag[1000][2];

void vertical(int values[][2],int n);

void horizontal(int values[][2],int n);

int calculateLength(int n,int values[][2]);

void main()

{   int i,j;

int values[100][2];

int n;

int l;

printf("enter number of vertices");

scanf("%d",n);

printf("enter vertices in increasing order in form x,y");

for(i=0;i<n;i++)

scanf("%d,%d",&values[0],values[1]);

l=calculateLength(n,values[][2]);

for(i=0;i<n;i++)

printf(" flag values %d,%d   codnts %d,%d\n",flag[0],flag[1],values[0],values[1]);

if(l==-1)

printf("constraints violated");

else

printf("length is %d",l);

getch();

}

calculateLength(int n,int values)

{    int i,j;

vertical(values[][2],n);

horizontal(values[][2],n);

for(i=0;i<n;i++)

{

for(j=0;j<2;j++)

{

if(flag[j]!=1)

return (-1);

}

}

return (length);

}

void vertical(values,n)

{

int i,j;

for(i=0;i<n;i++)

{

for(j=i+1;j<n;j++)

{

if(values[0]==values[j][0]&&flag[0]==0&&flag[j][0]==0)

{

flag[0]=1;

flag[j][0]=1;

length+=abs(values[j][1]-values[1]);

printf("joined are %d,%d and %d,%d length is %d",values[0],values[1],values[j][0],values[j][1],length);

break;

}

}

}

printf("\n\nlength till now=%d\n---------------------------\n",length);

getch();

}

Monday, February 26, 2007 7:22 PM

### All replies

• i can post the source code if anybody is interested
Monday, February 26, 2007 7:21 PM
• #include<math.h>

#include<stdio.h>

#include<conio.h>

int length=0;

static int flag[1000][2];

void vertical(int values[][2],int n);

void horizontal(int values[][2],int n);

int calculateLength(int n,int values[][2]);

void main()

{   int i,j;

int values[100][2];

int n;

int l;

printf("enter number of vertices");

scanf("%d",n);

printf("enter vertices in increasing order in form x,y");

for(i=0;i<n;i++)

scanf("%d,%d",&values[0],values[1]);

l=calculateLength(n,values[][2]);

for(i=0;i<n;i++)

printf(" flag values %d,%d   codnts %d,%d\n",flag[0],flag[1],values[0],values[1]);

if(l==-1)

printf("constraints violated");

else

printf("length is %d",l);

getch();

}

calculateLength(int n,int values)

{    int i,j;

vertical(values[][2],n);

horizontal(values[][2],n);

for(i=0;i<n;i++)

{

for(j=0;j<2;j++)

{

if(flag[j]!=1)

return (-1);

}

}

return (length);

}

void vertical(values,n)

{

int i,j;

for(i=0;i<n;i++)

{

for(j=i+1;j<n;j++)

{

if(values[0]==values[j][0]&&flag[0]==0&&flag[j][0]==0)

{

flag[0]=1;

flag[j][0]=1;

length+=abs(values[j][1]-values[1]);

printf("joined are %d,%d and %d,%d length is %d",values[0],values[1],values[j][0],values[j][1],length);

break;

}

}

}

printf("\n\nlength till now=%d\n---------------------------\n",length);

getch();

}

Monday, February 26, 2007 7:22 PM
• Nice code.....
Monday, February 26, 2007 11:27 PM