Click here to Skip to main content
15,921,697 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello,
In my code I’m trying to find the distance between two points
(X1,Y1) & (X2,Y2)
These numbers are generating from the first part of the code
I got the first distance correctly but its keep repeating itself , I don’t know why
Any suggestion is appreciated

C++
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>

using namespace std;


int main()
{
                  /// generating 100 point, find x,y for each point///
	int  Radio_Range=80 ,No_Max=100 ;
	int  x, y ;
	int  Total_Degree=0;
	int GridF[100][2];
	ofstream  myfile;  
	myfile.open ("example.txt");  
	int row=0;
	int miny=0,max_y=99;
	for(int i=0;i<5;i++)
	{
		int minx=0,max_x=99;
		for(int j=0;j<5;j++)
		{
			for(int k=0;k<4;k++)
			{
				x= minx+rand()%(max_x - minx+1);
				y= miny+rand()%(max_y - miny+1);

				GridF[row][0]= x;
				GridF[row][1]= y;

				

				cout<<GridF[row][0]<<"  "<<GridF[row][1]<<endl;
				
				myfile<<GridF[row][0]<<"  "<<GridF[row][1]<<endl;
				
			
			}
			minx=max_x+1;
			max_x=max_x+100;
		}
		miny=max_y+1;
		max_y=max_y+100; 
	} 
		



/////////////////// Find the distance btw 2 points///////////

	double result ,xGrid,yGrid;
	int DistanceCounter=0;	
	cout<<"Distance : "<<endl;
	cout<<endl;
	
	for(int i=0;i<50;i++)
	{
	for(int j=0;j<2;j++)
	{
 
	
//xresult
		xGrid=(GridF[j+1][0] - GridF[j][j]);?????????????
		xGrid=abs(xGrid); xGrid=xGrid*xGrid;
		cout<<"xGrid="<<xGrid<<endl;
//yresult
		
		yGrid=(GridF[j+1][j+1] - GridF[j][j+1]);?????????
		yGrid=abs(yGrid); yGrid=yGrid*yGrid;
		cout<<"yGrid="<<yGrid<<endl;
////result
		result = xGrid + yGrid;
		result = sqrt (result);
		cout<<"Equation : "<<result<<endl;
	
		
	}
	
	}

	}


I thik the problem occur in GridF[][]// 2D array, is it the right way ??
Posted
Updated 29-Nov-12 9:03am
v2
Comments
CPallini 29-Nov-12 17:06pm    
It is not clear what are you trying to do. Could you please elaborate a bit?

The first issue, as already explained is the missing row variable.
However, the second is of a similar ilk, regarding the second part of the code...

Your second loop uses i from 0-49 and j from 0-1. You are only accessing GridF with j + 1. As a result xGrid and yGrid are always accessing the same index (1) - resulting in identical values.

As a "proof of concept", you should really have a single loop from 0-99 and access the array index using a single variable. If the code is a stripped down version of production code, then you must access the GridF array using a operation of both i and j.

I see a third problem in that your array has 100 values, but the loop for i only reaches 50.

A "proof of concept" solution would be the following:
for (int i = 1; i < 100; ++i)
{
  xGrid = abs(GridF[i][0] - GridF[i - 1][0]);
  xGrid *= xGrid;
  cout << "xGrid=" << xGrid << endl;

  yGrid = abs(GridF[i][1] - GridF[i - 1][1]);
  yGrid *= yGrid;
  cout << "yGrid=" << yGrid << endl;

  result = sqrt(xGrid + yGrid);
  cout << "Equation : " << result << endl;
}


Also note the obvious facts:
1) You will get 99 outputs, not 100,
2) There is no validation (if required for min-max values),
3) Consideration for the double-int casting.
 
Share this answer
 
Comments
laian2 1-Dec-12 4:58am    
thanx allot
i got the answer :
Equation : 67.3647
Equation : 42.4382
Equation : 35.171
Equation : 84.214
Equation : 60.0833
Equation : 78.1025
Equation : 67.0522
Equation : 142.678
Equation : 68.2642
Equation : 71.5542
Equation : 101.597
Equation : 192.2
Equation : 96.9381
Equation : 79.0569
Equation : 74.8465
Equation : 124.579
Equation : 26.0768
Equation : 87.023
Interesting code. I can't find where you change your row variable. It looks like it always stays as 0. In this case you first tripple-nested loop always assigns values to GridF[0][0] and GridF[0][1] elements like 5000 times in a row, leaving all other elements unassigned. I can easily fix your code, but please try to fix it yourself first and let us know about the results
 
Share this answer
 
Comments
laian2 30-Nov-12 9:46am    
u mean i have to make it like this
....
row++

ok i will try it :)
laian2 30-Nov-12 10:30am    
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647
xGrid=49
yGrid=4489
Equation : 67.3647

i have the same result
:/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900