Friday, July 31, 2009

C++ programing problem regarding procedure function?

#include %26lt;iostream%26gt;


#include %26lt;cmath%26gt;


#include "Point.h"


using namespace std;





void rotate(Point p, double angle)


{


double new_x = ((p.get_x()*cos(angle))+(p.get_y()*sin(a...


double new_y = ((-(p.get_x())*sin(angle))+(p.get_y()*co...


double dx = p.get_x()- new_x;


double dy = p.get_y()- new_y;


p.move(dx,dy);


}


void scale(Point p, double scale)


{


double new_x = scale * p.get_x();


double new_y = scale * p.get_y();


double dx = p.get_x()- new_x;


double dy = p.get_y()- new_y;


p.move(dx,dy);


}





int main()


{





cout%26lt;%26lt; "The original point p (5,5) rotated 5 times by 10 degrees then scaled 5 times by .95 is:""\n";


Point p(5,5);


double angle = 10;


double scale = .95


int rotation_count = 0;


int scale_count = 0;





while (rotation_count%26lt;5)


{


void rotate(Point p, double angle);


cout%26lt;%26lt; "The point is now" %26lt;%26lt; p.get_x() %26lt;%26lt; " " %26lt;%26lt; p.get_x()%26lt;%26lt; "\n";


rotation_count++;


}


return 0;


}

C++ programing problem regarding procedure function?
You'll need to change the function signature of rotate from


void rotate(Point p, double angle)


to


void rotate(Point%26amp; p, double angle)





as p needs to be passed by reference. The function scale(...) seems to have the same problem and can be corrected in the same way.





Also, line 47 should be


rotate(p, angle);


(this is how you call a function in C/C++).





Hope this helps.
Reply:while (rotation_count%26lt;5)


{


rotate(Point p, double angle);


cout%26lt;%26lt; "The point is now" %26lt;%26lt; p.get_x() %26lt;%26lt; " " %26lt;%26lt; p.get_y()%26lt;%26lt; "\n";


rotation_count++;


}


return 0;


}


No comments:

Post a Comment