Monday, July 27, 2009

Closest- pair program in C language?

the divide and conquer algorithm requires that its recursive, i know nothing about recursion, i'm really not good at recursion even though its already been discussed..





can anyone help me on the recursion part?








//the points which i've put in a struct are global so i dont have to pass parameters too much





i already have a function to:


*create points


%26gt;%26gt; void create();


*display the points


%26gt;%26gt;void display();


*find the distance of 2 points


%26gt;%26gt;distance(double x, double y)





my problem would be how to go about calling the distance function (recursively, ive been told) and in what order should i check the distance of the points.... should i make a function that would check a min of the distance?





HELP....!!!

Closest- pair program in C language?
the distance function would be whatever the function you wrote would be, i dont have enough information on your problem but im pretty sure you would go something like this:





dist_func(param 1, param 2);





what do you mean by min of the distance? look i know this formula from my geometry class i had a while back, maybe this could help you:





/*


complete distance2D formula code:


*/





#include %26lt;iostream%26gt;


#include %26lt;math.h%26gt; // if the complier throws an error remove


// the .h





using namespace std; // well use the standard namespace





// this is a function prototype, its an initalizer basically.


long 2D_Distance(long x1,long x2, long y1, long y2);





int main()


{


long firstX1,firstX2,firstY1,firstY2;





cout%26lt;%26lt;"Im going to demonstrate how to retrieve the distance between this cordinate set."%26lt;%26lt;endl;





firstX1 = 3.4;


firstX2 = 2.5;


firstY1 = 9.5;


firstY2 = 5.6;





cout%26lt;%26lt;"The Dist Of X1 %26amp; X2 %26amp; Y1 %26amp; Y2 is:"%26lt;%26lt;2D_Distance(firstX1,firstX2,firstY... , !"%26lt;%26lt;endl;





cout%26lt;%26lt;"\n \n Please press any key to clear the variables of the distance cordinates, and you can enter in your own cordinates:"%26lt;%26lt;endl;





system("pause");


system("cls");





cout%26lt;%26lt;"enter in x1 cord:"%26lt;%26lt;endl;


cin %26gt;%26gt; firstX1;


cout%26lt;%26lt;"enter in x2 cord:"%26lt;%26lt;endl;


cin %26gt;%26gt; firstX2;


cout%26lt;%26lt;"enter in y1 cord:"%26lt;%26lt;endl;


cin %26gt;%26gt; firstY1;


cout%26lt;%26lt;"enter in y2 cord:"%26lt;%26lt;endl;


cin %26gt;%26gt; firstY2;


cout%26lt;%26lt;"now preforming calcuation... \n \n"%26lt;%26lt;endl;


cout%26lt;%26lt;"x1="%26lt;%26lt;firstX1%26lt;%26lt;",x2="%26lt;%26lt;firstX2%26lt;... = "%26lt;%26lt;2D_Distance(firstX1,firstX2,firstY1,f...





cout%26lt;%26lt;"\n was this satisfactory? :-)"%26lt;%26lt;endl;


system("pause");


return 0;


}





long sqrd(value)


{


// their is no standard squared function in C++ math.h module,


// so i always have to write my own. this is pretty simple


return value*value;


}





long 2D_Distance(long x1,long x2, long y1, long y2)


{


long tempA,tempB,tempC,tempD,tempF;


tempA = (x2-x1);


tempB = (y2-y1);


tempC = sqrd(tempA);


tempD = sqrd(tempB);


tempF = tempC + tempD;


sqrt(tempF);


return tempF;


}








now im only 15 years old. So if my code isnt what you expected sorry, as i dont understand your question well enough , however i will kindly work further on your problem if you wish by dropping me an email at: aricholland2003@gmail.com if this did not solve your problem.





Hope it worked out for you :-)


No comments:

Post a Comment