Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
January 17th, 2013, 01:56 PM
 1415926535
Registered User

Join Date: Jan 2013
Posts: 7
Time spent in forums: 2 h 35 m 44 sec
Reputation Power: 0
Longest envelope of the set of points.

Hello,
I am supposed to find the longest envelope of the randomly generated set of points.
1. I have generated those points, that part works.
(putting x-coord into even elements of an array, and y-coord into odd elements)
2. I have found the "edge" points, namely the highest, lowest and the two on right and left.
I intended to find the envelope by taking as a starting point the highest one, then finding the least steep slope between points and putting coordinates of that found point into another array.
In order to make sure that I will not consider any points from "the wronf side of my point" I put a restriction on x-coord so that they had to be greater (later lower) than the previous point. Then repeat the procedure until the points fro which i have started .
The problem is that the program crashes when I use the function to find the edge points.
Could you make a hint, please?

The points and coordinates should be like that

tab[c-1],tab[c]

tab[d], tab[d+1] tab[a], tab[a+1]

tab[b-1],tab[b

Code:
```void ComparingSlopes(int a, int b, int c, int d, int n, int tab[200], int Arr[100])

{
double slope1, slope2, slope3 , slope4;
//int Arr[100];			// array to hold coordinates,	 again even are x's. // in this place i have coordinates of the edge points
Arr[0]=tab[c-1];
Arr[1]=tab[c];			// tab[c-1], tab [c] respectively X and Y coordinate of the highest point

double pomocnicze = 1000;
double pomocnicze1 = 1000;
double pomocnicze2= 1000;
double pomocnicze3 = 1000;

int DoPetli = 1;

do
{
for(int i = 0; i < n ; i++)
{
if(tab[2*i]>Arr[2*DoPetli])						// at some moment they will have to meet with the lowest point
{
if(abs((tab[2*i+1]-Arr[2*DoPetli+1])/(tab[2*i]-Arr[2*DoPetli])) < pomocnicze)
{
pomocnicze = abs((tab[2*i+1]-Arr[2*DoPetli+1])/(tab[2*i]-Arr[2*DoPetli]));
Arr[2*DoPetli]=tab[2*i];
Arr[2*DoPetli+1]=tab[2*i+1];
}
}
}
DoPetli++;
}
while(Arr[2*DoPetli]!=tab[a]);

int DoPetli1 = DoPetli;

do
{
for(int i = 0; i < n ; i++)
{
if(tab[2*i]<Arr[2*DoPetli1])						// at some moment they will have to meet with the lowest point
{
if(abs((tab[2*i+1]-Arr[2*DoPetli1+1])/(tab[2*i]-Arr[2*DoPetli1])) < pomocnicze1)
{
pomocnicze1 = abs((tab[2*i+1]-Arr[2*DoPetli1+1])/(tab[2*i]-Arr[2*DoPetli1]));
Arr[2*DoPetli1]=tab[2*i];
Arr[2*DoPetli1+1]=tab[2*i+1];
}
}
}
DoPetli1++;
}
while(Arr[2*DoPetli]!=tab[b-1]);

int DoPetli2 = DoPetli1;

do
{
for(int i = 0; i < n ; i++)
{
if(tab[2*i]<Arr[2*DoPetli2])						// at some moment they will have to meet with the lowest point
{
if(abs((tab[2*i+1]-Arr[2*DoPetli2+1])/(tab[2*i]-Arr[2*DoPetli2])) < pomocnicze2)
{
pomocnicze2 = abs((tab[2*i+1]-Arr[2*DoPetli2+1])/(tab[2*i]-Arr[2*DoPetli2]));
Arr[2*DoPetli2]=tab[2*i];
Arr[2*DoPetli2+1]=tab[2*i+1];
}
}
}
DoPetli2++;
}
while(Arr[2*DoPetli2]!=tab[d]);

int DoPetli3 = DoPetli2;

do
{
for(int i = 0; i < n ; i++)
{
if(tab[2*i]>Arr[2*DoPetli3])						// at some moment they will have to meet with the lowest point
{
if(abs((tab[2*i+1]-Arr[2*DoPetli3+1])/(tab[2*i]-Arr[2*DoPetli3])) < pomocnicze3)
{
pomocnicze3 = abs((tab[2*i+1]-Arr[2*DoPetli3+1])/(tab[2*i]-Arr[2*DoPetli3]));
Arr[2*DoPetli3]=tab[2*i];
Arr[2*DoPetli3+1]=tab[2*i+1];
}
}
}
DoPetli3++;
}
while(Arr[2*DoPetli3]!=tab[c-1]);
}```

#2
January 17th, 2013, 02:17 PM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,260
Time spent in forums: 2 Months 4 Weeks 1 Day 15 h 3 m 57 sec
Reputation Power: 1827
Your code would be a lot easier to understand if you used a structure to store points.
Code:
```typedef struct {
int x;
int y;
} point;

void ComparingSlopes(int a, int b, int c, int d, int n, point tab[100], point Arr[100])```

Then you can do things like this
Code:
```  do {
for (int i = 0; i < n; i++) {
if (tab[i].x > Arr[DoPetli].x)  // at some moment they will have to meet with the lowest point
{
if (abs((tab[i].y - Arr[DoPetli].y) / (tab[i].x - Arr[DoPetli].x)) < pomocnicze) {
pomocnicze = abs((tab[i].y - Arr[DoPetli].y) / (tab[i].x - Arr[DoPetli].x));
Arr[DoPetli] = tab[i];
}
}
}
DoPetli++;
}
while (Arr[DoPetli].x != tab[a].x);```

> int DoPetli = 1;
Arrays start at 0, not 1
If your input array is full, you're going to run off the end of your output array.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

#3
January 18th, 2013, 05:15 AM
 1415926535
Registered User

Join Date: Jan 2013
Posts: 7
Time spent in forums: 2 h 35 m 44 sec
Reputation Power: 0
I did as you have adviced.

It is way easier to read, but still I am blind: cannot spot the mistake.

This time I will post the whole code, if someone could look through it, I would be obliged.

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <ctime>
#include <math.h>

typedef struct {
int x;
int y;
} point;

int ScanningNumberOfPoints();
void GeneratingPoints(int n, point tab[200]);
void PrintingPoints(int n, point tab[200]);
void FindingExtremePositions(int a, int b, int c, int d, point tab[200], int n);
void ComparingSlopes(int a, int b, int c, int d, int n, point tab[200], point Arr[100]);
void Distance (point Arr[100], int n);

int main()
{
int a, b, c, d;
point tab[200];
point Arr[100];
int n = ScanningNumberOfPoints();
GeneratingPoints(n, tab);
PrintingPoints(n, tab);
FindingExtremePositions(a,  b,  c,  d, tab, n);
ComparingSlopes( a,  b,  c,  d, n , tab, Arr);
Distance (Arr, n);
}

void GeneratingPoints(int n, point tab[200])
{

srand((unsigned)time(NULL));
int b,c;
b=40; // upper bond of generated coordinates
c=10;	// lower bond of generated coordinates

for(int i =0; i < n; i++)
{
tab[i].x = (b-c)*rand()/double(RAND_MAX) + c;
tab[i].y = (b-c)*rand()/double(RAND_MAX) + c;
}
}

void PrintingPoints(int n, point tab[200])
{
for(int k = 0 ; k < 40; k++)
{
for(int a = 0; a < 40; a++)
{
bool prt = true;
for(int i = 0 ; i < n; i++)      // Printing points to the given coordinates
{
if(k==(40 - tab[i].y)  && (a == tab[i].x) )
prt = false;
}
if(prt)
{
printf(" ");
}
else
printf("*");

}
printf("\n");
}

}

int ScanningNumberOfPoints()
{
int n;
printf("input how many points you'd like to see:\n");
scanf("%d", &n);
return n;
}

void FindingExtremePositions(int a, int b, int c, int d, point tab[200], int n)
{
int  max1 =0, max2 = 110, max3 = 0, max4 =110;
// a rightest		a,b,c,d,
// b lowest
// c highest
// d leftest

for(int i =0;i<n;i++) //finding maximum x index				//0
{
if(tab[i].x>max1)
{
max1 = tab[i].x;
a=i;
}
}

for(int i =0;i<n;i++)			//finding minumum x index 			//1
{
if(tab[i].x<max2)
{
max2 = tab[i].x;
b=i;
}
}

for(int i =0;i<n;i++)			//finding maximum y index		//2
{
if(tab[i].y>max3)
{
max3=tab[i].y;
c=i;
}
}

for(int i =0;i<n;i++)				//finding minumum y index //3
{
if(tab[i].y<max4)
{
max4=tab[i].y;
d=i;
}
}

printf("\n\nthe coordinates of the point with the biggest x coordinate are:\n%dx\t\t%dy\n\n", tab[a].x, tab[a].y);
printf("the coordinates of the point with the lowest x coordinate are:\n%dx\t\t%dy\n\n", tab[b].x, tab[b].y);
printf("the coordinates of the point with the biggest y coordinate are:\n%dx\t\t%dy\n\n", tab[c].x, tab[c].y);
printf("the coordinates of the point with the lowest y coordinate are:\n%dx\t\t%dy\n\n", tab[d].x, tab[d].y);

}

void ComparingSlopes(int a, int b, int c, int d, int n, point tab[100], point Arr[100])

{

Arr[0].x=tab[c].x;
Arr[0].y=tab[c].y;

double pomocnicze = 1000;
double pomocnicze1 = 1000;
double pomocnicze2= 1000;
double pomocnicze3 = 1000;

int DoPetli = 0;

do {										// finding the slopes in 1st quadrant of the coordinate system
for (int i = 0; i < n; i++) {
if (tab[i].x > Arr[DoPetli].x)
{
if (abs((tab[i].y - Arr[DoPetli].y) / (tab[i].x - Arr[DoPetli].x)) < pomocnicze) {
pomocnicze = abs((tab[i].y - Arr[DoPetli].y) / (tab[i].x - Arr[DoPetli].x));
Arr[DoPetli].x = tab[i].x;
Arr[DoPetli].y = tab[i].y;
}
}
}
DoPetli++;
}
while (Arr[DoPetli].x != tab[a].x);

int DoPetli1 = DoPetli;

do {										// finding the slopes in 2nd quadrant of the coordinate system
for (int i = 0; i < n; i++) {
if (tab[i].x < Arr[DoPetli1].x)  // at some moment they will have to meet with the lowest point
{
if (abs((tab[i].y - Arr[DoPetli1].y) / (tab[i].x - Arr[DoPetli1].x)) < pomocnicze1) {
pomocnicze1 = abs((tab[i].y - Arr[DoPetli1].y) / (tab[i].x - Arr[DoPetli1].x));
Arr[DoPetli1].x = tab[i].x;
Arr[DoPetli1].y = tab[i].y;
}
}
}
DoPetli1++;
}
while (Arr[DoPetli1].x != tab[d].x);

int DoPetli2 = DoPetli1;

do {												// finding the slopes in 3rd quadrant of the coordinate system
for (int i = 0; i < n; i++) {
if (tab[i].x < Arr[DoPetli2].x)  // at some moment they will have to meet with the lowest point
{
if (abs((tab[i].y - Arr[DoPetli2].y) / (tab[i].x - Arr[DoPetli2].x)) < pomocnicze2) {
pomocnicze2 = abs((tab[i].y - Arr[DoPetli2].y) / (tab[i].x - Arr[DoPetli2].x));
Arr[DoPetli2].x = tab[i].x;
Arr[DoPetli2].y = tab[i].y;
}
}
}
DoPetli2++;
}
while (Arr[DoPetli2].x != tab[b].x);

int DoPetli3 = DoPetli2;

do {													// finding the slopes in 4th quadrant of the coordinate system
for (int i = 0; i < n; i++) {
if (tab[i].x > Arr[DoPetli3].x)  // at some moment they will have to meet with the lowest point
{
if (abs((tab[i].y - Arr[DoPetli3].y) / (tab[i].x - Arr[DoPetli3].x)) < pomocnicze3) {
pomocnicze3 = abs((tab[i].y - Arr[DoPetli3].y) / (tab[i].x - Arr[DoPetli3].x));
Arr[DoPetli3].x = tab[i].x;
Arr[DoPetli3].y = tab[i].y;
}
}
}
DoPetli3++;
}
while (Arr[DoPetli3].x != tab[c].x);

}

void Distance (point Arr[100], int n)

{
double sum = 0;
for(int i = 0; i < n; i++)
{
sum += sqrt((Arr[i+1].x - Arr[i].x)+(Arr[i+1].y - Arr[i].y));
}
printf("\n\nthe length of an envelope of the set is: \n%lf\n\n", sum);
}```

#4
January 18th, 2013, 06:10 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,260
Time spent in forums: 2 Months 4 Weeks 1 Day 15 h 3 m 57 sec
Reputation Power: 1827
What input do you provide?

Say
3
1 2
3 4
5 6

It would help if your GeneratingPoints() either had fixed data, or allowed user input. Then we all would see the same thing. Your rand() is likely to be different to my rand().

Try say
Code:
```void GeneratingPoints(int n, point tab[200])
{
static point test[200] = { { 1,2 }, { 3, 4 }, { 5, 6 } };
for(int i =0; i < n; i++)
{
tab[i]. = test[i];
}
}```

Start testing with really simple examples - ie, ones you can work out the correct answer to on paper, and then make sure the code works for this simple case.

If this doesn't work, it's very easy for us to try it, and see the same results with no guesswork.

When you're happy with a simple case, try a more complicated case (4 points).

At some point, you'll feel confident enough to tackle a full randomised set of points of any length.

#5
January 18th, 2013, 06:32 AM
 1415926535
Registered User

Join Date: Jan 2013
Posts: 7
Time spent in forums: 2 h 35 m 44 sec
Reputation Power: 0
Thanks for reply, although I mean that the program crashes.
It generates points, finds the extreme point coordinates and then just "has stopped working".

It compiles, (I don't know whether it is dependant on a compiler I use - does it compile for you?)

I have reviewed the part many times, thought how it works, and appears that I am wrong. So I think that my question is: why is crashes?

Edit: Is crashing probable to be dependant on the input, you have mentioned?

#6
January 18th, 2013, 10:01 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,260
Time spent in forums: 2 Months 4 Weeks 1 Day 15 h 3 m 57 sec
Reputation Power: 1827
Time to learn how to use a debugger.
Code:
```\$ g++ -g foo.c -lm
\$ gdb ./a.out
GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
(gdb) run
Starting program: /home/sc/Documents/a.out
input how many points you'd like to see:
10

**
**

the coordinates of the point with the biggest x coordinate are:
10x		9y

the coordinates of the point with the lowest x coordinate are:
9x		9y

the coordinates of the point with the biggest y coordinate are:
10x		10y

the coordinates of the point with the lowest y coordinate are:
9x		9y

Program received signal SIGSEGV, Segmentation fault.
ComparingSlopes (a=-7568, b=32767, c=0, d=0, n=10, tab=0x7fffffffd810, Arr=0x7fffffffde50) at foo.c:187
187	  do {                        // finding the slopes in 3rd quadrant of the coordinate system
(gdb) list
182	  while (Arr[DoPetli1].x != tab[d].x);
183
184	  int DoPetli2 = DoPetli1;
185
186
187	  do {                        // finding the slopes in 3rd quadrant of the coordinate system
188	    for (int i = 0; i < n; i++) {
189	      if (tab[i].x < Arr[DoPetli2].x)  // at some moment they will have to meet with the lowest point
190	      {
191	        if (abs((tab[i].y - Arr[DoPetli2].y) / (tab[i].x - Arr[DoPetli2].x)) < pomocnicze2) {
(gdb) print DoPetli1
\$1 = 190
(gdb) ```

Arr is an array of 100 elements, yet the last subscript used on it is 190.
You've overflowed the array quite significantly.

#7
January 19th, 2013, 09:58 AM
 1415926535
Registered User

Join Date: Jan 2013
Posts: 7
Time spent in forums: 2 h 35 m 44 sec
Reputation Power: 0
Yes, you are right; I have never heard about debugging. And since it is the case I can't see where is my mistake.

I have reviewed my code, made it way shorter, accomodated much more space for arrays and still it crashes.

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <ctime>
#include <math.h>

typedef struct {
int x;
int y;
} point;

int ScanningNumberOfPoints();
void GeneratingPoints(int n, point tab[200]);
void PrintingPoints(int n, point tab[200]);
void FindingExtremePositions(point tab[200], int n, point Arr[1000], int size);					// two points in each line; one most on right, and one most on left
void ComparingSlopes(int size, point Arr[1000], point Arr1[1000]);
void Distance (point Arr1[1000], int n, int size);

int main()
{
int size;			// size of an array of extreme points
point tab[200];					// array of all coordinates
point Arr[1000];			//array of extreme points
point Arr1[1000];				// array of en envelope of the set of points
int n = ScanningNumberOfPoints();			// number of randomly generated points
GeneratingPoints(n, tab);
PrintingPoints(n, tab);
FindingExtremePositions(tab, n, Arr, size);
ComparingSlopes( size, Arr, Arr1);
Distance (Arr1, n, size);
}

void GeneratingPoints(int n, point tab[200])
{

srand((unsigned)time(NULL));
int b,c;
b=40; // upper bond of generated coordinates
c=0;	// lower bond of generated coordinates

for(int i =0; i < n; i++)
{
tab[i].x = (b-c)*rand()/double(RAND_MAX) + c;
tab[i].y = (b-c)*rand()/double(RAND_MAX) + c;
}
}

void PrintingPoints(int n, point tab[200])		// Printing points to given coordinates
{
for(int k = 0 ; k < 40; k++)
{
for(int a = 0; a < 40; a++)
{
bool prt = true;
for(int i = 0 ; i < n; i++)
{
if(k==(40 - tab[i].y)  && (a == tab[i].x) )
prt = false;
}
if(prt)
{
printf(" ");
}
else
printf("*");

}
printf("\n");
}

}

int ScanningNumberOfPoints()
{
int n;
printf("input how many points you'd like to see:\n");
scanf("%d", &n);
return n;
}

void FindingExtremePositions( point tab[200], int n, point Arr[1000], int size)
{
int max=0;
int min=1000;
int licznik = 0;
int licznik1 = 0;

for(int i=0;i<n;i++)						// in this loop i find points most on right
{
if(tab[i].x>20)							// twenty is the middle of the board I print points on
{
licznik++;
for(int a=0;a<n;a++)
{
if(tab[i].x==tab[a].x)
{
if(tab[a].y>max)
{
max=tab[a].y;
Arr[i].x=tab[i].x;
Arr[i].y=tab[a].y;
}
}
}
}
}

for(int i=0;i<n;i++)										// in this loop i find points most on left
{
if(tab[i].x<20)
{
licznik1++;
for(int a=0;a<n;a++)
{
if(tab[i].x==tab[a].x)
{
if(tab[a].y<min)
{
min=tab[a].y;
Arr[licznik+i].x=tab[i].x;				//to prevent overlappinvg the index of an array I conrinue using an array from the last occupied index
Arr[licznik+i].y=tab[a].y;
}
}
}
}
}

size = licznik +licznik1;
}

void ComparingSlopes(int size, point Arr[1000], point Arr1[1000])

{
int pomocnicze3 = 1000;

for (int i = 0; i < size; i++)
{
for(int a=1;a<size;a++)
{
if (abs((Arr[i].y - Arr[a].y) / (Arr[i].x - Arr[a].x)) < pomocnicze3)
{
pomocnicze3 = abs((Arr[i].y - Arr[a].y) / (Arr[i].x - Arr[a].x));
Arr1[i].x = Arr[i].x;
Arr1[i].y = Arr[i].y;
}
}
}
}

void Distance (point Arr1[1000], int n, int size)

{
double sum=0;
for(int i = 0;i<size-1;i++)
{
sum+=sqrt(pow((Arr1[i+1].x-Arr1[i].x),2)+pow((Arr1[i+1].y-Arr1[i].y),2));
}
//sum=sum+sqrt(pow((Arr1[size-1].x-Arr1[0].x),2)+pow((Arr1[size-1].y-Arr1[0].y),2));
printf("%lf\n", sum);
}```

#8
January 19th, 2013, 12:18 PM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,260
Time spent in forums: 2 Months 4 Weeks 1 Day 15 h 3 m 57 sec
Reputation Power: 1827
Quote:
 Originally Posted by salem What input do you provide? Say 3 1 2 3 4 5 6

Once again, code with no indication of what input you tested with.
I tried 5 and 200 and it didn't crash.
I tried 500 and it crashed.

You need to pick up the ball here.
I've shown you what you need to do (use a debugger).

#9
January 19th, 2013, 01:02 PM
 clifford
Contributing User

Join Date: Aug 2003
Location: UK
Posts: 4,965
Time spent in forums: 1 Month 4 Days 3 h 30 m 19 sec
Reputation Power: 1801
What tool chain or IDE are you using? Then you can get help on using the specific debugger available to you. Salem's example used raw command line GDB which is frankly scary and will likely put you off using a debugger for a long time!

In a debugger you can step through the code one source line at a time (or even one machine instruction at a time), view (and modify) the value of variables and memory, set breakpoints, and most usefully in this case perhaps when a crash occurs the debugger will in most cases catch it and show you exactly what line in the code it occurred at, with a call-stack trace so you can map back to the root cause, and the state of all variables at that point. If you are using an IDE, you will in most cases be able to do all that in the same environment in which you develop the code.

If you are using Visual C++, you can say what you like about the compiler, but its debugger is about the best available. GCC in Eclipse of Code::Blocks is pretty easy to use too, but if you are using Dev-C++, dump it - its integration with GDB is horribly broken.

BTW configure your code editor to indent with space characters rather then tabs; it is causing your code to render very badly when posted to the forum.

#10
January 20th, 2013, 03:44 PM
 1415926535
Registered User

Join Date: Jan 2013
Posts: 7
Time spent in forums: 2 h 35 m 44 sec
Reputation Power: 0
I have once again rearrenaged the whole program. Now my idea is to find the edge point in every line, there are 3 case:
1. no points - go to the next line,
2. 1 point - put it into array twice,
3, 2 or more points, then find the most on the left and the most on the right.

I intend to start putting them into array at the ery top of, this time input coordinates, then proceed left, so if there is only one point in a line and "m" is a number of non-empty lines, its coordinates would be Arr[i].x,Arr[i].y and
Arr[i+m].x,Arr[i+m].y

My input was
3
1 2
3 4
5 6
The program crashed. I tried working with debugger but it was of no help, since I do not fulluy understand the way the debugger works.

I put my code.

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <ctime>
#include <math.h>

typedef struct {
int x;
int y;
} point;

int ScanningNumberOfPoints();
void GeneratingPoints(int n, point tab[200]);
void FindingExtremePositions(point tab[200], int n, point Arr[1000], int m);					// two points in each line; one most on right, and one most on left
void Distance (point Arr1[1000], int n, int m);

int main()
{
int m=0;			// size of an array of extreme points
point tab[200];					// array of all coordinates
point Arr[1000];			//array of extreme points
int n = ScanningNumberOfPoints();			// number of randomly generated points
GeneratingPoints(n, tab);
FindingExtremePositions(tab, n, Arr, m);
Distance (Arr, n, m);
}

int ScanningNumberOfPoints()
{
int n;
printf("input how many points you'd like to see:\n");
scanf("%d", &n);
return n;
}

void GeneratingPoints(int n, point tab[200])
{

for(int i =0; i < n; i++)
{
scanf("%d", &tab[i].x);
scanf("%d", &tab[i].y);
}
}

void FindingExtremePositions( point tab[200], int n, point Arr[1000], int m)
{
int max=0;
int min=1000;
int licznik = 0;
int licznik1 = 0;
//half of the points
int a; //The highest point
int d=0;
int numerpodwojnejlinii;
int zapamietajmaxwpodwojnejlinii=0;
int zapamietajminwpodwojnejlinii =111;

for(int i=0;i<50;i++)
{
for(int y=0;y<50;y++)
{
if(tab[y].x!=i)	//HOW MANY POINTS ARE IN ONE LINE
{
m++; // NUMBER OF EMPTY LINES
}
}
}

for(int i=0;i<n;i++)
{
if(tab[i].y>max)
{
max=tab[i].y;
a=i;
}
}

for(int i=0;i<50;i++)
{
for(int a=0;a<50;a++)
{
if(tab[a].y==i)	//HOW MANY POINTS ARE IN ONE LINE
{
d++;
numerpodwojnejlinii=i;
}
}
if(d==0)
{}
else
if(d==1)
{
Arr[i].x=tab[i].x;
Arr[i].y=tab[i].y;
Arr[i+m].x=tab[i].x;
Arr[i+m].y=tab[i].y;
}
if(d>=2)
{
for(int r=0;r<n;r++)
{
if(tab[r].y==numerpodwojnejlinii)
{
if(tab[r].x>max)
{
max=tab[r].x;
zapamietajmaxwpodwojnejlinii=r;
}
if(tab[r].x<min)
{
min=tab[r].x;
zapamietajminwpodwojnejlinii=r;
}
}
}
Arr[i].x=tab[zapamietajmaxwpodwojnejlinii].x;
Arr[i].y=tab[zapamietajmaxwpodwojnejlinii].y;
Arr[i+m].x=tab[zapamietajminwpodwojnejlinii].x;
Arr[i+m].y=tab[zapamietajminwpodwojnejlinii].y;
//numerpodwojnejlinii=0;
//zapamietajmaxwpodwojnejlinii=0;
//zapamietajminwpodwojnejlinii=110;
//max=0;
//	min=100;
}
}
}

void Distance (point Arr[1000], int n, int m)

{
double sum=0;
for(int i = 0;i<2*m;i++)
{
sum+=sqrt(pow((Arr[i+1].x-Arr[i].x),2)+pow((Arr[i+1].y-Arr[i].y),2));
}
sum=sum+sqrt(pow((Arr[2*m-1].x-Arr[0].x),2)+pow((Arr[2*m-1].y-Arr[0].y),2));
printf("%lf\n", sum);
}```

#11
January 20th, 2013, 04:22 PM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,260
Time spent in forums: 2 Months 4 Weeks 1 Day 15 h 3 m 57 sec
Reputation Power: 1827
I suggest you replicate these steps yourself in your debugger (you haven't said anything about your environment, so we can't offer any concrete advice).

Seriously, between the lack of information and cross-posting , you're becoming very high maintenance.

Code:
```\$ gdb ./a.out
GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
(gdb) run
Starting program: /home/sc/Documents/a.out
input how many points you'd like to see:
3
1 2 3 4 5 6

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400b5d in FindingExtremePositions (tab=0x7fffffffdb40, n=3, Arr=0x7fffffffbc00, m=2486) at foo.c:124
124	    Arr[i+m].x=tab[zapamietajminwpodwojnejlinii].x;
(gdb) list
119	          }
120	        }
121	    }
122	    Arr[i].x=tab[zapamietajmaxwpodwojnejlinii].x;
123	    Arr[i].y=tab[zapamietajmaxwpodwojnejlinii].y;
124	    Arr[i+m].x=tab[zapamietajminwpodwojnejlinii].x;
125	    Arr[i+m].y=tab[zapamietajminwpodwojnejlinii].y;
126	    //numerpodwojnejlinii=0;
127	    //zapamietajmaxwpodwojnejlinii=0;
128	    //zapamietajminwpodwojnejlinii=110;
(gdb) print i
\$1 = 0
(gdb) print m
\$2 = 2486
(gdb) print zapamietajminwpodwojnejlinii
\$3 = 111
(gdb) ```

Now, figure out how come m ended up with such a big value.

Code:
```for(int i=0;i<50;i++)
{
for(int y=0;y<50;y++)
{
if(tab[y].x!=i) //HOW MANY POINTS ARE IN ONE LINE
{
m++; // NUMBER OF EMPTY LINES
}
}
}```

Perhaps you can begin by explaining
- why 50; surely you need to related to the number of pointed entered, not some random magic number.
- why tab[y].x!=i does what the comment says
- why the value in m (2486) is almost 50*50 (the number of times this nested loop runs).
- why m is significant in the later subscripts.

> I tried working with debugger but it was of no help, since I do not fulluy understand the way the debugger works.
You run the code, you wait for it to crash, then you print some key variables.
It's not much, but it's a hell of a lot better than dumping your code and "it doesn't work, plzhelpmei'mauselessnoob"

#12
January 20th, 2013, 04:29 PM
 1415926535
Registered User

Join Date: Jan 2013
Posts: 7
Time spent in forums: 2 h 35 m 44 sec
Reputation Power: 0
I need to admit that you are right. Knowing that it is pointless, still I apologize for not reading about cross pointing. It's not an excuse - this is my fault. I ll try understand your advice then ask a question. Once again, thanks for help, and sorry for idiotism.

My environment = dev 5.2.0.3

#13
January 20th, 2013, 04:47 PM
 1415926535
Registered User

Join Date: Jan 2013
Posts: 7
Time spent in forums: 2 h 35 m 44 sec
Reputation Power: 0
To prove you wrong ~not a useless noob. I will do my best in answering your questions how I see them.

Code:
```for(int i=0;i<50;i++)
{
for(int y=0;y<n;y++)
{
if(tab[y].y!=i)	//how many lines are empty
{
m++; // NUMBER OF EMPTY LINES
}
}
}```

Why 50?

It is size of my board on which I print coordinates. I am sure that it takes every line on the board, compares it with the value on every Y-COORDINATE, then checks how many times the line is empty. After changing to what is in that post I am getting a mistake in line 124.
Now it does exactly what the comment says.
Why is it so large? Because I should have put "n" instead of 50 in the 2nd loop but I did not.

Code:
```void Distance (point Arr[1000], int n, int m)

{
int h = 50-m;  // Size of a board minus amount of empty lines.
double sum=0;
for(int i = 0;i<2*m;i++)
{
sum+=sqrt(pow((Arr[i+1].x-Arr[i].x),2)+pow((Arr[i+1].y-Arr[i].y),2));
}
sum=sum+sqrt(pow((Arr[2*h-1].x-Arr[0].x),2)+pow((Arr[2*h-1].y-Arr[0].y),2));
printf("%lf\n", sum);```

I made a stupid mistake there, should put 50-h.

And now the code compiles.

With a little conundrum, namely I keep getting 2,36 for any answer I have tried so far:
3
1 2
3 4
5 6

and

5
1 2
3 4
5 6
7 8
9 10

and..
2
1 5
8 3
I get 34419....

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Longest envelope of the set of points.