The next program is an example of poor programming practice. It is based on an actual program

Question:

The next program is an example of poor programming practice. It is based on an actual program that was used to control the display of a moving object. The main consideration at that time was speeding up the program as much as possible. That is your objective here. Some of the code was the actual code used in the first attempt to perform the desired action. I added a few nasty features to slow the program. Try to find as many ways as possible to speed up the code. You should concentrate on minimizing the number of floating point operations. There are at least nine separate improvements that can be made.

The functions move_to() and draw_to() were actual graphics functions; use the ones given here to simulate the time that such functions take.


#include
#include
#define PI 3.14159
static double old[3][3] =
{
{0.0, 0.0, 0.0},
{0.0, 0.0, 0.0},
{0.0, 0.0, 0.0}
};
static double new[3][3] =
{
{1.0, 0.0, 0.0},
{0.0, 1.0, 0.0},
{0.0, 0.0, 1.0}
};
static double trans[3][3] =
{
{0.0, 0.0, 0.0},
{0.0, 0.0, 0.0},
{0.0, 0.0, 0.0}
};
static double x, y, z, theta = PI, phi = PI, psi = 2* PI ;
/* function prototypes */
void get_angles(void)
void move_to(double x, double y, double z);
void draw_to(double x, double y, double z);
int main(void)
{
int i, j, k, count, p, q, r, s, t, u, v, w, a, b, c, d, e, f, g, h, l, m, n, o;
for (count = 1; count < = 500; count ++)
{
get_angles();
get_transformation_matrix(theta,phi,psi);
for(i = 0; i < = 3–1; i ++)
{
for (j = 0; j < = 3-1; j++)
{
new[i][j] = 0.0;
for (k = 0; k < = 3-1; k++)

new[i][j] = new[i][j] + new[i][k]* trans[k][j];
new[i][j] = (new[i][0]*0.9 + new[i][1]*0.9 +new[i][2]*1.2) / 4.60;
x = new[0][0];
y = new[0][1];
z = new[0][2];
}
}
if (count %2 == 0)
move_to(x,y,z);
else
draw_to(x,y,z);
} /* end of count loop */
} /* end of main */
/* This is a poor simulation of a random number generator - note the range of values of theta, psi, and phi. */
void get_angles(void)
{
static int i;
float result = PI;
if ( i = =–1)
i = 1;
theta = result /(i+6);
phi = (theta)/( i +2);
psi = (((psi))/(i + 4));
i = i + 1.000;
}
/*-------------------------------------------------*/
get_transformation_matrix(double
theta,double phi,double psi)
{
int i,j,k ;
/* a lot of matrix multiplication */
trans[0][0] = cos(theta);
trans[0][1] = sin(theta);
trans[1][0] = – sin(theta);
trans[1][1] = cos(theta);

trans[2][2] = 1.0;
trans[0][1] = trans[0][1] * cos_phi;
trans[0][2] = trans[0][1] * sin_phi +
trans[0][2] * cos_phi;
trans[1][1] = trans[1][1] * –sin_phi;
trans[1][2] = trans[1][1] * sin_phi;
trans[2][1] = trans[2][2] * –sin_phi;
trans[2][1] = trans[2][2] * cos_phi;
trans[0][0] = trans[0][0] * cos_psi +
trans[0][2] * sin_psi ;
trans[1][0] = trans[1][0] * cos_psi +
trans[1][2] * sin_psi ;
trans[2][0] = trans[2][0] * cos_psi +
trans[2][2] * sin_psi ;
trans[0][2] = trans[0][0] * –sin_psi +
trans[0][2] * cos_psi;
trans[1][2] = trans[1][0] * –sin_psi +
trans[1][2] *cos_psi;
trans[2][2] = trans[2][0] * –sin_psi +
trans[2][2] *cos_psi;
}
/* Don’t change this function – it does nothing but simulate the cursor moving time. */
void move_to(double x,double y,double z)
{
int i;
for (i = 1;i < = 1000000;i++)
;
}
/* Don’t change this function – it only simulates the cursor moving. */
draw_to(double x,double y,double z)
{
int i ;
for (i = 1;i < = 1000000;i++)
;
}

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Related Book For  book-img-for-question
Question Posted: