• 0

[C++]Conceptual and Function Help


Question

Hi the program is supposed to use C++ to compare the faces of 3 anonymous suspects with a database of 25 people. In the database file you have the number of people (25) their first and last name on the next line the 5 x coordinates and the next line the y coordinates. It is repeated for all 25 people. The suspect files only contain 5 x and y coordinates.

The first step is to read in the database of people which I did with this.

while (!fin.eof())
	{
		//Input names and place them in outfile
		fin>>names[count_n];
		fout<<names[count_n];
		count_n++;
		fin>>names[count_n];
		fout<<" "<<names[count_n]<<"\n";
		//Find x coordinates
		fin>>coord[count_c];
		fin>>coord[count_c+2];
		fin>>coord[count_c+4];
		fin>>coord[count_c+6];
		fin>>coord[count_c+8];
		//Find y coordinates
		fin>>coord[count_c+1];
		fin>>coord[count_c+3];
		fin>>coord[count_c+5];
		fin>>coord[count_c+7];
		fin>>coord[count_c+9];
		//Output names and coordinates neatly
		fout<<"("<<coord[count_c]<<","<<coord[count_c+1]<<")\n";
		fout<<"("<<coord[count_c+2]<<","<<coord[count_c+3]<<")\n";
		fout<<"("<<coord[count_c+4]<<","<<coord[count_c+5]<<")\n";
		fout<<"("<<coord[count_c+6]<<","<<coord[count_c+7]<<")\n";
		fout<<"("<<coord[count_c+8]<<","<<coord[count_c+9]<<")\n";
		count_c+=10;
	}

So now I have the names in an array as well as the x and y coordinates in an array going [x,y,x,y,x,y...].

From there I have to write a function to calculate the distance of line segments based on the coordinates. There will be 10 line segment distances for each person because it has to test every possible combination of the coordinates (so 1-5,2-4,2-3,1-4...so on and so forth). Which I have tried to do here...

//Function Prototypes
void getdistance(double (&d)[D_SIZE],double c[D_SIZE],int n);

int main(void)
{
...
yadda
...
getdistance(distance,coord,D_SIZE);

return 0;
}

void getdistance(double (&d)[D_SIZE],double c[D_SIZE],int n)
{
	int count(0),count_d(0);

	while (count_d<n)
	{
		d[count_d]=sqrt(pow(c[count+2]-c[count],2)-pow(c[count+3]-c[count+1],2));
		count_d++;
		d[count_d]=sqrt(pow(c[count+4]-c[count],2)-pow(c[count+5]-c[count+1],2));
		count_d++;
		d[count_d]=sqrt(pow(c[count+6]-c[count],2)-pow(c[count+7]-c[count+1],2));
		count_d++;
		d[count_d]=sqrt(pow(c[count+8]-c[count],2)-pow(c[count+9]-c[count+1],2));
		count_d++;
		d[count_d]=sqrt(pow(c[count+8]-c[count+2],2)-pow(c[count+9]-c[count+3],2));
		count_d++;
		d[count_d]=sqrt(pow(c[count+6]-c[count+2],2)-pow(c[count+7]-c[count+3],2));
		count_d++;
		d[count_d]=sqrt(pow(c[count+4]-c[count+2],2)-pow(c[count+5]-c[count+3],2));
		count_d++;
		d[count_d]=sqrt(pow(c[count+8]-c[count+4],2)-pow(c[count+9]-c[count+5],2));
		count_d++;
		d[count_d]=sqrt(pow(c[count+6]-c[count+4],2)-pow(c[count+7]-c[count+5],2));
		count_d++;
		d[count_d]=sqrt(pow(c[count+8]-c[count+6],2)-pow(c[count+9]-c[count+7],2));
		count+=10;
	}
}

This is basically where it falls apart and I can't quite figure out why. I believe I am doing the formula right (d=sqrt(x2-x1)^2-(y2-y1)^2 however some of them are coming out wrong and also not all the values are getting calculated (the loop stops prematurely). I've tried setting a break and debugging but I couldn't find the error and it was really stressing me out so I gave it a break.

From there I have to find the sum of the differences in squares of their distances using this formula sum=(distance1-suspectdistance1)^2+(distance2-suspectdistance2)^2... etc. I can't really think of any way to do this outside of making a function and calling it 25 different times or 25 for loops and I know there has to be a better way to do it. So any bright ideas here would be useful. From there you have to choose the top 4 with the closest matching square which should just be a matter of a few easy lines.

If you need anymore code or information let me know. I fear it seems like I'm asking you to do my homework but I really need guidance more than having it done for me. Thanks.

Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.