Hi, I should printout the value(s) of the "sum" of the end of the code... But i don't know...:( I am not good at C++. Please help!
The code is not so difficult: it generates random 3 dimensional vectors, e.g. number of 5. Then makes a matrix of 3x3 from a vector and another vector makes another matrix, ...etc. And finally adds the five matrix and divide by 5. The sum of the 5 matrix in the end of the code is "sum". I should printout this matrix.
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <ctime>
#include <cmath>
using namespace std;
const float M_2PIf = 6.283185307179586476925286766559f;
const float M_PIf = 3.141592653589793238462643383279f;
/* -----------------RANDOM_GENERATOR------------------------------- */
float rand(float min, float max)
{
return min + (max - min) * rand() / (float)RAND_MAX;
}
/*------------------------------------------------------------------*/
struct vektor {
float x,y,z;
};
/* ------------------MATRIX_CLASS-----------------------------------*/
class Matrix {
private:
float tomb[3][3];
public:
void init(vektor &a){
tomb[0][0] = a.x * a.x;
tomb[0][1] = a.x * a.y;
tomb[0][2] = a.x * a.z;
tomb[1][0] = a.y * a.x;
tomb[1][1] = a.y * a.y;
tomb[1][2] = a.y * a.z;
tomb[2][0] = a.z * a.x;
tomb[2][1] = a.z * a.y;
tomb[2][2] = a.z * a.z;
}
Matrix &operator +=(Matrix m){
int k,l;
for(k=0; k<3; k++)
for(l=0; l<3; l++)
tomb[k][l] += m.tomb[k][l];
return *this;
}
Matrix &operator/=(float c){
int k,l;
for(k=0; k<3; k++)
for(l=0; l<3; l++)
tomb[k][l] /= c;
return *this;
}
};
/* -------------------------------------------------------------------- */
int main() {
int i,j;
vektor tar[5];
for (i = 0;i<5;i++) {
float phi = rand(0.0f, M_2PIf);
float costheta = rand(-1.0f, 1.0f);
float x = 1.0f * sqrtf(1-costheta*costheta) * cosf(phi);
float y = 1.0f * sqrtf(1-costheta*costheta) * sinf(phi);
float z = 1.0f * costheta;
tar[i].x = x;
tar[i].y = y;
tar[i].z = z;
}
Matrix m[5];
for (i=0;i<5;i++) {
m[i].init(tar[i]);
}
Matrix sum = m[0];
for (i=0;i<5;i++) {
sum += m[i];
}
sum /= 5;
return 0;
}