Difference between revisions of "C++:3D Exercises"

From Progzoo
Jump to: navigation, search
Line 29: Line 29:
 
}
 
}
 
]]></prog>
 
]]></prog>
 +
 +
<answer><![CDATA[
 +
#include <math.h>
 +
#include <iostream>
 +
using namespace std;
 +
 +
float *subtract(float a[3], float b[3]){
 +
  float *r = new float[3];
 +
  for (int i=0;i<3;i++)
 +
    r[i] = a[i]-b[i];
 +
  return r;
 +
}
 +
 +
float *crossproduct(float a[3], float b[3]){
 +
  float *r = new float[3];
 +
  r[0] = a[1]*b[2]-a[2]*b[1];
 +
  r[1] = a[2]*b[0]-a[0]*b[2];
 +
  r[2] = a[0]*b[1]-a[1]*b[0];
 +
  return r;
 +
}
 +
 +
int main(int argc, char **argv)
 +
{
 +
  float A[]={1,0,1};
 +
  float B[]={1,1,2};
 +
  float *C = crossproduct(A,B);
 +
  cout << C[0]<<" "<<C[1]<<" "<<C[2]<< endl;
 +
  return 0;
 +
}
 +
]]></answer>
 
</question>
 
</question>

Revision as of 08:23, 23 November 2011

Calculate the cross product of two vectors. Fill in the details in the function crossproduct



[Font] [Default] [Show] [Resize] [History] [Profile]