Friday, July 31, 2009

C++ arrays and functions?

Novice c++ guy here, trying to wrap my head around arrays and functions. This is supposed to be a program that takes the center and radii from 2 3d spheres and determines if they collide.





#include %26lt;iostream%26gt;


#include %26lt;math.h%26gt;


#include %26lt;string%26gt;


using namespace std;





struct sphere


{


float center[3];


float radius;


};





bool sphereDist (sphere %26amp;s1, sphere %26amp;s2)


{


return (pow(s2.center[0] - s1.center[0], 2) +


pow(s2.center[1] - s1.center[1], 2) +


pow(s2.center[2] - s1.center[2], 2)) %26lt;


(s1.radius + s2.radius);








}











void main ()


{


cout %26lt;%26lt;"Sphere collision detection program, by D.R."%26lt;%26lt; endl;


cout %26lt;%26lt;"Input the centers of sphere one"%26lt;%26lt; endl;


cin%26gt;%26gt;s1.center;


cout %26lt;%26lt;"input the radius of sphere one"%26lt;%26lt; endl;


cin%26gt;%26gt;s1.radius;


cout %26lt;%26lt;"Now input the centers of sphere two"%26lt;%26lt; endl;


cin%26gt;%26gt;s2.center;


cout %26lt;%26lt;"Please input the radius of sphere two"%26lt;%26lt; endl;


cin%26gt;%26gt;s2.radius;





if (sphereDist == true)


{


cout %26lt;%26lt;"The spheres collide." %26lt;%26lt; endl;


}


else


{


cout %26lt;%26lt; "the spheres do not collide." %26lt;%26lt;endl;


}

C++ arrays and functions?
when you call a function, you need to tell it what variables are going into it. You have "if (sphereDist == true)". First, if you have an if statement, and the condition is if a bool is true, you can just put "if (shereDist)" or "if (!shereDist)" if the condition were if it is false. But since you are calling a finction, you need to say


"if (shereDist(s1, s2))"


replace the s1 and s2 with the names of the 2 variables that you want to use in your shereDist function.
Reply:main should return int, not void


you need to define your spheres s1 and s2 in main


cin %26gt;%26gt; doesn't know what to do with an array of floats...


you need to call your function in the conditional in main:





#include %26lt;iostream%26gt;


#include %26lt;math.h%26gt;


#include %26lt;string%26gt;


using namespace std;





struct sphere


{


float center[3];


float radius;


};





bool sphereDist (sphere %26amp;s1, sphere %26amp;s2)


{


return (pow(s2.center[0] - s1.center[0], 2) +


pow(s2.center[1] - s1.center[1], 2) +


pow(s2.center[2] - s1.center[2], 2)) %26lt;


(s1.radius + s2.radius);








}








//int return type


int main ()


{


sphere s1, s2 ;


cout %26lt;%26lt;"Sphere collision detection program, by D.R."%26lt;%26lt; endl;


cout %26lt;%26lt;"Input the centers of sphere one"%26lt;%26lt; endl;


cin%26gt;%26gt;s1.center[0];


cin%26gt;%26gt;s1.center[1];


cin%26gt;%26gt;s1.center[2];


cout %26lt;%26lt;"input the radius of sphere one"%26lt;%26lt; endl;


cin%26gt;%26gt;s1.radius;


cout %26lt;%26lt;"Now input the centers of sphere two"%26lt;%26lt; endl;


cin%26gt;%26gt;s2.center[0];


cin%26gt;%26gt;s2.center[1];


cin%26gt;%26gt;s2.center[2];


cout %26lt;%26lt;"Please input the radius of sphere two"%26lt;%26lt; endl;


cin%26gt;%26gt;s2.radius;





if (sphereDist( s1, s2 ) == true)


{


cout %26lt;%26lt;"The spheres collide." %26lt;%26lt; endl;


}


else


{


cout %26lt;%26lt; "the spheres do not collide." %26lt;%26lt;endl;


}


}

daisy

No comments:

Post a Comment